Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Queue

• Queue is an abstract data structure, somewhat similar


to Stacks.
• Unlike stacks, a queue is open at both its ends.
• One end is always used to insert data (enqueue) and
the other is used to remove data (dequeue).
• Queue follows First-In-First-Out methodology, i.e., the
data item stored first will be accessed first.
• A real-world example of queue can be a single-lane
one-way road, where the vehicle enters first, exits first.
More real-world examples can be seen as queues at
the ticket windows and bus-stops.
• A queue is logically a First in first out(FIFO)
type of list.
• A queue is a non-primitive linear data
structure. It is an homogeneous collection of
elements in which new elements are added at
one end called the Rear end, and the existing
elements are deleted from other end called
the front end.
The following figures show queue graphically during insertion operation:
figure 1
• It is clear from the previous figure 1 that
whenever we insert an element in the queue,
the value of Rear is incremented by one i.e,
Rear=Rear+1
• Note that during the insertion of the first
element in the queue we always increment
the Front by one,
Front=Front+1
• Afterwards the Front will not be changed
during the entire addition operation.
The following figure shows queue graphically during deletion operation.
Figure 2
Now if we insert two elements in the queue, the queue will look like:
figure 3
• Note: This is clear from the figure 3 that
whenever an element is removed or deleted
from the queue the value of Front is
incremented by one i.e
Front=Front+1
Basic operations associated with queues −

• enqueue() − add (store) an item to the queue.


• dequeue() − remove (access) an item from the
queue.
• As in stacks, a queue can also be implemented
using Arrays, Linked-lists, Pointers and
Structures.
Enqueue Operation

• Queues maintain two data pointers, front and rear.


Therefore, its operations are comparatively difficult to
implement than that of stacks.
• The following steps should be taken to enqueue (insert)
data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and
exit.
• Step 3 − If the queue is not full, increment rear pointer to
point the next empty space.
• Step 4 − Add data element to the queue location, where
the rear is pointing.
• Step 5 − return success.
Algorithm for enqueue operation

procedure enqueue(data)
if queue is full
 return overflow
 endif
 rear ← rear + 1
queue[rear] ← data
 return true
end procedure
Dequeue Operation

• Accessing data from the queue is a process of two tasks


− access the data where front is pointing and remove
the data after access. The following steps are taken to
perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow
error and exit.
• Step 3 − If the queue is not empty, access the data
where front is pointing.
• Step 4 − Increment front pointer to point to the next
available data element.
• Step 5 − Return success.
Algorithm of Dequeue operation
procedure dequeue
 if queue is empty
 return underflow
end if
 data = queue[front]
front ← front + 1
return true
end procedure
Implementation of Queue using Array
#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert()
{ int val;
if (rear == n - 1) -> pointing to last index
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}}
void Delete() {
if (front == - 1 && rear==-1) {
cout<<"Queue Underflow ";
return ;
}
else {
cout<<"Element deleted from queue is : "<< queue[front]
<<endl;
front++;;
}}
void Display()
{ if (front == - 1)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}}
int main()
{ int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element fromqueue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
} }
while(ch!=4);
return 0; }
output
1) Insert element to queue
2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice Enter your choice : 4
Exit
Limitation of linear queue
• Now if we attempt to add more
elements, the elements can’t be
inserted, because in a queue the new
Lets look at this array: elements are always added from the
rear end and rear here indicates to last
location of the array. Hence though the
space is there in the queue we are not
able to insert the elements. To remove
this problem, the first and an obvious
solution which comes into our mind is
whenever an element is deleted shift
all afterward elements to the left by
one position.
• But if the queue is too large of say
5000 elements it will be a difficult job
to do later. Also note that now Front=5,
this takes Front pointer out. To take this
pointer again pointing to the right
position in the queue we always
reset(or check) both the pointers Front
and Rear, this step is highlighted in the
deletion algorithm given above.

You might also like