[go: up one dir, main page]

0% found this document useful (0 votes)
4 views6 pages

data structure assignment

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

10. Example of converting parse matrix into triplet ?

Ans:

11. Advantages of triplet over Sparse matrix ?


Ans: Memory Efficiency: Stores only non-zero elements and their indices, saving space compared to storing the entire
matrix structure.

12. Pointer variable used in stack ?


Ans: A pointer variable used in a stack typically serves as the **top pointer**, which keeps track of the index of the most
recent element added to the stack..

13. Definition of stack ?


Ans: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, where the last element added is
the first one to be removed. It supports two primary operations: push (adding an element to the top) and pop
(removing the top element).

14. Overflow and underflow condition of stack ?


Ans: if (top == MAX - 1) (Over Flow Condition)
if (top == -1) (Under Flow condition)

15. Condition where only element present in the stack ?


Ans: Condition with Only One Element in the Stack: The stack is neither full nor empty; it contains exactly one element.
This means `top == 0` and there are no other elements below it.

16. Condition after inserting a single element in the stack (stack is empty) ?
Ans: Condition After Inserting a Single Element: The stack will have one element, and the `top` pointer will be set to `0`
(indicating the index of the only element).

17. Function for traversing the stack ?


Ans: void traverseStack() {
if (top == -1) {
printf("Stack is empty.\n");
return;
}
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
18. Types of linked list ?
Ans: here are the 4 main Types of linked lists
1. Singly Linked list
2. Doubly Linked list
3. Circular Linked list.
4. Circular Doubly Linked List

.
31. Function for deleting a node at end of the single linked list ?
Ans: Node* ptr = *start;
Node* prev = NULL;
// Traverse to the last node
while (ptr->link != NULL) {
prev = ptr;
ptr = ptr->link;
} // If there is only one node
if (prev == NULL) {
*start = NULL; } else {
prev->link = NULL; // Remove the last node
}
free(ptr); // Free the memory of the node
}

32. Function for traversing a single linked list?


Ans: void traverse_list(Node* start) {
Node* ptr = start;
while (ptr != NULL) {
printf("%d -> ", ptr->info);
ptr = ptr->link;
} printf("NULL\n");
}
33. Function for merging two single linked list ?
Ans: void merge_lists(Node** start1, Node** start2) {
Node* ptr = *start1;
34. Definition and example of double circular linked list ?
Ans: A doubly circular linked list is a linked list where each node has two pointers: one to the next node and one to the
previous node, and the list is circular such that the last node points to the first node, and the first node points to the last
node.

35. Definition and types of queues ?


Ans: A queue is a linear data structure that follows the First In, First Out (FIFO) principle. In a queue, the element added
first is the one to be removed first.
Types of queues are : Simple Queue, Circular Queue, Deque (Double-Ended Queue), Priority Queue:, Priority
Deque:
36. Overflow condition for linear queue ?
Ans: if (rear == MAX_SIZE - 1) {
printf("Queue Overflow\n");
}
37. Underflow condition for linear queue ?
Ans: if (front > rear) {
printf("Queue Underflow\n");
}
38. Function for inserting elements into a queue (deletion) ?
Ans: void enqueue(Queue *q, int value) {
if (q->rear == MAX_SIZE - 1) {
printf("Queue Overflow\n"); } else {
q->data[++(q->rear)] = value;
}} .
27. Function for inserting a node at the Specific position of the single linked list ?
Ans: if (ptr == start) {
// Inserting at the beginning
fresh->link = start;
start = fresh; } else {
// Inserting in the middle or end
if (prev != NULL){
prev->link = fresh;
fresh->link = ptr; } }
28. Function for inserting a node at End of the single linked list ?
Ans: // Initialize the new node
fresh->link = NULL;
if (*start == NULL) {
// If the list is empty, make the new node the start
*start = fresh; }
else { Node* ptr = *start;
// Traverse to the end of the list
while (ptr->link != NULL)
{ ptr = ptr->link; }
// Insert the new node at the end
ptr->link = fresh; } }

