in Programming edited by
460 views
1 vote
1 vote

A circular linked list is used to represents a queue. A single variable L is used to access the queue. To which node should L point such that both the operation enqueue and dequeue can be performed in constant time ?

  1.  node next to front
  2.  front node
  3.  not possible with a single node
  4.  rear node
in Programming edited by
by
460 views

3 Answers

0 votes
0 votes
Answer is D) rear node because, from rear, we can go to front to delete and add a node(at rear) because we are at last node. We can't do this with any other node.
0 votes
0 votes

Enqueue is done at the rear.

Dequeue is done at the front.

 

So we need to point to a node such that we can access both the front and the rear easily (in $O(1)$ time).

In circular LL, if we point to the rear, we can access rear and front easily. So, Option D

// front == (rear + 1) mod n.

 


 

Why are other options incorrect?

If we point to the front, to access the rear node, we have to traverse all the way to the other end, ie, $O(n)$

So, Option B

By extension Option A

And obviously since rear node is the right answer, Option C

–1 vote
–1 vote

Ans is C

by

3 Comments

Answer D-- Rear Node is correct , just see this 

http://quiz.geeksforgeeks.org/data-structures-linked-list-question-12/

3
3
answer should be B. front node. as logic given in above link is right but direction is wrong. from front node dequeue operation occurs and from rear node new insertion is one. so direction moves from rear to front. just try to visualize.
0
0
Answer:

Related questions