Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Data Structures Using C Satish 8886503423

QUEUES

A queue also called First In First Out (FIFO) system.


A Queue is an ordered collection of data such that the data is inserted at one
end and removed from another end.
(or)
It is a linear list in which deletions take place only at one end of the lists. The
"front " of the list, and insertion can take place only at other end of the list, the
rear of the list.

In real life, Queues from at many places such as Cafeterias, Petrol Stations,
Shopping centers.

Deletion Insertion
Front end Rear end

Rules for variables:


→ Each item information is added to the Queue, we increment Rear.
→ Each item information is taken from the Queue, we increment Front.
→ Whenever Front=Rear, the Queue is Empty.

The applications of queues are :-


1) An important application of queue is time sharing system of computer
CPU.
2) Queues are used in breadth first traversal of Q-graph.

Insert element into Queue :


If re=n then
Display “Queue is Overflow”
return
++re;
Q[r]=x;
1
Data Structures Using C Satish 8886503423

Delete element to Queue:


If fe=re then
Display “Queue is Underflow”
return
x=Q[fe];
fe=fe+1

Queue operation using structures.


#include<stdio.h>
#include<conio.h>
struct queue
{
int num[100];
int fe,re;
}s;

void insert();
void del();
void display();

void insert()
{
int x;
if(s.re==49)
{
printf("Queue Overflow......");
getch();
return;
}
printf("\nEnter the element:");
scanf("%d",&x);
s.re++;
2
Data Structures Using C Satish 8886503423

s.num[s.re]=x;
printf("%d is Pushed successfully",x);
getch();
}

void del()
{
int x;
if(s.fe==s.re)
{
printf("Queue underflow.........");
getch();
return;
}
x=s.num[s.fe+1];
++s.fe;
printf("\nDeleted element:%d",x);
getch();
}

void display()
{
int i;
printf("\nElements in FIFO order:\n");
for(i=s.fe+1;i<=s.re;i++)
printf("%d\t",s.num[i]);
getch();
}

void main()
{
int ch;
3
Data Structures Using C Satish 8886503423

s.fe=s.re=-1;
do
{
clrscr();
printf("\nQUEUE OPERATIONS\n");
printf("1.Insert an Element\n");
printf("2.Delete an Element\n");
printf("3.Display queue\n");
printf("4.Exit\n");
printf("\nEnter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid Choice\n");
getch();
}
}
while(ch!=4);
}

You might also like