• edited by
43,038 views
56 56 votes

A circular queue has been implemented using a singly linked list where each node consists of a value and a single pointer pointing to the next node. We maintain exactly two external pointers FRONT and REAR pointing to the front node and the rear node of the queue, respectively. Which of the following statements is/are CORRECT for such a circular queue, so that insertion and deletion operations can be performed in $O(1)$ time?

  1. Next pointer of front node points to the rear node.
  2. Next pointer of rear node points to the front node.
  1. (I) only.
  2. (II) only.
  3. Both (I) and (II).
  4. Neither (I) nor (II).

12 Answers

Best answer
43 43 votes

Reference: https://gateoverflow.in/1033/gate2004-36

This is how the things look.

We do insertion by cutting in between Rear and Front and we do deletion by forwarding the Front pointer and updating the Rear accordingly.

Correct Answer: $B$

• edited by
28 28 votes

Answer is Next pointer to Rear node has Pointer to Front node.

Hence, only (II) is correct.

13 13 votes

Answer B

for circular queue using single link list, for enqueue and dequeue operation in O(1) time rear->next should point front

When you create a new node then there should be a pointer which points that newly created node . This pointer is necessary.

For enqueue

pointer->next = rear->next

rear->next=pointer

rear=pointer

 It takes O(1) times

For dequeue

rear->next = front->next

pointer=front ( this extra pointer required to    free the memory  deleted node)

front= rear->next

Free(pointer)

It takes O(1) times

8 8 votes
Since linked list is a dynamic data structure we don't need to worry about efficient space utilization (as we do in case of implementing circular queue using array). We can perform both enqueue and dequeue in constant time by using only front and rear pointers. Simply the next pointer of rear node points to NULL and next pointer of front node points to the node which was inserted just after front node in the  queue (i.e second element from left in the list). Enqueue is done at rear and dequeue is done from front ,both in constant time. So both options are false.  D should be the answer.

http://googleweblight.com/i?u=http://btechsmartclass.com/DS/U2_T9.html&grqid=wV00kE3q&hl=en-IN
• edited by
5 5 votes

Only Front and Rear pointers are enough to enqueue and dequeue in constant time in a circular queue.

Code for it can be easily found with a simple Google search. The heart of the algorithm is:

To enqueue, increment rear and add the element there

To dequeue, delete the element in "front", then increment front.

A circular queue has been implemented 

Question says that the queue is already circular, so we already have everything we need to perform enqueue and dequque in constant time.

Option D is the actual correct answer


However, the answer in the official key is Option B.

Option B can be the answer when the question asks what to do to implement enqueue and dequque ninconstant time. Then we do what Option B says, which will make the Queue a circular queue, hence making enqueue and dequue constant time operations.


Official answer: B. Actual answer: D

2 2 votes

A Circular Queue by definition ,https://en.wikipedia.org/wiki/Circular_buffer , is a Data Structure that uses a circular buffer of fixed size. Each element in this buffer points to the next element. 

Now initially , front and rear point to the same element . Upon insertion , rear moves forward, and upon deletion front moves forward. While trying to insert an element, if rear->next points to front, we know that buffer is full.

This image shows this clearly :

So , it is clear that , rear->next need not be front , and front->next need not be rear, and we will always have O(1) operations.

Therefore , answer is D

Edit: Note, there is a difference between a circular linked list, and implementing a circular queue using a circular linked list. In the first case, last element has to point to first, by definition .

In the second case, we will use a circular linked list, but front and rear have different meaning with respect to the queue and they are different from the first and last of the circular linked list(which will be fixed while front and rear vary).

• edited by
Answer:
Position:
Show:

Related questions

35 35 votes
6 answers 6 answers
15.2k
15.2k views
Arjun asked Feb 14, 2017
15,240 views
The pre-order traversal of a binary search tree is given by $12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20$. Then the post-order traversal of this tree is$2, 6, 7, 8, 9, 10, ...
1 1 vote
2 2 answers
1.2k
1.2k views
admin asked Apr 1, 2020
1,164 views
Which of the following is useful in traversing a given graph by breadth first search?StackSetListQueue
1 1 vote
2 2 answers
4.7k
4.7k views
admin asked Apr 1, 2020
4,692 views
If queue is implemented using arrays, what would be the worst run time complexity of queue and dequeue operations?$O(n),O(n)$$O(n),O(1)$$O(1),O(n)$$O(1),O(1)$
1 1 vote
3 3 answers
1.8k
1.8k views
admin asked Mar 30, 2020
1,791 views
A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT($n$ refers to t...