Implementation of Queue using Array
In the simple queue we can insert the element only at the rear end and delete the element from the front end.
We are going to create a menu based program .In which we have three options one for insert element second for delete element and last for display elements of queue.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
int queue[max];
int rear,front;
rear=-1;
front=-1;
// function for insert element into queue
void insert()
{
int element;
if(rear==max–1)
printf(“nqueue is overflow”);
else
{
if(front==-1)
front=0;
printf(“nEnter a value”);
scanf(“%d”,&element);
rear+=1;
queue[rear]=element;
}
}
// function for delete element from the queue
void delete()
{
int element;
if(front==-1||front>rear)
printf(“nEnter underflow condition”);
else
{
element=queue[front];
front+=1;
printf(“%d is deletedn”,element);
}
}
// function for display all the element of the queue
void display()
{
int i;
if(front==-1|| front>rear)
printf(“Underflow conditionn”);
else
{
for(i=front;i<=rear;i++)
printf(“%dn”,queue[i]);
}
}
// Driver function
void main()
{
int ch;
printf(“1. insert elementn”);
printf(“2. delete elementn”);
printf(“3. display elementn”);
printf(“4. exitn”);
while(1)
{
printf(“Enter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf(“nWrong key”);
}
}
}
Output:-
1.insert element
2. delete element
3. display element
4. exit
Enter your choice
1
Enter a value 12
Enter your choice
2
12 is deleted
Enter your choice
1
Enter a value 23
Enter your choice
3
23
Enter your choice
4