What is Queue?

“A queue is a linear data structure into which items can only be inserted at one end and removed from the other.”

A queue is a FIFO (First In First Out) structure. Means, the 1st inserted element will be removed 1st.

Queue in Real Life

We queue up while depositing a utility bill or purchasing a ticket. The person who has joined the queue first is served first.

If the requirement is to serve the people in some sort of priority order, there is a separate data structure that supports priorities.

Queue Operations

The queue data structure supports the following operations.

Fig 1: Queue Operations

Queue using Linked List

Following are the key points associated with the linked list implementation.

  • Insert works in constant time for either end of a linked list.
  • Remove works in constant time only.
  • Seems best that head of the linked list be the front of the queue so that all removes will be from the front.
  • Inserts will be at the end of the list.
Fig 2: Queue implementation using Linked List

After dequeue() is called once:

Fig 3: Removal of one element from queue using dequeue()

Queue after enqueue(9) call:

Fig 4: Insertion of one element using enqueue(9)

Queue using Linked List Methods

dequeue()

int dequeue()
{
	int x = front->get();
	Node* p = front;
	front = front->getNext();
	delete p;
	return x;
}

enqueue()

void enqueue(int x)
{
	Node* newNode = new Node();
	newNode->set(x);
	newNode->setNext(NULL);
	rear->setNext(newNode);
	rear = newNode;
}

front()

int front()
{
	return front->get();
}

isEmpty()

int isEmpty()
{
	return ( front == NULL );
}

Queue using Array

If we use an array to hold the queue elements, both insertions and removal at the front (start) of the array are expensive. This is due to the fact that we may have to shift up to “n” elements.

Fig 5: Queue implemented using an Array

By calling enqueue(6) and enqueue(8):

Fig 6: Insertion of 6 and 8

Now, let’s call dequeue():

Fig 7: Removal of an element from front

dequeue() for the 2nd time:

Fig 8: Removal of another element from front

Now, by calling enqueue(9) and enqueue(12):

Fig 9: Insertion of elements in the queue

Problems

  • First: Array has attained the size limit. And we know that we’ll face the problem with its limited size.
  • Second: As we called dequeue() method, first locations of the array are empty. Which causes the useless memory consumption.

Solution

If we allow queue to wrap around, the problems can be avoided.

Circularly Array helps to achieve the purpose of wrapping around the queue.

Fig 10: Circularly array to implement queue

Queue using Array Methods

enqueue()

Perform enqueue(21):

Fig 11: An element added in Circularly array

enqueue(7):

Fig 12: Another element added in circularly array
void enqueue( int x)
{
	rear = (rear + 1) % size;   // rear will be between 0 and 7 in given case.
	array[rear] = x;
	noElements = noElements + 1; // incrementing the no. of elements.
}

isFull()

int isFull()
{
	return noElements == size;
}

isEmpty()

int isEmpty() {
	return noElements == 0;
}

dequeue()

Call the dequeue() method:

Fig 13: Element removed from the circularly array
int dequeue()
{
	int x = array[front];
	front = (front + 1) % size;
	noElements = noElements - 1;
	return x;
}

Use of Queues

Out of the numerous uses of the queues, one of the most useful is simulation. A simulation program attempts to model a real-world phenomenon. Many popular video games are simulations, e.g., SimCity, Flight Simulator etc. Each object and action in the simulation has a counterpart in the real world.

REFERENCE: CS301 Handouts (page 84 to 92)

Categorized in:

Data Structures, Learning,

Last Update: March 21, 2024