Data Structure - Lec6
Data Structure - Lec6
queue Implementation
Circular queue
queue Application
rear
front
Queue
5
Introduction to queue
Queue is a linear data structure where the first element is inserted from
one end called REAR and deleted from the other end called as FRONT.
Front points to the beginning of the queue and Rear points to the end of
the queue.
According to its FIFO structure, element inserted first will also be removed
first.
In a queue, one end is always used to insert data (enqueue) and the other is
used to delete data (dequeue), because queue is open at both its ends.
queue operations
Basic operations: enqueue and dequeue
enqueue
insert an element at the end of the list(called the
rear)
dequeue
Delete and return the element at the start of the
list(known as the front)
enqueue dequeue
Queue
front
rear
queue operations
Some functionality is added to queue to
Check the status of queue
C
B A
rear Front
C B A
queue operations: dequeue
dequeue operation : accessing the content while removing it from the
queue. Dequeue operation may involve the following steps :
Step 1 − Checks if the queue is empty.
Step 2 − If the queue is empty, produces an error and exit.
Step 3 − If the queue is not empty, accesses the data element at
which front is pointing.
Step 4 − Increment front pointer to point to the next available data
element..
rear Front
Step 5 − Returns success.
C B A
rear Front
dequeue
C B A
Array-based queue Implementation
Array is the easiest way to implement a queue. Queue
can be also implemented using Linked List
0 1 2 3 4 5 6
A B C 55555
D E F G
front rear
Array-based queue Implementation
rear rear=front=-1
0 1 2 3 4 5
A 55555
void enqueue(char item)
{
if (is_full())
front {
rear cout<< "queue is overflow";
return;
0 1 2 3 4 5 }
55555 else{/*If queue is initially empty*/
A B
if(front==–1)
front=0;
front
rear
rear++;
0 1 2 3 4 5 queue_items[rear]=new_item;
}
A B C55555
}
front
Array-based queue Implementation
rear
int queue::dequeue()
0 1 2 3 4 5
A {
B C55555 if (is_empty())
{
cout<< "queue is underflow";
front exit(0) ;
rear
}
else if (front==rear)
0 1 2 3 4 5 {
A B C55555 item=queue_items[front];
front=-1;rear=-1;
}
front
else {item=queue_items[front];
front++; }
return item;
0 1 2 3 4 5 }
A B C 55555
rear=front=-1
Array-based queue Implementation
#include <iostream>
#define max_size 100
using namespace std;
class queue
{
private:
int queue_items[max_size];
public:
queue(){front=-1,rear=-1;}
void enqueue(int x);
int dequeue();
int is_empty();
int is_full();
void print_all_elements();
~stack();
};
Array-based queue Implementation
//Is_empty
int queue::is_empty()
{
if((front== -1&&rear== -1))
return 1;
return 0;
}
Array-based queue Implementation
//Is_fullFunction
int queue::is_full()
{
if (rear == max_size-1)
return 1;
return 0;
}
Array-based queue Implementation
// enqueue Function
void queue::enqueue(int new_item)
{
if (is_full())
{
cout<< "queue is overflow";
return;
}
else{
if(front==–1) /*If queue is initially empty*/
front=0;
rear++;
queue_items[rear]=new_item;
}
}
Array-based queue Implementation
// enqueue Function
int queue::dequeue()
{ int item;
if (is_empty())
{
cout<< "queue is underflow";
exit(0) ;
}
else if (front==rear)
{
item=queue_items[front];
front=-1;rear=-1;
}
else {item=queue_items[front];
front++; }
return item;
Array-based queue Implementation
//print all element in the queue
void queue::print_all_elements()
{
if(is_empty())
{
cout<<"\n Queue is empty"<<endl; return;
}
cout<<"the current items of the queue are:" <<endl;
for(int i=front; i<=rear; i++)
cout<<queue_items[i]<<" ";
cout<<endl;
}
Array-based queue Implementation
// File: Main_program.cpp
// Run of the Main Program
#include <iostream>
using namespace std;
void main()
{
queue q;
q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4);
q. print_all_elements();
int val=q.dequeue(); val=q.dequeue();
q. print_all_elements();
q. enqueue(5); q. enqueue(6); q.print_all_elements();
}
Circular queue
The indexes 0 and 1 can only be used after the queue is
reset when all the elements have been dequeued.
Circular queue avoids the wastage of space in a regular
queue implementation using arrays
Circular Queue works by the process of circular increment
i.e. when we try to increment any variable and we reach the
end of queue, we start from the beginning of queue by
modulo division with the queue size.
i.e. if rear + 1 == 6 (overflow!), rear = (rear + 1)%6= 0 (start of
queue 2 front
1
0 1 2 3 4 5 10
10 55555 20 30 40 20 3
0
40 30
rear rear 5 4
front
Circular queue Implementation
// enqueue Function
void queue::enqueue(int new_item)
{
if((rear+1)%max_size==front) return;
else{
if(front==–1) /*If queue is initially empty*/
front=0;
rear=(rear+1)%max_size;
queue_items[rear]=new_item;
}
}
Circular queue Implementation
// enqueue Function
int queue::dequeue()
{ int item;
if (is_empty())
{
cout<< "queue is underflow";
exit(0) ;
}
else if (front==rear)
{
item=queue_items[front];
front=-1;rear=-1;
}
else { item=queue_items[front];
front=(front+1)%max_size; }
return item;
}
Applications OF Queue
Printer server routines (in drivers) are designed using
queues.
All types of customer service software (like
Railway/Air ticket reservation) are designed using
queue to give proper service to the customers.
Round robin techniques for processor scheduling is
implemented using queue.