Skip to main content

13. Circular Queue - Menu driven program

 

#include <stdio.h>
#include <stdlib.h>
struct MyQueue
{
    int max;
    int front;
    int rear;
    int *Base;
} queue;
void setArray(struct MyQueue *a, int tsize)
{
    (*a).max = tsize;
    (*a).front = (*a).rear = -1;
    (*a).Base = (int *)malloc(sizeof(int) * tsize);
}
void enqueue(struct MyQueue *a)
{
    if (((*a).rear + 1) % (*a).max == (*a).front)
        printf("\t\t\tQueue is full, enqueue is not possible...\n");
    else
    {
        if ((*a).front == -1)
        {
            (*a).front = (*a).rear = 0;
        }
        else
        {
            (*a).rear = ((*a).rear + 1) % (*a).max;
        }
        printf("\t\tEnter the element to be pushed: ");
        scanf("%d", &(*a).Base[(*a).rear]);
        printf("\t\tInsertion successful...");
    }
}
void dequeue(struct MyQueue *a)
{
    if ((*a).front == -1)
        printf("\t\t\tQueue is empty, dequeue is not possible...\n");
    else
    {
        printf("\t\tPopped value: %d\n", (*a).Base[(*a).front]);
        if ((*a).front == (*a).rear)
            (*a).front = (*a).rear = -1;
        else
            (*a).front = ((*a).front + 1) % (*a).max;
    }
}
void display(struct MyQueue *a)
{
    if ((*a).front == -1)
        printf("\t\t\tQueue is empty, no element to show...\n");
    else
    {
        printf("\t\tPrinting the elements in queue...\n\t\t");
        while ((*a).front != (*a).rear)
        {
            printf("%d\t", (*a).Base[(*a).front]);
            (*a).front = ((*a).front + 1) % (*a).max;
        }
        printf("%d", (*a).Base[(*a).rear]);
    }
}
void star()
{
    int j = 0;
    printf("\n\n\t\t");
    while (j <= 5)
    {
        printf("================");
        j++;
    }
    printf("\n\n");
}
void main()
{
    int item, choice;
    setArray(&queue, 4);
    while (1)
    {
        star();
        printf("\t\t\t\tCIRCULAR QUEUE IMPLEMENTATION\n\n\t\t\tPress '1' to perform ENQUEUE in array\n\t\t\t
        Press '2' to perform DEQUEUE in array\n\t\t\tPress '3' to SHOW the queue in array\n\n\t\t\t
        Press 'any other key' to EXIT\n\t\t\tEnter your choice: ");
        scanf("%d", &choice);
        star();
        if (choice == 1)
        {
            enqueue(&queue);
            star();
        }
        else if (choice == 2)
        {
            dequeue(&queue);
            star();
        }
        else if (choice == 3)
        {
            display(&queue);
            star();
        }
        else
        {
            printf("\t\t\tExiting...");
            star();
            exit(0);
        }
    }
}


Output :

================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter the element to be pushed: 1 Insertion successful... ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter the element to be pushed: 2 Insertion successful... ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter the element to be pushed: 3 Insertion successful... ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter the element to be pushed: 4 Insertion successful... ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Queue is full, enqueue is not possible... ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 2 ================================================================================================ Popped value: 1 ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter the element to be pushed: 5 Insertion successful... ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 3 ================================================================================================ Printing the elements in queue... 2 3 4 5 ================================================================================================ ================================================================================================ CIRCULAR QUEUE IMPLEMENTATION Press '1' to perform ENQUEUE in array Press '2' to perform DEQUEUE in array Press '3' to SHOW the queue in array Press 'any other key' to EXIT Enter your choice: 5 ================================================================================================ Exiting... ================================================================================================

Comments