2 Introduction To Queue
2 Introduction To Queue
0 1 2 3 4 5
10 20 30 40
Queue
DEEPAK SHARMA
Introduction To Queue
Add: We add a new element in Queue
at the rear.
Front Rear
0 1 2 3 4
10 20 30 40 Before Add
Front Rear
0 1 2 3 4
10 20 30 40 50 After Add
DEEPAK SHARMA
Introduction To Queue
Delete: We add an element from the
front of the Queue .
Front Rear
0 1 2 3 4
Before Delete
10 20 30 40
Front Rear
0 1 2 3 4
20 30 40 After Delete
DEEPAK SHARMA
Introduction To Queue
Empty Queue: If front and rear of the
Queue is -1, it means Queue is empty.
Front
-1 0 1 2 3 4
Rear
Empty Queue
DEEPAK SHARMA
Introduction To Queue
Full Queue: If rear of the Queue is at
the end , it means Queue is Full.
Front Rear
0 1 2 3 4
10 20 30 40
Queue is full
DEEPAK SHARMA
Introduction To Queue
WAP of Queue
#include<stdio.h>
#define size 100
#include<stdlib.h>
struct queue
{
int front;
int item[size];
int rear;
};
main()
{
int c=0,a,b;
struct queue *q;
q=NULL;
Cont…
Introduction To Queue
(*q).front=0;
(*q).rear=-1;
while(c<5)
{
clrscr();
fflush(stdin);
printf("\nYou have following choice");
printf("\n1. Add");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit");
Cont…
Introduction To Queue
switch(c)
{
case 1:
printf("\nEnter the no");
scanf("%d",&a);
(*q).rear++;
insert(&q,a);
break;
case 2:
b=delete(&q);
printf("\nelement = %d",b);
(*q).front++;
break;
Cont…
Introduction To Queue
case 3:
list(&q);
break;
case 4:
exit(0);
}
}
}
Cont…
Introduction To Queue
insert(struct queue **pq, int n)
{
if((*pq)->rear>=size)
{
printf("queue is full");
getch();
return;
}
else
{
(*pq)->item[(*pq)->rear]=n;
return;
}
}
Cont…
Introduction To Queue
delete(struct queue **pq)
{
if((*pq)->rear<(*pq)->front)
{
printf("\Queue is empty");
getch();
return;
}
else
{
return((*pq)->item[(*pq)->front]);
}
}
Cont…
Introduction To Queue
list(struct queue **pq)
{
int i, b, a;
a=(*pq)->front;
b=(*pq)->rear;
for(i=a;i<=b;i++)
{
printf("%d\t",(*pq)->item[i]);
}
getch();
return;
}
Cont…
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-
Introduction To Queue
Output :-