Unit 2c Queues
Unit 2c Queues
QUEUE
A Queue is a data structure in which elements are
added at one end (called the rear), and elements
are removed from the other end (called the front).
Front Rear
End End
For Inseting
if(queue_not_full)
Rear++
Queue[Rear] = val 100 200 300 400 500
endif
Front Front
Rear Front
Rear Front
Rear Rear
Front Front
Rear Rear Front
For Deleting
Note : After Deletion
if (queue_not_Empty)
Front++ If Front > Rear
endif Front = 0; Rear = -1;
endif
Types of QUEUE
Normal Queue
Circular Queue
Double Ended Queue
Priority Queue
printf("\t\t\t Enter the choice : ");
Ordinary Queue scanf("%d", &choice);
#include <stdio.h> switch (choice)
#include <process.h> {
#include <conio.h> case 1: // push into the queue
#define QUEUE_SIZE 5 printf("Enter the item to be inserted :
scanf("%d", &item);
insert_rear(item, q, &r);
void main()
continue;
{
case 2: // pop from the queue
void insert_rear(int, int *, int *);
delete_front(q, &f, &r);
void delete_front(int *, int *, int *); break;
void display(int *, int, int); case 3: // display queue
int choice, item, f, r, q[10]; display(q, f, r);
/* Queue is empty */ break;
f = 0; /* Front end of queue*/ case 4:
r = -1; /* Rear end of queue*/ exit(0);
for (;;) default:
{ printf("\t\t\tInvalid Input – Try Again");
clrscr(); } // end of switch
printf("\t\t\t Ordinary Queue operation\n\n"); getch();
printf("1. Push 2 Pop 3 View 4 Exit \n"); }// end of for
} // end of main
void insert_rear(int item, int q[], int *r)
{
if (qfull(*r)) /* Is queue full ? */
{
printf("\t\t\tQueue overflow\n");
return;
}
/* Queue is not full */
q[++(*r)] = item; /* Update rear pointer and insert a item */
}
void delete_front(int q[], int *f, int *r)
{
if (qempty(*f, *r))
{
printf("\t\t\tQueue underflow\n");
return;
}
printf(" Pop Successfull, element deleted = %d ",q[(*f)++]);
if(*f> *r)
{
*f=0,*r=-1;
}
}
void display(int q[], int f, int r)
{
int i;
if (qempty(f,r))
{
printf("Queue is empty\n");
return;
}
printf("\t\t\t Queue Container\n\n");
for(i=f;i<=r; i++)
printf("\t\t\t| %5d |\n",q[i]);
}
int qempty(int f, int r)
{
return (f>r)?1:0;
/* returns true if queue is empty otherwise returns false */
}
int qfull( int r)
{
return (f== QUEUE_SIZE)?1:0;
/* returns true if queue is fullotherwise returns false */
}
Circular Queue
50 40
4 3
60
5 2 30
6 1 100
70 20
7 0
80 10
90
Circular Queues
#define SIZE 5
int CQ[SIZE], f=-1, r=-1;
int CQdelete()
{
int elem; /* Function to display status of Circular
if(CQempty()) Queue */
{
printf("\n\nUnderflow!!!!\n\n"); display()
return(-1); {
} int i;
else if(CQempty())
{ printf(" \n Empty Queue\n");
elem=CQ[f]; else
if(f==r) {
{ printf("Front[%d]->",f);
f=-1; r=-1; for(i=f; i !=r; i=(i+1)%SIZE)
} /* Q has only one element ? */ printf("%d ",CQ[i]);
else printf("%d ",CQ[i]);
f=(f+1) % SIZE; printf("<-[%d]Rear",r);
return(elem); }
} }
}
main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("\n ### Circular Queue Operations ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
CQinsert(elem); break;
case 2: elem=CQdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Circular Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n"); break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
Double Ended Queue
Front Rear
10 5 8 2 1 20 10
2 1
8
5 20
2 types Insertion
• Ascending P. Q.
• Descending P.Q.
Circular Queues
#include<stdio.h>
#define max 3
int q[10],front=0,rear=-1; while(1)
void main() {
{ printf("Enter your choice:");
int ch; scanf("%d",&ch);
void insert(); switch(ch)
void delet(); {
void display(); case 1: insert();
clrscr(); break;
printf("\nCircular Queue operations\n"); case 2: delet();
break;
printf("1.insert\n2.delete\n3.display\n4.exit\n"); case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch(); else
exit(); {
} for(i=front;i<=rear;i++)
if(front>rear) {
{ printf("\t%d",q[i]);
for(i=0;i<=rear;i++) }
printf("\t%d",q[i]); printf("\nrear is at %d\n",q[rear]);
for(j=front;j<=max-1;j++) printf("\nfront is at %d\n",q[front]);
printf("\t%d",q[j]); }
printf("\nrear is at %d\n",q[rear]); printf("\n");
printf("\nfront is at %d\n",q[front]); }
} getch();
Multiple Stacks
When a stack is created using single array, we can not able to
store large amount of data, thus this problem is rectified using
more than one stack in the same array of sufficient array. This
technique is called as Multiple Stack.