C source code for implementation of Queue using array
/* Queue using array */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
typedef struct Q
{
int R,F;
int data[MAX];
}Q;
void initialize(Q *P);
int empty(Q *P);
int full(Q *P);
void addqueue(Q *P,int x);
int delqueue(Q *P);
void print(Q *P);
void main()
{
Q q;
int op,x;
initialize(&q);
clrscr();
do
{
printf("\n1.insert\n2.delete\n3.print\n4.quit");
printf("\nenter your choice");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nenter a value");
scanf("%d",&x);
if(!full(&q))
{
addqueue(&q,x);
else
printf("\nqueue is full");
break;
case 2:
if(!empty(&q))
{
x=delqueue(&q);
printf("\ndeleted data=%d",x);
}
else
printf("\nqueue is empty");
break;
case 3:
print(&q);
break;
}
}while(op!=4);
}
void initialize(Q *P)
{
P->R=-1;
P->F=-1;
}
int empty(Q *P)
{
if(P->R==-1)
return 1;
return 0;
}
int full(Q *P)
{
if((P->R+1)>=MAX-1)
return 1;
return 0;
}
void addqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1);
P->data[P->R]=x;
}
}
int delqueue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1);
return(x);
}
void print(Q *P)
{
int i;
if(!empty(P))
{
printf("\n");
for(i=P->F;i!=P->R;i++)
printf("%d\t",P->data[i]);
}
printf("%d\t",P->data[i]);
}
0 comments:
Post a Comment