Implementation of Queue Data Structure
* Queue :- Queue is a linear data structure in which elements are inserted from one end and deleted from another end.
inserting rear
11
|
22
|
33
|
1.) Queue follows a principle first in first out (FIFO). The element which came in the Queue first will be deleted first.
2.) Append :- Inserting a element into a Queue is known as Append operation.
3.) serve :- Deleting elements from a Queue is known as serve operation.
4.) underflow :- Trying to delete elements in a Queue, when the Queue is already empty. we will get Queue underflow .
5.) overflow :- when we try to insert an element into a Queue, when the Queue is already full. we will get Queue overflow.
* Applications of Queue/usage of Queue:-
1) Queue is useful in print spooling. spooling lets you place a number of print jobs in a Queue and finish by first come first serve(FCFS) basis.
2) Queue is used in CPU scheduling, Disk scheduling.
3) Queue is used in BFS(Breadth first search) in a graph.
4) In real life scenario, call center phone systems uses Queues to hold people calling them in an order , until a service representative is free.
/*C program on Queue operation... */
#include<stdio.h>
#define SIZE 5
int front=-1, rear=-1;
int q[SIZE];
void Append()
{
if(rear==SIZE-1)
{
printf("\n Queue overflow");
}
else
{
printf("Enter a number:");
scanf("%d", &q[++rear]);
}
}
void serve()
{
if(front==rear)
{
printf(" Queue underflow");
}
else
{
front++;
printf("Element served is %d", q[front]);
}
}
void show()
{
int i;
if(rear==front)
{
printf("Queue is empty:\n");
}
else
{
for(i=front+1; i<=rear; i++)
printf("%d", q[i]);
}
}
int main()
{
int ch;
do
{
printf("::Queue operation::\n");
printf("1.Append 2.serve 3.show 4.exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: Append(); break;
case 2: serve(); break;
case 3: show(); break;
default: printf("Invalid choice");
}
}while(ch!=4);
return 0;
}
//INPUT/OUTPUT:-
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:88
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:66
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:78
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:36
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:85
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Queue overflow::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:3
8866783685::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 88::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 66::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 78::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 36::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 85::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Queue underflow::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:3
Queue is empty:
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:4
Invalid choice..
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:88
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:66
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:78
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:36
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Enter a number:85
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:1
Queue overflow::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:3
8866783685::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 88::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 66::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 78::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 36::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Element served is 85::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:2
Queue underflow::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:3
Queue is empty:
::Queue operation::
1.Append 2.serve 3.show 4.exit
Enter your choice:4
Invalid choice..