IMPLEMENTATION OF QUEUE BY ARRAY : C PROGRAM
Here is the C program for implementation of queue using array.
Queue is a data structure.
program code :
#include<stdio.h>
#include<stdlib.h>
int que[6],f=-1;r=-1;
void enque(int d)
{
if(f==5)
{
printf("queue is full,enque not possible.\n");
return;
}
if(f==-1)
{
f=r=0;
que[r]=d;
}
else{
r++;
que[r]=d;
}
}
void deque()
{
if(f==-1){
printf("list is empty,deletion not possible\n");
return;
}
if(f==r){
printf("deleted element is %d\n",que[r]);
f=r=-1;
}
else{
printf("deleted element is %d\n.",que[f]);
f++;
return;
}
}
void display()
{
int i;
if(f==-1){
printf("queue is empty.\n");
return;
}
printf("queue elements are : ");
for(i=f;i<=r;i++)
printf("%d ",que[i]);
printf("\n");
}
int main()
{
int op,d,exit=0;
do
{
printf("1.enqueue 2.dequeue 3.display 4.exit\n");
printf("enter your choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("enter data : ");
scanf("%d",&d);
enque(d);
break;
case 2:
deque();
break;
case 3:
display();
break;
case 4:
exit=1;
break;
default:
printf("enter valid key\n");
break;
}
}while(exit==0);
}
output:
Happy coding,
Comment what you need 🙂.
Comments
Post a Comment