queues data structures
queues data structures
Queue
1. Definition of queue
2. Graphical representation of queue
3. Applications of queue
4. Implementation of queue
5. Differences between stacks and queues
6. Basic operations
7. Program to implement queue using
a. Array
b. Linked list
DEFINITION OF QUEUE
Queue is a linear data structure, in which the first element is inserted from one end called
the REAR (also called tail), and the removal of existing element takes place from the other end
called as FRONT (also called head).
This makes queue as FIFO (First in First Out) data structure, which means that element inserted
first will be removed first.
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.
APPLICATIONS OF QUEUE
1. Real-time Examples
Waiting in a line
2. Related to computer science
Key board buffer
Job scheduling.
IMPLEMENTATION OF QUEUE
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.
#include<stdio.h>
#include<conio.h>
int Q[20],f=-1,r=-1,s;
void insert(int x)
{
if(r == s-1)
printf("Queue is full");
else
{
printf("enter the element to insert");
scanf("%d",&x);
if ( r == -1)
f = r =0;
else
r++;
Q[r]= x;
}
}
int del()
{
int x;
if( f == -1)
{
printf("Queue is empty");
return -1;
}
else
{
x = Q[f];
if ( f == r)
f = r = -1;
else
f++;
return x;
}
}
void display()
{
int i;
if(f == -1)
printf("Queue is empty");
else
{
for(i=f;i<=r;i++)
printf("%5d",Q[i]);
}
}
void main()
{
int i,x,op;
clrscr();
printf("\n Enter the size of the Queue");
scanf("%d",&s);
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n 4.Exit");
printf("\n Enter your choice");
scanf("%d",&op);
switch (op)
{
case 1 : insert(x);
break;
case 2 : x = del();
if (x != -1)
printf("\n Deleted elements is %d",x);
break;
case 3 : display();
break;
}
}
while(op != 4);
getch();
}
Circular Queue
1. Limitation of linear queue
2. Definition of circular queue
3. Graphical representation of circular queue
4. Differences between linear queue and circular queue
5. Basic operations
6. Implementation of queue
7. Program to implement queue using
a. Array
b. Linked list
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 the above situation, even though we have empty positions in the
queue we cannot make use of them to insert the new element. This is the major problem in a
linear queue. To overcome this problem we use a circular queue data structure.
DEFINITION OF CIRCULAR QUEUE
A 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.
GRAPHICAL REPRESENTATION OF CIRCULAR QUEUE
BASIC OPERATIONS
enqueue () − add (store) an item to the queue.
dequeue () − remove (access) an item from the queue.
peek () − Gets the element at the front of the queue without removing it.
isfull () − Checks if the queue is full.
isempty () − Checks if the queue is empty.
In circular queue, we always dequeue (or access) data, pointed by front pointer and while
enqueing (or storing) data in the queue we take help of rear pointer.
IMPLEMENTATION OF QUEUE
int Q[20],f=-1,r=-1,s,x;
void insert()
{
void display()
{
int i;
if(f == -1)
printf("Queue is empty");
else if (f>r)
{
for(i=f;i<s;i++)
printf("%5d",Q[i]);
for(i=0;i<=r;i++)
printf("%5d",Q[i]);
}
else
{
for(i=f;i<=r;i++)
printf("%5d",Q[i]);
}
}
void main()
{
int i,x,op;
clrscr();
printf("\n Enter the size of the Queue");
scanf("%d",&s);
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n 4.Exit");
printf("\n Enter your choice");
scanf("%d",&op);
switch (op)
{
case 1 : insert();
break;
case 2 : x = del();
if (x != -1)
printf("\n Deleted elements is %d",x);
break;
case 3 : display();
break;
}
}
while(op != 4);
getch();
Dequeue/Deque
1. Need for dequeue
2. Definition of dequeue
3. Graphical representation of dequeue
4. Differences between linear queue and dequeue
5. Basic operations
6. Implementation of dequeue
7. Program to implement queue using
a. Array
b. Linked list
NNED FOR DEQUEUE
When the user want the fast access of data items.
DEFINITION OF DEQUEUE
A double-ended queue is an abstract data type that generalizes a queue, for which elements can
be added to or removed from either the front (head) or rear (tail).
New items can be added at either the front or the rear. Likewise, existing items can be removed
from either end. This linear structure provides all the capabilities of stacks and queues in a single
data structure.
GRAPHICAL REPRESENTATION OF DEQUEUE
In real scenario we can attached it to a Ticket purchasing line, It performs like a queue but some
time It happens that some body has purchased the ticket and suddenly they come back to ask
something on front of queue. In this scenario because they have already purchased the ticket so
they have privilege to come and ask for any further query. So in these kind of scenario we need a
data structure where according to requirement we add data from front. And In same scenario user
can also leave the queue from rear.
Dequeue has 2 possible sub-types:
An input-restricted dequeue is one where deletion can be made from both ends, but insertion
can be made at one end only.
An output-restricted dequeue is one where insertion can be made at both ends, but deletion
can be made from one end only.
BASIC OPERATIONS
insertFront (): Adds an item at the front of Dequeue.
insertRear(): Adds an item at the rear of Dequeue.
deleteFront(): Deletes an item from front of Dequeue.
deleteRear(): Deletes an item from rear of Dequeue.
isEmpty(): Checks whether Dequeue is empty or not.
isFull(): Checks whether Dequeue is full or not.
In circular queue, we can perform insert, delete operations by considering both front and rear
pointers.
IMPLEMENTATION OF DEQUEUE
}
else if(f==(size-1))
{
printf("\nThe deleted element is %d", deque[f]);
f=0;
}
else
{
printf("\nThe deleted element is %d", deque[f]);
f=f+1;
}
}
}
else if(r==0)
{
printf("\nThe deleted element is %d", deque[r]);
r=size-1;
}
else
{
printf("\nThe deleted element is %d", deque[r]);
r=r-1;
}
}
int main()
{
// inserting a value from the front.
enqueue_front(2);
// inserting a value from the front.
enqueue_front(1);
// inserting a value from the rear.
enqueue_rear(3);
// inserting a value from the rear.
enqueue_rear(5);
// inserting a value from the rear.
enqueue_rear(8);
// Calling the display function to retrieve the values of deque
display();
// Retrieve the front value
getfront();
// Retrieve the rear value.
getrear();
// deleting a value from the front
dequeue_front();
//deleting a value from the rear
dequeue_rear();
// Calling the display function to retrieve the values of deque
display();
return 0;
}