Tweet
/* Program to implement queue using array */
#include<stdio.h>
void display(int a[],int n,int *front,int *rear)
{
int i;
if(*front==-1)
{
printf("\nQueue underflow !!! ");
return;
}
else if(*front>=n)
{
printf("\nQueue underflow !!! ");
return;
}
else
{
printf("\nQueue : ");
for(i=*front;i<=*rear;i++)
printf(" %d ",a[i]);
}
printf("\n");
}
void insert(int a[],int n,int *front,int *rear)
{
int x;
printf("\n Input element : ");
scanf("%d",&x);
if((*front==-1)&&(*rear==-1))
{
(*rear)++;
a[++(*front)]=x;
}
else if((*rear<n-1)&&(*front>-1))
{
a[++(*rear)]=x;
}
else
{
printf("\nQueue overflow !!! ");
return;
}
}
void delete(int a[],int n,int *front,int *rear)
{
int x;
if(*front==-1)
{
printf("\nQueue underflow !!! ");
return;
}
else if(*front> *rear)
{
printf("\nQueue underflow !!! ");
return;
}
else
{
x=a[*front];
(*front)++;
printf("\nDeleted element is : %d\n",x);
}
}
main()
{
int front=-1,rear=-1;
printf("\nProgram to implement queue using array : \n");
int n;
printf("\nEnter array capacity : ");
scanf("%d",&n);
int a[n];
int ch;
do
{
printf("\n[1] Insert [2] Delete [3] Display [4] Exit : \n");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert(a,n,&front,&rear);
display(a,n,&front,&rear);
break;
case 2:
delete(a,n,&front,&rear);
display(a,n,&front,&rear);
break;
case 3:
display(a,n,&front,&rear);
break;
case 4:
break;
default : printf("\nEnter correct choice : \n");
break;
}
}while(ch!=4);
}
Queue using array in C programming
Posted by
LAHAUL SETH
~
Queue using array in C programming
2011-11-29T10:55:00+05:30
LAHAUL SETH
Programming in C
|
Queue Implementation
|
Comments
Queue using array in C programming
2011-11-29T10:55:00+05:30
LAHAUL SETH
Programming in C
|
Queue Implementation
|