Sunday, October 2, 2011

Data Structure C program Queue using array Dev c++

 #include <stdio.h>  
#define MAX 20
struct queue{
int x[MAX];
int front;
int rear;
}q;
void enqueue(int);
void display();
int dequeue();
int main()
{
q.front=-1;
q.rear=-1;
int ch,x,flag=1;
while(flag)
{
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter element:");
scanf("%d",&x);
enqueue(x);
break;
case 2:x=dequeue();
printf("Removed %d\n",x);
break;
case 3:display();
break;
case 4:flag=0;
break;
defalt:printf("Wrong choice\n");
}
}
}
void enqueue(int x)
{
if(q.rear==MAX)
printf("Queue full\n");
else
{
q.x[++q.rear]=x;
if(q.front==-1)
q.front=0;
}
}
int dequeue()
{
if(q.rear==-1 && q.front==-1)
printf("Queue Empty\n");
else
return q.x[q.front++];
}
void display()
{
printf("Queue is\n");
int i;
for(i=q.front;i<=q.rear;i++)
printf("%d\n",q.x[i]);
}

No comments:

Post a Comment