# 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; ` `} ` |

*chevron_right*

*filter_none*

**Choose the correct alternative to replace the blank line.**

(A) q = NULL; p->next = head; head = p;

(B) q->next = NULL; head = p; p->next = head;

(C) head = p; p->next = q; q->next = NULL;

(D) q->next = NULL; p->next = head; head = p;

Answer(D)

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).

ii) Set next of p as head (p->next = head).

iii) Make head as p ( head = p)

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

Answer (C)

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

Answer (C)

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.**

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.

## Recommended Posts:

- Data Structures and Algorithms | Set 34
- Data Structures and Algorithms | Set 31
- Data Structures and Algorithms | Set 29
- Data Structures and Algorithms | Set 28
- Data Structures and Algorithms | Set 30
- Data Structures and Algorithms | Set 36
- Data Structures and Algorithms | Set 35
- Data Structures and Algorithms | Set 33
- Data Structures and Algorithms | Set 38
- Data Structures and Algorithms | Set 32
- Data Structures and Algorithms | Set 27
- Data Structures and Algorithms | Set 26
- Data Structures and Algorithms | Set 15
- Data Structures and Algorithms | Set 14
- Data Structures and Algorithms | Set 13