CH # 4 (Queue)
CH # 4 (Queue)
Chapter # 4
THE QUEUES
By:
Muhammad Imran
Assistant Prof. of Computer Science
Govt. Millat Degree College Mumtazabad, Multan
Queue
Queue is a linear data structure in which the insertion and deletion operations are
performed at two different ends. In a queue data structure, adding and removing of
elements are performed at two different positions. The insertion is performed at one end
and deletion is performed at other end. In a queue data structure, the insertion operation is
performed at a position which is known as 'rear' and the deletion operation is performed
at a position which is known as 'front'. In queue data structure, the insertion and deletion
operations are performed based on FIFO (First In First Out) principle.
In a queue data structure, the insertion operation is performed using a function called
"enQueue()" and deletion operation is performed using a function called "deQueue()".
Queue data structure is a linear data structure in which the operations are
performed based on FIFO principle.
"Queue data structure is a collection of similar data items in which insertion and
deletion operations are performed based on FIFO principle".
The structure of Queue is the same as that of a line of a people. A person who wishes to
stand in line must go to the back (rear), and the person in front of the line is served first.
Example
Once a new element is inserted into the Queue, all the elements inserted before the
new element in the queue must be removed, to remove the new element.
peek( ) function is oftenly used to return the value of first element without
dequeuing it.
Applications of Queue
The purpose of queue is to provide some form of buffering, In computers, the Queues
are used for:
Process Management: For example in time sharing system processes are added to
a queue and are executed one after the other.
Buffer between the fast computer and a slow printer: The documents which are
added first in the queue, printed first and vice versa.
Operations on a Queue
The following operations are performed on a queue data structure...
Representation of Queues
Queue data structure can be implemented in two ways. They are as follows...
1. Using Array
When a queue is implemented using array, that queue can organize only limited number
of elements. When a queue is implemented using linked list, that queue can organize
unlimited number of elements.
{ System.out.println("OverFlow\nProgram Terminated");
System.exit(1);
}
else
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
}
if (isEmpty())
System.out.println("UnderFlow\nProgram Terminated");
System.exit(1);
else
count--;
Circular Queue
In a normal Queue Data Structure, we can insert elements until queue becomes full. But
once if queue becomes full, we cannot insert the next element until all the elements are
deleted from the queue. For example consider the queue below. After inserting all the
elements into the queue.
Now consider the following situation after deleting three elements from the
queue...
This situation also says that Queue is Full and we cannot insert the new element because,
'rear' is still at last position. In above situation, even though we have empty positions in
the queue we cannot make use of them to insert new element. This is the major problem
in normal queue data structure. To overcome this problem we use circular queue data
structure.
Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first
position to make a circle. It is also called ‘Ring Buffer’.
Two pointers called FRONT and REAR are used to keep track of the first and last
elements in the queue.
When initializing the queue, we set the value of FRONT and REAR to -1.
Enqueue an element, we circularly increase the value of REAR index and place
the new element in the position pointed to by REAR. Before enqueing, we check if
queue is already full.
When dequeing the last element, we reset the values of FRONT and REAR to -1.
System.out.println("Queue is full");
} else {
front = 0;
qArray[rear] = item;
In a circular queue, deQueue() is a function used to delete an element from the circular
queue. In a circular queue, the element is always deleted from front position. The
deQueue() function doesn't take any value as parameter. We can use the following steps
to delete an element from the circular queue...
void deQueue() {
System.out.println("Queue is empty.");
else {
rear = -1;
front = -1;
else {
Double Ended Queue can be represented in TWO ways, those are as follows...
In output restricted double ended queue, the deletion operation is performed at only one
end and insertion operation is performed at both the ends.
The following are the basic operations that can be performed on deque.
deleteFront: Delete or remove the item from the front of the queue.
delete last: Delete or remove the item from the rear of the queue.
Deque Illustration
An empty deque is represented as follows:
Next, we add element 5 to the front and when incremented the front points to 4.
Then, we insert elements 7 at the rear and 9 at the front. The deque will look as shown below.