0% found this document useful (0 votes)
23 views

2 Introduction To Queue

Queue is a data structure that follows FIFO (First In First Out) principles where elements are added to the rear and deleted from the front. Elements can be added using an insert function by incrementing the rear pointer, and deleted using a delete function by incrementing the front pointer. A queue is empty when the front and rear pointers are -1, and full when the rear pointer reaches the end of the allocated memory size. Sample C code is provided to demonstrate adding, deleting, and displaying elements in a queue.

Uploaded by

bhawna.kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

2 Introduction To Queue

Queue is a data structure that follows FIFO (First In First Out) principles where elements are added to the rear and deleted from the front. Elements can be added using an insert function by incrementing the rear pointer, and deleted using a delete function by incrementing the front pointer. A queue is empty when the front and rear pointers are -1, and full when the rear pointer reaches the end of the allocated memory size. Sample C code is provided to demonstrate adding, deleting, and displaying elements in a queue.

Uploaded by

bhawna.kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction To Queue

Queue: Queue is a data structure where


elements add at end, and delete from
the front. Queue follows FIFO(First in
First Out) concept.
Front Rear

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");

printf("\nEnter your choice");


scanf("%d",&c);

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 :-

You might also like