You are on page 1of 25

VKS-LEARNING HUB

QUEUE

VKS-LEARNING HUB

Queue is a linear data structure in which the insertion and deletion


operations are performed at two different ends. 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 onFIFO (First In First Out)principle.
R

In a queue data structure, the insertion operation from rear end is performed using a
function called "enQueue()
deletion operation is performed from front end using a function called "deQueue()".

VKS-LEARNING HUB

Queue data structure can be implemented in two


ways. They are as follows...
Using Array
Using Linked List
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.

VKS-LEARNING HUB

Queue Using Array


A queue data structure can be implemented using one dimensional
array. But, queue implemented using array can store only fixed
number of data values.

The implementation of queue data structure using array is very


simple, just define a one dimensional array of int Or structure of
specific size and insert or delete the values into that array by
using FIFO (First In First Out) principle with the help of variable
'front' and 'rear'.

Initially both front and 'rear' are set to -1.

Whenever, we want to insert a new value into the queue, increment


'rear' value by one and then insert at that position.

Whenever we want to delete a value from the queue, then increment


'front' value by one and then display the value at 'front' position as
deleted element.

VKS-LEARNING HUB
Steps to create an empty queue.
Step 1: Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2: Declare a class Queue with private : one dimensional array with above
defined SIZE (int queue[SIZE]) & two other data member 'front' and 'rear' and
public : functions which are used in queue implementation. A constructor to
initialize both 'front' and 'rear' with '-1'. (int front = -1, rear = -1)
Step 4: Then implement main method by displaying menu of operations list and
make suitable function calls to perform operation selected by the user on queue.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void enQueue();
void deQueue();
void display();

VKS-LEARNING HUB
enQueue(value) is a function used to insert a new element into the queue.
In a queue, the new element is always inserted at rear position. The enQueue() function
takes one integer value as parameter and inserts that value into the queue.
Step 1: Check whether queue is FULL. (rear == SIZE-1)
Step 2: If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
Step 3: If it is NOT FULL, then increment rear value by one (rear++) and
set queue[rear] = value.
void enQueue()
{
int add_item;
if (rear == MAX - 1)
cout<<"Queue Overflow \n";
else {
if (front == - 1) /*If queue is initially empty */
front = 0;
cout<<"\nInset the element in queue : "; cin>>add_item;
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/

VKS-LEARNING HUB
deQueue() - is a function used to delete an element from the queue
In a queue, the element is always deleted from front position. The deQueue() function
does not take any value as parameter.
Step 1: Check whether queue is EMPTY. (front == rear)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 3: If it is NOT EMPTY, then increment the front value by one (front ++). Then
display queue[front] as deleted element. Then check whether both front and rear are
equal (front == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
void del()
{
if (front == - 1 || front > rear) {
cout<<"\nQueue Underflow \n";
return ;
}
else {
cout<<"\n\nElement deleted= :"<<queue_array[front]<<endl;
front = front + 1;
}
} /*End of delete() */

VKS-LEARNING HUB
display() - Displays the elements of a Queue
We can use the following steps to display the elements of a queue...
Step 1: Check whether queue is EMPTY. (front == rear)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 4: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the
same until 'i' value is equal to rear (i <= rear)
void display()
{
int i;
if (front == - 1)
cout<<"\nQueue is empty \n";
else
{
cout<<"\nQueue is : \n\n";
for (i = front; i <= rear; i++)
cout<<queue_array[i]<<endl;
cout<<"\n";
}
} /*End of display() */

VKS-LEARNING HUB
void main()
{
int choice;
do {
cout<<"\n *********MENU******************\n";
cout<<"\n1.Insert element to queue \n";
cout<<"\n2.Delete element from queue \n";
cout<<"\n3.Display all elements of queue \n";
cout<<"\n4.Quit \n";
cout<<"\nEnter your choice : "; cin>>choice;
switch (choice) {
case 1:
enQueue(); break;
case 2:
deQueue(); break;
case 3:
display(); break;
case 4:
exit(1);
default:
cout<<"Wrong choice \n";
} /*End of switch*/
}while (choice!=4); /*End of while*/
getch();
} /*End of main()*/

Array Queue
Implementation
Stack[6]//size=6
Add(6);

front = -1
0
rear =

front

-1
0

rear

If rear=front=-1 then front=0;


rear=rear+1; stack[rear]=value

Array Queue Example


front =

rear =

0
3
1
2
4

rear

6
front

rear=
rear= (rear=rear+1)//rear++
(rear=rear+1)//rear++
rear=(1+1)=2
rear=(0+1)=1
rear=(3+1)=4
rear=(2+1)=3
insert
insert item
item at
at this
this position
position

Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)

Array Queue Example


front =

1
2
0

rear

4
5

make front = (0 + 1) = 1
make front = (1 + 1) = 2
rear= (rear=rear+1)//rear++
rear=(4+1)=5
insert item at this position

Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
del();//front = 1
del();//front = 2
Add(9);

Array Queue Example


front =

count =

5
4

rear==Size-1 stack full!! No


more addition as per the
queue concept even
though there are two space
empty

Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
del();//front = 1
del();//front = 2
Add(9);
Add(5);

Circular Arrays
use a circular array to insert and remove
items from a queue The idea of a circular
array is that the end of the array wraps
around to the start of the array
7

5
4

The mod Operator


The mod operator (%) is used to calculate
remainders:
1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3

mod can be used to calculate the front and


rear positions in a circular array, therefore
avoiding comparisons to the array size
The rear of the queue is:

(front + count) % size


where count is the number of items
currently in the queue
After removing an item the front of the queue
is:
(front + 1) % size;

Array Queue Example


front =

count =

1
0

Stack[6]//size=6
Add(6);

6
0

rear= (front + count) % size


insert item at this position
Rear=(0+0)%6=0

Array Queue Example


front =

count =

1
2
4
3
5

rear= (front + count) % size


rear=(0+1)%6=1
rear=(0+2)%6=2
rear=(0+3)%6=3
rear=(0+4)%6=4
insert item at this position

Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)

Array Queue Example


front =

1
2
0

count =

3
4
5

make front = (0 + 1) % 6 = 1
make front = (1 + 1) % 6 = 2

Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
del();//front = 1
del();//front = 2
Add(9);

Array Queue Example


front =

count =

5
4

5
0

insert at (front + count) % 6


= (2 + 4) % 6 = 0

Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
del();//front = 1
del();//front = 2
Add(9);
Add(5);

Queue: Linked List


Implementation
Removing items from the front of the queue is
straightforward
But we need to insert items at the back of the
queue in constant time
So cannot traverse through the list
By using an additional reference (and a little
administration) we can keep track of the node at
the back of the queue

List Queue Example


6
front
back

//Java Code
Queue q = new Queue();
q.enqueue(6);

List Queue Example


6
front
back

//Java Code
Queue q = new Queue();
q.enqueue(6);
q.enqueue(4);

List Queue Example


6
front
back

4
7

//Java Code
Queue q = new Queue();
q.enqueue(6);
q.enqueue(4);
q.enqueue(7);

List Queue Example


6
front
back

4
7
3

//Java Code
Queue q = new Queue();
q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);

List Queue Example


6
front
back

4
7
3

//C++ Code
Queue q = new Queue();
q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);
q.dequeue();