Friday, October 17, 2008

Code for circular queue in structure type

#include
#include
#include
#define Max_Queue 50
typedef struct Queue {
int r, f;
int data[Max_Queue];
}q;
int qempty( int count) {
return (count==0?1:0);
}

int qfull (int count) {
return (count==Max_Queue?1:0);
}

void insert_rear(int item, Queue *q, int *count) {
if (qfull(*count)) printf ("Queue is overflow\n");
else {
q->r=(q->r+1) % Max_Queue;
q->data[q->r]=item;
*count += 1;
}
}

void delete_front(Queue *q, int *count) {
if (qempty(*count)) printf ("Queue is underflow\n");
else {
printf("The delete element is %d \n", q->data[q->f]);
q->f=(q->f+1) % Max_Queue;
*count -=1;
}
}

void display (Queue *q, int count) {
int i, j;
if (qempty(count)) printf (" Queue is empty \n");
else {
printf(" Content of Queue is \n");
i=q->f;
for (j=1; j<=count; j++) {
printf ("\t%d", q->data[i]);
i= (i+1) % Max_Queue;
}
printf ("\n");
}
}

void main () {
Queue *q;
int choice, item, count;
q->f=0; q->r=-1; count=0;

for(;;) {
printf(" 1: Insert 2: Delete\n");
printf(" 3: Display 4: Exit \n");
printf(" Enter the choice: ");
scanf(" %d", &choice);
switch(choice) {
case 1:
printf (" Enter the item to be inserted: ");
scanf (" %d", &item);
insert_rear(item, q, &count);
break;
case 2:
delete_front(q, &count);
break;
case 3:
display(q,count);
break;
default:
exit(0);
}
}
}

1 comment:

Anonymous said...

Thank! Teacher...