29. Function for deleting a node at the beginning of the single linked list ?
Ans: void delete_at_beginning(Node** start) {
if (*start == NULL) {
printf("Underflow\n");
return; }
// Set PTR to the current start
Node* ptr = *start;
// Update START to the next node
*start = (*start)->link;
// Free the memory of the old start node
free(ptr); }
30. Function for deleting a node at particular position of the single linked list ?
Ans: void delete_at_position(Node** start, int loc) {
if (*start == NULL) {
// Check if the list is empty
printf("Underflow\n"); return;
}
Node* ptr = *start; Node* prev = NULL; int i = 1;
while (i < loc && ptr != NULL) {
prev = ptr;
ptr = ptr->link;
i++; }
if (ptr == NULL) {
printf("Location Not Found\n"); } else {
if (ptr == *start) {
node *start = ptr->link; } else {
// If deleting a node in the middle or end
prev->link = ptr->link; }
free(ptr);

.
39. Overflow condition for Circular queue ?
Ans: if ((rear + 1) % MAX_SIZE == front) {
printf("Queue Overflow\n");
}
40. Underflow condition for Circular queue ?
Ans: if (front > rear) {
printf("Queue Underflow\n");
}
41. Definition of dequeue and priority queue ?
Ans: Definition of Deque: A deque (double-ended queue) is a linear data structure that allows insertion and deletion of
elements from both the front and the rear ends. It combines features of both stacks and queues.
Definition of Priority Queue: A priority queue is a data structure where each element has a priority assigned to it.
Elements are dequeued based on their priority, with higher-priority elements being removed before lower-priority ones.

42. Types of dequeue ?


Ans: Input-Restricted Deque: Elements can only be added at one end (rear) but removed from both ends (front and rear).
Output-Restricted Deque: Elements can be added at both ends (front and rear) but can only be removed from one
end (front).
43. Difference b/w row major order and column major order ?
Ans: Column-Major Order:
Row-Major Order:

In row-major order, elements of a multi- In column-major order, elements are stored


dimensional array are stored in memory column by column. The entire column is stored
row by row. The entire row is stored in in contiguous memory locations before moving
contiguous memory locations before to the next column.
moving to the next row.

44. difference b/w linear and nonlinear data structure with proper example ?
Ans:
Linear Data-Structure Non -Linear Data-Structure

• In linear data structures, elements are • In nonlinear data structures, elements


arranged sequentially, and each element are arranged in a hierarchical or
has a unique predecessor and successor interconnected manner, where each
(except for the first and last elements). element can have multiple successors or
The data structure can be traversed in a predecessors. Traversal can involve
single level of sequence. multiple paths and levels.
Examples : Arrays, Linked Lists, Stacks, Examples: Trees, Graphs.
Queues.

45. Find the address of 10th location of the array element whose base address is 1000 and each element is having size 6
bits.
Ans: To find the address of the 10th element in an array, you can use the formula:
Address=Base Address+(Index * Size of each element)

Given:
Base Address (B) = 1000
Index (I) = 9
Size of each element (W) = 6 bits (which is 0.75 bytes, since 1 byte = 8 bits)
Address=1000+(9×0.75)=1000+6.75=1006.75

So, the address of the 10th element is 1006.75.

.
19. Smallest element of linked list ?
Ans: Smallest Element of Linked List: The smallest element is the one with the minimum value among all the nodes in the
linked list after traversing the entire list.

20. Use of pointer variables in linked list ?


Ans: Typically, a linked list uses at least one pointer variable per node to point to the next node. Additionally, you need a
pointer to the head of the list to manage the start of the list.

21. Definition and example of single linked list ?


Ans: Definition of Singly Linked List: A singly linked list is a data structure where each element (node) contains a data
value and a pointer to the next node in the sequence. It allows for traversal in one direction, from the head node to
the end of the list, where the last node points to NULL.

Example :

22. Overflow condition in single linked list ?


Ans: The condition FRESH= =NULL indicates that no free memory blocks (or nodes) exist in the memory. At this point of
time if we try to create a new node overflow situation occurs.

23. Underflow condition in single linked list ?


Ans: The condition START= =NULL indicates that linked list has no nodes or linked list does not exist. At this point of time
if we try to delete a node from linked list it leads to underflow situation.

24. Function for creating a node in single linked list ?


Ans: Set INFO[FRESH] := ITEM
Set LINK[FRESH] := NULL

25. Condition after inserting an element into a single lined list (single linked list is empty) ?
Ans: After inserting the first element into an empty singly linked list, the head pointer is no longer NULL; it points to the
newly created node. The next pointer of this new node is set to NULL.

26. Function for inserting a node at the beginning of the single linked list ?
Ans: void insertAtBeginning(int item) {
FRESH = nextFreeIndex;
nodes[FRESH].data = item;
if (START == -1) {
nodes[FRESH].next = -1;
} else {
nodes[FRESH].next = START;
}
START = FRESH;
nextFreeIndex++;
}

.
1. Definition of abstract Datatype ?
Ans : The process of providing only the essential and hiding the details is known as data abstraction.

2. Difference
Big-ohbetween Big-Oh
represents the(O) and bound
upper big omega notation ?
of the Big-omega represents the lower bound of the
Ans: running time of an algorithm. running time of an algorithm.
it gives the worst case complexity of an it gives the best case complexity of an
algorithm. algorithm.

3. Function of array creation ?


Ans: void createArray(int array[], int size, int default_value) {
for (int i = 0; i < size; i++) {
array[i] = default_value;
}
}

4. Function for array traversing ?


Ans: void traverseArray(int array[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
}

5. Condition for array overflow in an Array?


Ans: if (index < 0 || index >= size) {
}

6. Condition for array underflow in an Array ?


Ans: if (size == 0) {
}

7. Function for array insertion ?


Ans: for (int i = size; i > position; i--) {
array[i] = array[i - 1];
}
array[position] = element;
size++; }

8. Function for array deletion ?


Ans: if (position < 0 || position >= size) {
return; }
for (int i = position; i < size - 1; i++) { array[i] = array[i + 1]; }}

9. Definition and condition for Sparse matrix ?


Ans : 1. A sparse matrix is a matrix in which most of the elements are zero.
2. Condition for a Sparse Matrix
A matrix is considered sparse if the number of zero elements is greater than the number of non-zero
elements. Mathematically, a matrix is sparse if:
Number of zero elements > Number of non-zero elements.
.

You might also like