Header Ads Widget

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

#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 deleted\n",element);
}
}

// function for display all the element of the queue

void display()
{
int i;
if(front==-1|| front>rear)
printf("Underflow condition\n");
else
{
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}

// Driver function

void main()
{
int ch;
printf("1. insert element\n");
printf("2. delete element\n");
printf("3. display element\n");
printf("4. exit\n");
while(1)
{
printf("Enter your choice\n");
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

Post a Comment

0 Comments