Linked List Practice Questions 1. The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. /* head_ref is a double pointer which points to head (or start) pointer of linked list */ void reverse(node* head_ref) node* prev = NULL; node* current = head_ref; node* next; while (current!= NULL) next = current->link(); current->set_link(prev); prev = current; current = next; /*ADD A STATEMENT HERE*/ What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly reverses a linked list. A. head_ref = prev; B. head_ref = current; C. head_ref = next; D. head_ref = NULL; 2. The following function takes a simply-linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank. Choose the correct alternative to replace the blank line. node *move_to_front(node *head) node *p, *q; if ((head == NULL: (head->link() == NULL)) return head; q = NULL; p = head; while (p->link()!=null) q = p; p = p->link(); return head; A. q = NULL; p->link() = head; head = p; B. q->link() = NULL; head = p; p->link() = head; C. head = p; p->link() = q; q->link() = NULL;
D. q->link() = NULL; p->link() = head; head = p; 3. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is A. log 2 n B. n/2 C. log 2 n 1 D. n 4. What are the time complexities of finding 8th element from beginning and 8th element from end in a singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8. A. O(1) and O(n) B. O(1) and O(1) C. O(n) and O(1) D. O(n) and O(n) 5. You are given pointers to first and last nodes of a singly linked list, which of the following operations are dependent on the length of the linked list? A. Delete the first element B. Insert a new element as a first element C. Delete the last element of the list D. Add a new element at the end of the list 6. Consider the following function to traverse a linked list. void traverse(node *head) while (head->link()!= NULL) cout << head->data(); head = head->link(); Which of the following is FALSE about above function? A. The function may crash when the linked list is empty B. The function doesn't print the last node when the linked list is not empty
C. The function is implemented incorrectly because it changes head 7. What is the time complexity to count the number of elements in the linked list? a) O(1) b) O(n) c) O(logn) d) None of the mentioned 8. Implement a function (the header is shown as follows) to delete the last element in the singly linked list. node* removelast(node * head) 9. What is the functionality of the following piece of code? int function(int data) node* temp = header_ptr; int var = 0; while(temp!= NULL) if(temp->data() == data) return var; var++; temp = temp->link(); return -1; a) Find and delete a given element in the list b) Find and return the given element in the list c) Find and return the position of the given element in the list d) Find and insert a new element in the list 10. Assume the following declarations and statements: node *P1, *P2; int *P3; P1 = new node; P2 = new node; P3 = new int; Show the output produced by the following segments of code. Indicate if an error occurs. 1) P1->set_data(123); P2->set_data(456); P1->set_link(p2);
2) P1->set_data(12); P1 = P2; 3) P1->set_data(123); P2->set_data(456); cout << P2->link()->data(); 4) P1->set_data(12); P3->set_data(34); P2->set_link(P3); cout << P3->data(); 5) P1->set_data(111); P2->set_data(222); cout << P1->data() << P2->data(); cout << P1->link()->link()->data(); 6) P1->set_data(12); P1 = P2; cout << P2->link()->data(); 11. Assume the following declarations and statements: node *P1, *P2, *P3, *P4; P1 = new node; P2 = new node; P3 = new node; P4 = new node; P1->set_data("Cat"); P2->set_data("Dog"); P3->set_data("Ewe"); P4->set_data("Rat"); P2->set_link(P3); P3->set_link(P4); P4->set_link(NULL); Show the output produced by the following segments of code. Indicate if an error occurs.
1) P1 = P2->link(); 2) P4 = P1; cout << P4->data(); 3) P4->set_data(P1->data()); cout << P4->data(); 4) P1->set_data(P3->link()->data()); cout << P1->data() << P1->link()->data(); 5) P2->set_link(P3->link()); 6) P4->set_link(P1); cout << P4->link()->data(); 7) P4->set_link(P2->link()); P3 = P1->link(); cout << P1->link()->data() << P2->link()->data() << P3->link->data() << P4->link()->data(); 8) P4->set_link(P3); P4->link()->link() = P2; P4->link()->link()->link() = P1; P1 = NULL; cout << P1 << P2->link()->data() << P3->link()->data() << P4->link();