WHAT'S NEW?
Loading...

Circular Queue Operations by Sacrificing the last cell


/*circular queue operations performed by sacrificing the last cell*/
#include <stdio>
#define max 50

struct queue {
    int items[max];
    int front,rear;
};

void display(struct queue *pq) {
    int i=0;
    if(pq->front==pq->rear) {
        printf("Queue underflow!!\n");
        return;
    }
    printf("The queue items are:\n");
    for(i=(pq->front+1+i)%max;i!=(pq->rear+1);i++)
        printf("%5d",pq->items[i]);
}

void insrt(struct queue *pq) {
    int val;
    if(pq->front==(pq->rear+1)%max) {
        printf("Queue overflow!\n");
        return;
    }
    printf("Enter the data to insert: ");
    scanf("%d",&val);
    pq->rear=(pq->rear+1)%max;
    pq->items[pq->rear]=val;
}

void del(struct queue *pq) {
    if(pq->rear==pq->front) {
        printf("Queue underflow!\n");
        return;
    }
    pq->front=(pq->front+1)%max;
    printf("The deleted data of queue is: %d\n",pq->items[pq->front]);
}

void main() {
    struct queue *q;
    int x;
    q->front=q->rear=max-1;
    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:
                del(q);
                break;
            case 3:
                display(q);
                break;
            case 4:
                exit(1);
            default:
                printf("\nInvalid Choice!\n");
        }
    }
}

0 comments:

Post a Comment