answersLogoWhite

0

Explain how circular queue differs from linear queue?

Updated: 8/21/2019
User Avatar

Wiki User

6y ago

Best Answer

Both linear and circular queues are constructed from nodes:

struct node { T data; // the node's value (of type T)

node* next; // refers to the next node in the sequence

};

To implement a queue efficiently, we need to keep track of both the front and back of the queue. This is because all insertions occur at the back of the queue while all extractions occur at the front of the queue, and both operations must be performed in constant time.

struct queue {

node* head; // refers to the front of the queue

node* tail; // refers to the back of the queue

};

Initially, the queue is empty, so both the head and tail must be set to NULL.

We use the following function to insert some data at the back of the queue:

node* push (queue* q, T value) {

node* n = malloc (sizeof (node));

n->data = value;

n->next = NULL;

if (q->tail == NULL) {

q->head = n;

q->tail = n;

} else {

q->tail->next = n;

q->tail = n;

}

return n;

}

The following function extracts the head node:

void pop (queue* q) {

node* n = q->head;

if (q->head) {

q->head = q->head->next; // may be NULL

if (!q->head) q->tail = NULL;

}

free n;

}

In a circular queue, we don't need to keep track of the head because tail->next always points to the head unless tail is NULL. So we can eliminate the head pointer from the queue structure:

struct queue {

node* tail;

};

The insertion function therefore changes as follows:

node* push (queue* q, T value) {

node* n = malloc (sizeof (node));

n->data = value;

if (q->tail == NULL) {

n->next = n;

q->tail = n;

} else {

n->next = q->tail->next;

q->tail->next = n;

q->tail = n;

}

return n;

}

And the extraction function changes as follows:

void pop (queue* q) {

if (q->tail == NULL) return; // empty list

node* n = q->tail;

if (q->tail == q->tail->next) // a list of one

q->tail = NULL;

else

q->tail->next = q->tail->next->next; // adjust the head

free n;

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain how circular queue differs from linear queue?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How compiler differentiate between linear queue and circular queue?

It doesn't.


Difference between circular queue and linear queue?

In circular queue the memory of the deleted process can be used by some other new process..


What is the difference between linear and circular queue?

What is the difference between linear and circular queue? In: http://wiki.answers.com/Q/FAQ/2545-37 [Edit categories]The Queue by Default is Linear, it would be termed as Circular if the Last Element of the Queue pointsto the first element of the List


Define is circular queue with example?

A circular queue uses the same conventions as that of linear queue. Using Front will always point one position counterclockwise from the first element in the queue.


What is queue explain the basic element of queue?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Following are the types of queue: Linear queue Circular queue Priority queue Double ended queue ( or deque )


What is circular queues Explain in detail along with example?

circular queue


What are types of Queue?

Queue is a data structure which is based on FIFO that is first in first out. Following are the types of queue: Linear queue Circular queue Priority queue Double ended queue ( or deque )


What is the need of circular queue in data structures?

Circular queue is a linear data structure that follows the First In First Out principle. A re-buffering problem often occurs for each dequeue operation in a standard queue data structure. This is solved by using a circular queue which joins the front and rear ends of a queue.


What is circular queue .operations circular queue.?

Queue is difined as a special type of data structure .where elements are inserted one end &elements are deleted same end .The end from where they elements are inserted is called as "Rear end".The end from where elements are deleted is called "Front end". A linear queue is akin to the queue at the post office: it can be envisioned to be linear in space, and limited in space. When a linear queue of finite capacity is full, new arrivals are turned away (elements cannot be added, post office customers walk away). A circular queue behaves just like a normal queue, but is typically implemented in a a structure akin to a circle. The typical behavior is that the circular queue, when full, does not turn new entries away, but makes space by removing the oldest element in the queue. Of course, a queue might also be implemented to be of infinite, or virtually infinite, capacity.


What is linear queue?

What is linear queue


Difference between simple queue and circular queue?

Simple queue is a linear queue having front & rear var to insert & delete elements from the list.But there is a boundary that we have to insert at rear & have delete from front.For this reason instead of having space in the queue if there is a single element in the rear,the queue is full.the other space is wasted.To utilize space properly,circular queue is derived.In this queue the elements are inserted in circular manner.So that no space is wasted at all.


Circular queue in linear data structure?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer.