Link List · Data Structures
20 questions · asked in 15 of 27 years (2000–2026)
MCQ 19 · NAT 11-mark 14 · 2-mark 6
2000Questions per year2026
Questions 6–10 of 20
Consider the following ANSI C program:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int value;
struct Node *next;};
int main( ) {
struct Node *boxE, *head, *boxN; int index=0;
boxE=head= (struct Node *) malloc(sizeof(struct Node));
head -> value = index;
for (index =1; index <= 3; index++){
boxN = (struct Node *) malloc (sizeof(struct Node));
boxE -> next = boxN;
boxN -> value = index;
boxE = boxN; }
for (index=0; index <= 3; index++) {
printf("Value at index %d is %d\n", index, head -> value);
head = head -> next;
printf("Value at index %d is %d\n", index+1, head -> value); } } Which one of the following statements below is correct about the program?Answer: (D)
The list has 4 nodes: the first node (value 0) made before the loop, and 3 more made in the loop (values 1, 2, 3). The field of the last node is never set, and malloc does not initialize memory, so it holds a garbage value.
The final for loop runs for , which is 4 passes. In each pass, is printed, then moved to the next node once, then printed again.
Passes 0, 1 and 2 move from the 1st node to the 2nd, 3rd and 4th node. In pass 3, already points to the 4th node. The statement then loads the garbage pointer, and in the next printf dereferences it. This is undefined behaviour and may crash at run time.
Why the others are wrong:
(A) The list has 4 nodes, not 5.
(B) The for loop runs a fixed 4 times, so it cannot loop forever.
(C) In C, may end without a return statement (from C99 it returns 0 by itself), so this is not a compile error.
Answer: (D)
The final for loop runs for , which is 4 passes. In each pass, is printed, then moved to the next node once, then printed again.
Passes 0, 1 and 2 move from the 1st node to the 2nd, 3rd and 4th node. In pass 3, already points to the 4th node. The statement then loads the garbage pointer, and in the next printf dereferences it. This is undefined behaviour and may crash at run time.
Why the others are wrong:
(A) The list has 4 nodes, not 5.
(B) The for loop runs a fixed 4 times, so it cannot loop forever.
(C) In C, may end without a return statement (from C99 it returns 0 by itself), so this is not a compile error.
Answer: (D)
What is the worst case time complexity of inserting n elements into an empty linked list, if the linked list needs to be maintained in sorted order?
Answer: (C)
The list must stay sorted, so every insertion first walks the list to find the right place. When the list already has k elements, this takes up to k steps.
Worst case total:
Answer:
Worst case total:
Answer:
Consider the C code fragment given below.
typedef struct node {
int data;
node* next ;
} node;
void join (node* m, node* n) {
node* p=n ;
while (p->next ! =NULL){
p = p -> next ;
}
p-> next = m;
} Assuming that m and n point to valid NULL- terminated linked lists, invocation of join willAnswer: (B)
The function join moves to the last node of list n and then links that node to m. If list n is empty (n = NULL), then p next is a null pointer dereference. In every other case, list m gets appended at the end of list n.
(The paper writes node* next inside the struct, which is not valid C; it would need struct node *next. The question is meant as if it were valid.)
Answer: either cause a null pointer dereference or append list m to the end of list n
(The paper writes node* next inside the struct, which is not valid C; it would need struct node *next. The question is meant as if it were valid.)
Answer: either cause a null pointer dereference or append list m to the end of list n
N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed.
An algorithm performs the following operations on the list in this order: , , , and . What is the time complexity of all these operations put together?
An algorithm performs the following operations on the list in this order: , , , and . What is the time complexity of all these operations put together?
Answer: (C)
The list is sorted and doubly linked. Delete and decrease-key are given a pointer to the node.
- delete: for one delete for all of them
- insert: first find the place, which takes for one insert
- find: for one find
- decrease-key: the node may need to move back towards the front, which takes for one operation
Total =
Answer:
- delete: for one delete for all of them
- insert: first find the place, which takes for one insert
- find: for one find
- decrease-key: the node may need to move back towards the front, which takes for one operation
Total =
Answer:
An algorithm performs find operations, insert operations, delete operations, and decrease-key operations on a set of data items with keys drawn from a linearly ordered set. For a delete operation, a pointer is provided to the record that must be deleted. For the decrease-key operation, a pointer is provided to the record that has its key decreased. Which one of the following data structures is the most suited for the algorithm to use, if the goal is to achieve the best total asymptotic complexity considering all the operations?
Answer: (A)
Add up the cost of all operations for each structure, with finds, N inserts, and deletes and decrease-keys.
Unsorted array: an insert is O(1), so N inserts cost O(N). A find scans the array, O(N), so finds cost . With a pointer, a delete (swap with the last element) and a decrease-key are O(1) each, so they cost in total. Total: .
Min-heap: an insert is O(log N) in the worst case, so N inserts cost . This is already more than the unsorted array. (A find still takes O(N) in a heap.)
Sorted array or sorted doubly linked list: an insert must find the position and shift or walk, O(N) each, so N inserts cost .
Since , the unsorted array is best.
Answer: (A)
Unsorted array: an insert is O(1), so N inserts cost O(N). A find scans the array, O(N), so finds cost . With a pointer, a delete (swap with the last element) and a decrease-key are O(1) each, so they cost in total. Total: .
Min-heap: an insert is O(log N) in the worst case, so N inserts cost . This is already more than the unsorted array. (A find still takes O(N) in a heap.)
Sorted array or sorted doubly linked list: an insert must find the position and shift or walk, O(N) each, so N inserts cost .
Since , the unsorted array is best.
Answer: (A)
Keep practising