/*Circular queue operations using counter,
Circular queue programs, Insert in circular queue,delete in circular queue*/
#include <stdio.h>
#define max 5
struct queue {
int items[max];
int front, rear, counter;
};
void display(struct queue *pq) {
int i;
if (pq->counter == 0) {
printf("Queue underflow!!\n");
} else {
printf("The queue items are:\n");
for (i = 0; i < pq->counter; i++) {
printf("%5d", pq->items[(i + pq->front + 1) % max]);
}
}
}
void insrt(struct queue *pq) {
int val;
if (pq->counter == max) {
printf("Queue overflow!\n");
} else {
printf("Enter the data to insert: ");
scanf("%d", &val);
pq->rear = (pq->rear + 1) % max;
pq->items[pq->rear] = val;
pq->counter++;
}
}
void delt(struct queue *pq) {
int x;
if (pq->counter == 0) {
printf("Queue underflow!\n");
} else {
pq->front = (pq->front + 1) % max;
x = pq->items[pq->front];
printf("The deleted data of queue is: %d\n", x);
pq->counter--;
}
}
void main() {
struct queue *q;
int x;
q->front = max - 1;
q->rear = max - 1;
q->counter = 0;
while (1) {
printf("\nMENU");
printf("\n1.Enqueue\n2.Dequeue\n3.Display all items\n4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &x);
switch (x) {
case 1:
insrt(q);
break;
case 2:
delt(q);
break;
case 3:
display(q);
break;
case 4:
exit(1);
default:
printf("\nInvalid Choice!\n");
}
}
}
0 comments:
Post a Comment