answersLogoWhite

0

An ordinary queue is a first-in, first-out structure -- analogous with a queue of people waiting to be served. The simplest implementation employs a singly-linked circular list. This is like an ordinary singly-linked list, but the tail node points "down" to the head node rather than to NULL. By keeping track of the tail node (rather than the head node), we gain constant-time access to both the head and the tail through a single pointer.

The basic implementation details of the push and pop methods are as follows:

void push (Node* n) {

if (tail==NULL) { // the queue is empty

n->next = n; // the new node becomes the head (point to self)

} else { // the queue has one or more nodes

n->next = tail->next; // point to the current head of queue

}

tail = n; // the new node becomes the tail

}

void pop () {

if (tail==NULL) return; { // can't pop an empty queue

Node* head = tail->next; // temporarily point to the current head

if (head == tail) { // special case: a queue of 1 node (tail points to self)

free (head);

tail = NULL; // the queue is now empty

} else { // general case: a queue of 2 or more nodes

tail->next = head->next; // set the new head

free (head); // release the old head

}

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

Queue ADT Using Array?

implement the queue ADT using an array


How do you implement a FIFO structure?

FIFO is a first-in, first out structure. In other words, it is a queue. The most efficient way to implement a queue is with a circular array.


How many stacks needs to implement queue?

One.


What are advantages and disadvantages of circular queue over ordinary queue?

Circular queues are very efficient and work well with low level codes. Ordinary queues are the standard type of queue but they do not maximize memory data well.


Minimum number of queues needed to implement the priority queue?

Separated queue for every possible priority value.


What is most appropriate data structure to implement priority queue?

heap


Write an algorithm to implement any three operation of a queue?

8798797


How ordinary queue is solved in circular queue?

An ordinary queue requires constant time access to the first and last elements, because all insertions occur at the back of the queue and all extractions at the front. Thus we require two pointers. However, with a circular queue, the next element after the last element is always the first element, thus we gain constant-time access to both the front and back of the queue through a single pointer to the back of the queue.


Is it possible to implement stack and queues using linkes list?

Yes it is possible to implement stack and queue using linked list


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


How many minimum no of queues Rae needed to implement priority queue?

Two possible solutions: 1. Separated queue for every possible priority value. 2. One shared queue for every elements, sorted by priority.


Has C got a built-in queue library?

No.