Saturday, November 1, 2008

DS lab program 6 (Queue operation)

#include"stdio.h"
#include"conio.h"

#define MAX 5
#define TRUE 1
#define FALSE 0

struct Queue
{
int front;
int rear;
int items[MAX];
}q;

void insert(struct Queue *q, int );
void display(struct Queue *q);
int remov(struct Queue *q);
int empty(struct Queue *q);

main()
{
int i,n;
int choice,x;
q.rear=-1;
q.front=0;
clrscr();

while(1)
{
printf("\n1.INSERT\n2.DISPLAY\n3.REMOVE\n");
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i< n;i++)
{
scanf("%d",&x);
insert(&q,x);
}
printf(" the queue items are:\n");
display(&q);
break;

case 2:
display(&q);
break;

case 3:
x=remov(&q);
printf("\n the value of x=%d",x);
break;



default:
printf("\n Invalid Entry");
getch();
exit(1);
}
}
getch();
}

void insert(struct Queue *q,int y)
{
if(q->rear==MAX-1)
{
printf("\n Queue overflow");
getch();
exit(1);
}
else
q->items[++q->rear]=y;
}

int remov(struct Queue *q)
{
int x;
if(empty(q))
{
printf("\n queue underflow");
getch();
exit(1);
}
else
x=q->items[q->front++];
return(x);
}

int empty(struct Queue *q)
{
if(q->rear<>front)
return(TRUE);
else
return(FALSE);
}

void display(struct Queue *q)
{
int i,x;
for(i=q->front;i<= q->rear;i++)
{
x=q->items[i];
printf("\n %d",x);
}
}

No comments:

Post a Comment