| Traversal | Ends when NULL is reached | Can go on infinitely |
Q2. | Insertion at End | Needs traversal + NULL check | Easier with tail pointer |
| Uses | Simple data structures | Queues, circular buffers, etc. |
a) i) Write an algorithm to insert an element in a circular linked list in between the list
Steps: c) Merge two sorted lists into a third sorted linked list
1. Create a new node. Given two sorted lists A and B, merge into C.
2. Traverse to the required position.
3. Adjust pointers to insert node in between. Algorithm:
Step 1: Initialize pointers: pa = A, pb = B
Algorithm: Step 2: While pa != NULL and pb != NULL
Step 1: Create a new node temp with the given data. if pa->data < pb->data:
Step 2: Traverse the list to the node after which you want to insert (say position k). Add pa->data to C
Step 3: Let ptr be the node at position k. pa = pa->next
Step 4: temp->next = ptr->next else:
Step 5: ptr->next = temp Add pb->data to C
Step 6: Stop pb = pb->next
Step 3: If pa is not NULL, add remaining elements to C
ii) Insert at the end of the list Step 4: If pb is not NULL, add remaining elements to C
Algorithm: Step 5: Done
Step 1: Create a new node temp with data
Step 2: If list is empty: d) Write an algorithm to delete a node from the end of a doubly linked list
temp->next = temp Algorithm:
head = temp Step 1: If head == NULL
Else: Print "List is empty"
Traverse to the last node (node->next == head) Step 2: Traverse to the last node (tail)
last->next = temp Step 3: temp = tail->prev
temp->next = head Step 4: temp->next = NULL
Step 3: Stop Step 5: Free tail
Step 6: Done
b) Differentiate between Singly and Circular Linked List
| Feature | Singly Linked List | Circular Linked List | Q3.
|------------------------|--------------------------------|----------------------------------|
| Last Node | Points to NULL | Points back to head | a) Write an algorithm to perform all operations on a queue using static allocation
Using arrays:
#define MAX 100 c) Convert infix to postfix using stack:
int queue[MAX], front = -1, rear = -1; Given: A * (B * (C / (D * E) - F)) + G
Rules:
Enqueue: - Operands -> output
Step 1: If rear == MAX - 1 - Left Parenthesis -> stack
Overflow - Right Parenthesis -> pop until left paren
Step 2: Else if front == -1 - Operators -> precedence rules
front = rear = 0
Else Postfix: A B C D E * / F - * G + *
rear++
Step 3: queue[rear] = value d) Applications of a linear queue
- CPU scheduling
Dequeue: - Print queue
Step 1: If front == -1 - Disk scheduling
Underflow - Call center system
Step 2: value = queue[front] - Order processing systems
Step 3: If front == rear
front = rear = -1 Q4.
Else
front++ a) Write an algorithm to perform DFS on a graph
DFS (Depth First Search):
Peek: Return queue[front] Algorithm (recursive):
Traverse: Loop from front to rear Step 1: Start from source node
Step 2: Mark node as visited
b) Explain application of stacks with examples Step 3: Visit all unvisited adjacent nodes recursively
1. Expression Evaluation: Postfix and prefix evaluation.
2. Backtracking: Undo operations in text editors. Example DFS on given graph from node A:
3. Function Calls: Call stack manages recursive functions. Order may vary but example: A -> B -> E -> H -> F -> C -> D -> G
4. Parsing: Used in compilers for syntax parsing.
5. Browser History: Back/forward operations. b) Reconstruct binary tree from inorder and preorder; find postorder
Given:
Example: Balancing parentheses using stack. - Inorder: 4, 2, 5, 1, 3, 6
- Preorder: 1, 2, 4, 5, 3, 6 Q5.
Constructed Tree: a) Write an algorithm to search an element in array using binary search
1 Algorithm (iterative):
/ \ Step 1: Set low = 0, high = n-1
2 3 Step 2: While low <= high
/ \ \ mid = (low + high)/2
4 5 6 if arr[mid] == key -> Found
else if key < arr[mid] -> high = mid - 1
Postorder: 4, 5, 2, 6, 3, 1 else -> low = mid + 1
Step 3: Not Found
c) Create AVL tree from given sequence
Values: 25, 36, 40, 20, 23, 10, 48, 38, 12, 28 Time Complexity: O(log n)
Insert one by one and perform rotation if balance factor > 1 or < -1. b) Discuss all hash functions with examples
Rotations: Hash Function: Converts key into index in hash table.
- Right Rotation
- Left Rotation Types:
- Left-Right Rotation 1. Division Method: h(k) = k % m
- Right-Left Rotation 2. Multiplication Method: h(k) = floor(m * frac(k*A))
3. Folding Method: Split digits, add parts
Example: 4. Mid-square: Square key and extract middle digits
After inserting 36 -> 40: balance(25) = -2 -> Left Rotation
Example:
d) Compare BFS and DFS For key = 123456 and m = 100
| Feature | BFS | DFS | - Division: 123456 % 100 = 56
|-----------------|----------------------------|----------------------------|
| Data Structure | Queue | Stack (recursion/explicit) | c) Sort an array using bubble sort and calculate time complexity
| Traversal | Level-wise | Depth-wise | Bubble Sort Algorithm:
| Memory | More (stores all nodes) | Less (stack-based) | Step 1: For i = 0 to n-1
| Shortest Path | Yes | No | For j = 0 to n-i-1
| Suitable For | Shortest path, layers | Topological sort, components | If arr[j] > arr[j+1], swap
Time Complexity:
- Worst Case: O(n^2)
- Best Case: O(n) if optimized with swap flag
d) Create Max Heap from sequence
Given: 48, 72, 36, 8, 79, 22, 84, 66
Steps:
- Insert each element one by one and heapify up
- Alternatively, build heap from array using heapify (bottom-up)
Max Heap: Largest element at root.
Resultant max-heap (array form): 84, 79, 72, 66, 48, 22, 36, 8