edited by
47,239 views
81 81 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

13 Answers

Best answer
152 152 votes

The pointer points to the Rear node.

EnQueue: Insert newNode after Rear, and make Rear point to the newly inserted node: 

//struct node *newNode;
newNode->next = rear->next;
rear->next = newNode;
rear=newNode;

DeQueue: Delete the Front node, and make the second node the front node.

//rear->next points to the front node.
//front->next points to the second node.
struct node* front;
front = rear->next;
rear->next = front->next;
free(front);


Since there has been a lot of confusion in the comments, let me explain the answer using the pointer `p`

Note that the answer remains unchanged – the below code is just written in a different style.

struct node *p; // a pointer to the rear node

void enqueue(struct node *item) {
    item->next = p->next;
    p->next = item;
    p = item;
}

void dequeue() {
    struct node* temp = p->next;
    p->next = p->next->next;
    free(temp);
}

 

A single variable $p$ is used to access the Queue

This means that the data-structure only tracks the rear node.

You can create as many temporary pointers as you want – that has nothing to do with the data-structure’s internals.

In enqueue, there’s an extra pointer struct node *item because if you wish to enqueue something, you need that thing in the first place.

In dequeue, there’s an extra pointer struct node *temp because if you don’t keep track of the thing that you’re removing in order to free the memory, you will have a memory leak in your code.

edited by
24 24 votes

The pointer points to the Rear node.


EnQueue: Insert newNode after Rear, and make Rear point to the newly inserted node:

//struct node *newNode;
newNode->next = p->next;
p->next = newNode;
rear=newNode;

DeQueue: Delete the Front node, and make the second node the front node.

rear->next = rear->next->next;
9 9 votes

Answer should be A.

P should point to Last node(rear), so as to perform enqueue in constant time.

To perform dequeue operation, since it is circular linked list, rear node will point front. Therefor dequeue can be performed as below:

temp= p->next->next//stores the address of node next to front node.

Delete (p->next)// delete current front node

p->next= temp// update the rear node to point to new front node.

Thus dequeue can also be perform in constant time.

edited by
1 1 vote
answer should be C

enqueue is possible with 1 pointer but for dequeue we have to take 1 more pointer bcoz if dequeue compulsory we have to free the memory also which is not possible with only 1 pointer.if modify then we can do whatever we want but in dequeue free the memory also.

@ arjun sir plz verify
1 1 vote
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
Answer:
Position:
Show:

Related questions

95 95 votes
6 answers 6 answers
29.3k
29.3k views
Kathleen asked Sep 18, 2014
29,294 views
Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among $\text{union, intersection, membership, cardinality}$ wil...
158 158 votes
15 answers 15 answers
58.9k
58.9k views
Kathleen asked Sep 18, 2014
58,919 views
A program takes as input a balanced binary search tree with $n$ leaf nodes and computes the value of a function $g(x)$ for each node $x$. If the cost of computing $g(x)$ ...
56 56 votes
3 answers 3 answers
13.9k
13.9k views
Kathleen asked Sep 18, 2014
13,909 views
Consider the following C program segmentstruct CellNode{ struct CellNode *leftChild int element; struct CellNode *rightChild; }; int Dosomething (struct CellNode *ptr) { ...
41 41 votes
3 answers 3 answers
13.9k
13.9k views
Kathleen asked Sep 18, 2014
13,856 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...