Skip to content

luc064.c

Problem Statement

Simulate a Dequeue (Double Ended Queue) using an array. Support: retrieve left, retrieve right, insert left, insert right. Use pointers left and right.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Intermediate (index: 5 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Recursion
  • Pointers
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX 10

void insert_left(int *, int *, int *, int);
void insert_right(int *, int *, int *, int);
void retrieve_left(int *, int *, int *);
void retrieve_right(int *, int *, int *);
void display(int *, int, int);

int main()
{
    int dq[MAX];
    int left = -1, right = -1;
    int choice, val;

    while (1)
    {
        printf("\n--- Dequeue Menu ---\n");
        printf("1. Insert Left\n2. Insert Right\n");
        printf("3. Retrieve Left\n4. Retrieve Right\n");
        printf("5. Display\n6. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            printf("Enter value: ");
            scanf("%d", &val);
            insert_left(dq, &left, &right, val);
            break;
        case 2:
            printf("Enter value: ");
            scanf("%d", &val);
            insert_right(dq, &left, &right, val);
            break;
        case 3:
            retrieve_left(dq, &left, &right);
            break;
        case 4:
            retrieve_right(dq, &left, &right);
            break;
        case 5:
            display(dq, left, right);
            break;
        case 6:
            exit(0);
        default:
            printf("Invalid choice!\n");
        }
    }
    return 0;
}

void insert_left(int *dq, int *left, int *right, int val)
{
    // Check if full
    if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
    {
        printf("Overflow! Dequeue is full.\n");
        return;
    }

    if (*left == -1) // Initially empty
    {
        *left = 0;
        *right = 0;
    }
    else if (*left == 0) // Wrap around
        *left = MAX - 1;
    else
        (*left)--;

    dq[*left] = val;
    printf("Inserted %d at Left.\n", val);
}

void insert_right(int *dq, int *left, int *right, int val)
{
    if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
    {
        printf("Overflow! Dequeue is full.\n");
        return;
    }

    if (*left == -1) // Initially empty
    {
        *left = 0;
        *right = 0;
    }
    else if (*right == MAX - 1) // Wrap around
        *right = 0;
    else
        (*right)++;

    dq[*right] = val;
    printf("Inserted %d at Right.\n", val);
}

void retrieve_left(int *dq, int *left, int *right)
{
    if (*left == -1)
    {
        printf("Underflow! Dequeue is empty.\n");
        return;
    }

    printf("Retrieved from Left: %d\n", dq[*left]);

    if (*left == *right) // Only one element was present
    {
        *left = -1;
        *right = -1;
    }
    else if (*left == MAX - 1)
        *left = 0;
    else
        (*left)++;
}

void retrieve_right(int *dq, int *left, int *right)
{
    if (*left == -1)
    {
        printf("Underflow! Dequeue is empty.\n");
        return;
    }

    printf("Retrieved from Right: %d\n", dq[*right]);

    if (*left == *right) // Only one element was present
    {
        *left = -1;
        *right = -1;
    }
    else if (*right == 0)
        *right = MAX - 1;
    else
        (*right)--;
}

void display(int *dq, int left, int right)
{
    int i;
    if (left == -1)
    {
        printf("Dequeue is Empty\n");
        return;
    }

    printf("Elements: ");
    i = left;
    while (1)
    {
        printf("%d ", dq[i]);
        if (i == right)
            break;
        if (i == MAX - 1)
            i = 0;
        else
            i++;
    }
    printf("\n");
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc064.c):

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>

    #define MAX 10

    void insert_left(int *, int *, int *, int);
    void insert_right(int *, int *, int *, int);
    void retrieve_left(int *, int *, int *);
    void retrieve_right(int *, int *, int *);
    void display(int *, int, int);

    int main()
    {
        int dq[MAX];
        int left = -1, right = -1;
        int choice, val;

        while (1)
        {
            printf("\n--- Dequeue Menu ---\n");
            printf("1. Insert Left\n2. Insert Right\n");
            printf("3. Retrieve Left\n4. Retrieve Right\n");
            printf("5. Display\n6. Exit\n");
            printf("Enter choice: ");
            scanf("%d", &choice);

            switch (choice)
            {
            case 1:
                printf("Enter value: ");
                scanf("%d", &val);
                insert_left(dq, &left, &right, val);
                break;
            case 2:
                printf("Enter value: ");
                scanf("%d", &val);
                insert_right(dq, &left, &right, val);
                break;
            case 3:
                retrieve_left(dq, &left, &right);
                break;
            case 4:
                retrieve_right(dq, &left, &right);
                break;
            case 5:
                display(dq, left, right);
                break;
            case 6:
                exit(0);
            default:
                printf("Invalid choice!\n");
            }
        }
        return 0;
    }

    void insert_left(int *dq, int *left, int *right, int val)
    {
        // Check if full
        if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
        {
            printf("Overflow! Dequeue is full.\n");
            return;
        }

        if (*left == -1) // Initially empty
        {
            *left = 0;
            *right = 0;
        }
        else if (*left == 0) // Wrap around
            *left = MAX - 1;
        else
            (*left)--;

        dq[*left] = val;
        printf("Inserted %d 
    ... (truncated for brevity)