# Data Structures and Algorithms | Set 24

Following questions have been asked in GATE CS 2010 exam.

1. The following C 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.

 `typedef` `struct` `node  ` `{ ` `  ``int` `value; ` `  ``struct` `node *next; ` `}Node; ` ` `  `Node *move_to_front(Node *head)  ` `{ ` `  ``Node *p, *q; ` `  ``if` `((head == NULL: || (head->next == NULL))  ` `    ``return` `head; ` `  ``q = NULL; p = head; ` `  ``while` `(p-> next !=NULL)  ` `  ``{ ` `    ``q = p; ` `    ``p = p->next; ` `  ``} ` `  ``_______________________________ ` `  ``return` `head; ` `} `

Choose the correct alternative to replace the blank line.
(C) head = p; p->next = q; q->next = NULL;

When the while loop ends, q contains address of second last node and p contains address of last node. So we need to do following things after while loop.
i) Set next of q as NULL (q->next = NULL).
Step (ii) must be performed before step (iii). If we change head first, then we lose track of head node in the original linked list.
See https://www.geeksforgeeks.org/?p=6850 for more details.

2. A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below. Which one of the following choices gives a possible order in which the key values could have been inserted in the table?
(A) 46, 42, 34, 52, 23, 33
(B) 34, 42, 23, 52, 33, 46
(C) 46, 34, 42, 23, 52, 33
(D) 42, 46, 33, 23, 34, 52

The sequence (A) doesn’t create the hash table as the element 52 appears before 23 in this sequence.
The sequence (B) doesn’t create the hash table as the element 33 appears before 46 in this sequence.
The sequence (C) creates the hash table as 42, 23 and 34 appear before 52 and 33, and 46 appears before 33.
The sequence (D) doesn’t create the hash table as the element 33 appears before 23 in this sequence.

3. How many different insertion sequences of the key values using the same hash function and linear probing will result in the hash table shown above?
(A) 10
(B) 20
(C) 30
(D) 40

In a valid insertion sequence, the elements 42, 23 and 34 must appear before 52 and 33, and 46 must appear before 33.
Total number of different sequences = 3! x 5 = 30
In the above expression, 3! is for elements 42, 23 and 34 as they can appear in any order, and 5 is for element 46 as it can appear at 5 different places.

Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc. My Personal Notes arrow_drop_up

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.