-----------------
#include
#define Max_Stack 50
This is code for Circular Queue:
------------------
#include
#include
#include
#define Max_Queue 50
int qempty( int count) {
return (count==0?1:0);
}
int qfull (int count) {
return (count==Max_Queue?1:0);
}
void insert_rear(int item, int q[], int *r, int *count) {
if (qfull(*count)) printf ("Queue is overflow\n");
else {
*r=(*r+1) % Max_Queue;
q[*r]=item;
*count += 1;
}
}
void delete_front(int q[], int *f, int *count) {
if (qempty(*count)) printf ("Queue is underflow\n");
else {
printf("The delete element is %d \n", q[*f]);
*f=(*f+1) % Max_Queue;
*count -=1;
}
}
void display (int q[], int f, int count) {
int i, j;
if (qempty(count)) printf (" Queue is empty \n");
else {
printf(" Content of Queue is \n");
i=f;
for (j=1; j<=count; j++) {
printf ("\t%d", q[i]);
i= (i+1) % Max_Queue;
}
printf ("\n");
}
}
void main () {
int choice, item, f, r, count, q[20];
f=0; 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, &r, &count);
break;
case 2:
delete_front(q, &f, &count);
break;
case 3:
display(q,f,count);
break;
default:
exit(0);
}
}
}
No comments:
Post a Comment