edited by
31,064 views
55 votes
55 votes

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 $\text{enQueue}$ and $\text{deQueue}$ can be performed in constant time?

  1. rear node
  2. front node
  3. not possible with a single pointer
  4. node next to front
edited by

8 Answers

0 votes
0 votes
Answer can be both....if p points to rear then answer is already given

Now the  case if p points to front:

Enqueue:

1.save the data of the node pointed by p in a temp variable.

2.copy the data of new node to the node p is pointing to.

3.copy the data of the temp variable to the new node created

4.make newnode->next=p->next point to the new node

5.make p->next=newnode

6.make p=p->next

 

Dequeue:

1.copy the data of the node in p->next to p.

2.make p->next=p->next->next
0 votes
0 votes
We do insertion by cutting in between Rear and Front

and

we do deletion by forwarding Front pointer and updating Rear accordingly. so ans is rear end
edited by
0 votes
0 votes
ans is c    not possible with a single pointer bcoz any node to point every time cross the circal …!!
–2 votes
–2 votes
I think there is no front or rear pointer here only p pointer so enQueue() and deQueue operation should be this

1.) newNode-->next=p-->next-->next;

2.) p-->next-->next=NULL;

3.) free(p-->next);

4.) p-->next=newNode;
Answer:

Related questions

62 votes
62 votes
4 answers
1
103 votes
103 votes
11 answers
2
27 votes
27 votes
3 answers
4
Kathleen asked Sep 18, 2014
8,945 views
Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely?preorder and postorderi...