A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time? (GATE 2004)

**(A)** rear node

**(B)** front node

**(C)** not possible with a single pointer

**(D)** node next to front

**Answer:** **(A)** **Explanation:** Answer is not “(b) front node”, as we can not get rear from front in O(1), but if p is rear we can implement both enQueue and deQueue in O(1) because from rear we can get front in O(1). Below are sample functions. Note that these functions are just sample are not working. Code to handle base cases is missing.

`/* p is pointer to address of rear (double pointer). This function adds new ` ` ` `node after rear and updates rear which is *p to point to new node */` `void` `enQueue(` `struct` `node **p, ` `struct` `node *new_node) ` `{ ` ` ` `/* Missing code to handle base cases like *p is NULL */` ` ` ` ` `new_node->next = (*p)->next; ` ` ` `(*p)->next = new_node; ` ` ` `(*p) = new_node ` `/* new is now rear */` ` ` `/* Note that p->next is again front and p is rear */` ` ` ` ` `} ` ` ` `/* p is pointer to rear. This function removes the front element and ` ` ` `returns the new front */` `struct` `node *deQueue(` `struct` `node *p) ` `{ ` ` ` `/* Missing code to handle base cases like p is NULL, ` ` ` `p->next is NULL,... etc */` ` ` ` ` `struct` `node *temp = p->next->next; ` ` ` `p->next = p->next->next; ` ` ` `return` `temp; ` ` ` `/* Note that p->next is again front and p is rear */` `}` |

*chevron_right*

*filter_none*

Quiz of this Question

Quiz of this Question

## Recommended Posts:

- Data Structures | Linked List | Question 2
- Data Structures | Linked List | Question 9
- Data Structures | Linked List | Question 15
- Data Structures | Linked List | Question 6
- Data Structures | Linked List | Question 3
- Data Structures | Linked List | Question 10
- Data Structures | Linked List | Question 11
- Data Structures | Linked List | Question 1
- Data Structures | Linked List | Question 17
- Data Structures | Linked List | Question 16
- Data Structures | Linked List | Question 7
- Data Structures | Linked List | Question 13
- Data Structures | Linked List | Question 14
- Data Structures | Linked List | Question 8
- Data Structures | Linked List | Question 4
- Data Structures | Linked List | Question 5
- Introduction to Data Structures | 10 most commonly used Data Structures
- Data Structures | Heap | Question 9
- Data Structures | Graph | Question 6
- Data Structures | Queue | Question 11