Skip to main content

14. Double Ended 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 enqueueRear(struct MyQueue *a)
{
    if ((*a).rear == (*a).max - 1)
        printf("\t\t\tQueue is full, enqueue is not possible...\n");
    else
    {
        if ((*a).front == -1)
            (*a).front++;
        (*a).rear++;
        printf("\t\tEnter the element to be pushed: ");
        scanf("%d", &(*a).Base[(*a).rear]);
        printf("\t\tInsertion successful...");
    }
}
void enqueueFront(struct MyQueue *a)
{
    if ((*a).front == 0)
        printf("\t\t\tInsertion not possible...\n");
    else
    {
        if ((*a).front == -1)
        {
            (*a).front++;
            (*a).rear++;
        }
        else
            (*a).front--;
        printf("\t\tEnter the value to be inserted: ");
        scanf("%d", &(*a).Base[(*a).front]);
    }
}
void dequeueFront(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++;
    }
}
void dequeueRear(struct MyQueue *a)
{
    if ((*a).front == -1)
        printf("\t\t\tQueue is empty, dequeue is not possible...\n");
    else
    {
        printf("\t\tDeleted value: %d\n", (*a).Base[(*a).rear]);
        if ((*a).front == (*a).rear)
            (*a).front = (*a).rear = -1;
        else
            (*a).rear--;
    }
}
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");
        for (int i = (*a).front; i <= (*a).rear; i++)
            printf("%d\t", (*a).Base[i]);
    }
}
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, 10);
    while (1)
    {
        star();
        printf("\t\t\t\t\tQUEUE IMPLEMENTATION\n\n\t\t\tPress '1' to perform ENQUEUE from last in array\n\t\t\t
        Press '2' to perform DEQUEUE from first in array\n\t\t\tPress '3' to perform ENQUEUE from first the queue in array
        \n\t\t\tPress '4' to perform DEQUEUE from last in array\n\t\t\tPress '5' to DISPLAY the elements in array\n\t\t\t
        Press 'any other key' to EXIT\n\t\t\tEnter your choice: ");
        scanf("%d", &choice);
        star();
        if (choice == 1)
        {
            enqueueRear(&queue);
            star();
        }
        else if (choice == 2)
        {
            dequeueFront(&queue);
            star();
        }
        else if (choice == 3)
        {
            enqueueFront(&queue);
            star();
        }
        else if (choice == 4)
        {
            dequeueRear(&queue);
            star();
        }
        else if (choice == 5)
        {
            display(&queue);
            star();
        }
        else
        {
            printf("\t\t\tExiting...");
            star();
            exit(0);
        }
    }
}


Output :


                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 3


                ================================================================================================

                Enter the value to be inserted: 1


                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 1


                ================================================================================================

                Enter the element to be pushed: 2
                Insertion successful...

                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 1


                ================================================================================================

                Enter the element to be pushed: 3
                Insertion successful...

                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 3


                ================================================================================================

                        Insertion not possible...


                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 2


                ================================================================================================

                Popped value: 1


                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 3


                ================================================================================================

                Enter the value to be inserted: 0


                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 3


                ================================================================================================

                        Insertion not possible...


                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 5


                ================================================================================================

                Printing the elements in queue...
                0       2       3

                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 4


                ================================================================================================

                Deleted value: 3


                ================================================================================================



                ================================================================================================

                                        QUEUE IMPLEMENTATION

                        Press '1' to perform ENQUEUE from last in array
                        Press '2' to perform DEQUEUE from first in array
                        Press '3' to perform ENQUEUE from first the queue in array
                        Press '4' to perform DEQUEUE from last in array
                        Press '5' to DISPLAY the elements in array
                        Press 'any other key' to EXIT
                        Enter your choice: 7


                ================================================================================================

                        Exiting...

                ================================================================================================

Comments