Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
4.3 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

// Node structure for the queue
struct Node {
    int data;
    struct Node* next;
};

// Queue structure
struct Queue {
    struct Node* front;
    struct Node* rear;
};

// Function to initialize an empty queue
void initializeQueue(struct Queue* queue) {
    queue->front = NULL;
    queue->rear = NULL;
}

// Function to check if the queue is empty
int isEmpty(struct Queue* queue) {
    return (queue->front == NULL);
}

// Function to enqueue (insert) an element into the queue
void enqueue(struct Queue* queue, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;

    if (isEmpty(queue)) {
        queue->front = newNode;
        queue->rear = newNode;
    } else {
        queue->rear->next = newNode;
        queue->rear = newNode;
    }

    printf("Enqueued element: %d\n", data);
}

// Function to dequeue (remove) an element from the queue
int dequeue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Error: Queue underflow\n");
        return -1; // indicating queue underflow
    }

    int dequeuedData;
    struct Node* temp = queue->front;

    if (queue->front == queue->rear) {
        // If only one element in the queue
        dequeuedData = temp->data;
        free(temp);
        queue->front = NULL;
        queue->rear = NULL;
    } else {
        dequeuedData = temp->data;
        queue->front = temp->next;
        free(temp);
    }

    printf("Dequeued element: %d\n", dequeuedData);
    return dequeuedData;
}

// Function to display the elements in the queue
void displayQueue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue is empty\n");
        return;
    }

    struct Node* temp = queue->front;
    printf("Queue elements: ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// Main function for the menu-driven queue program
int main() {
    struct Queue myQueue;
    initializeQueue(&myQueue);

    int choice, element;

    do {
        printf("\nQueue Operations Menu:\n");
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Display Queue\n");
        printf("4. Check if Queue is Empty\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter the element to enqueue: ");
                scanf("%d", &element);
                enqueue(&myQueue, element);
                break;
            case 2:
                dequeue(&myQueue);
                break;
            case 3:
                displayQueue(&myQueue);
                break;
            case 4:
                if (isEmpty(&myQueue)) {
                    printf("Queue is empty.\n");
                } else {
                    printf("Queue is not empty.\n");
                }
                break;
            case 0:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }

    } while (choice != 0);

    return 0;
}

/*
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 1
Enter the element to enqueue: 10
Enqueued element: 10

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 1
Enter the element to enqueue: 20
Enqueued element: 20

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 3
Queue elements: 10 20 

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 2
Dequeued element: 10

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 3
Queue elements: 20 

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 4
Queue is not empty.

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Check if Queue is Empty
0. Exit
Enter your choice: 0
Exiting program.
*/
Leave a Comment