Untitled

mail@pastecode.io avatar
unknown
c_cpp
23 days ago
5.5 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

// Maximum size of the circular queue
#define MAX_SIZE 5

// Structure for the circular queue
struct CircularQueue {
    int* array;
    int front;
    int rear;
    int size;
};

// Function to initialize an empty circular queue
void initializeQueue(struct CircularQueue* queue) {
    queue->array = (int*)malloc(MAX_SIZE * sizeof(int));
    queue->front = -1;
    queue->rear = -1;
    queue->size = 0;
}

// Function to check if the circular queue is empty
int isEmpty(struct CircularQueue* queue) {
    return (queue->size == 0);
}

// Function to check if the circular queue is full
int isFull(struct CircularQueue* queue) {
    return (queue->size == MAX_SIZE);
}

// Function to enqueue (insert) an element into the circular queue
void enqueue(struct CircularQueue* queue, int data) {
    if (isFull(queue)) {
        printf("Error: Queue overflow\n");
        return;
    }

    if (isEmpty(queue)) {
        queue->front = 0;
    }

    queue->rear = (queue->rear + 1) % MAX_SIZE;
    queue->array[queue->rear] = data;
    queue->size++;

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

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

    int dequeuedData = queue->array[queue->front];
    if (queue->front == queue->rear) {
        // If there was only one element in the queue
        queue->front = -1;
        queue->rear = -1;
    } else {
        queue->front = (queue->front + 1) % MAX_SIZE;
    }

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

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

    int i = queue->front;
    printf("Circular Queue elements: ");
    do {
        printf("%d ", queue->array[i]);
        i = (i + 1) % MAX_SIZE;
    } while (i != (queue->rear + 1) % MAX_SIZE);
    printf("\n");
}

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

    int choice, element;

    do {
        printf("\nCircular Queue 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("5. Check if Queue is Full\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 5:
                if (isFull(&myQueue)) {
                    printf("Queue is full.\n");
                } else {
                    printf("Queue is not full.\n");
                }
                break;
            case 0:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }

    } while (choice != 0);

    free(myQueue.array); // Free dynamically allocated memory

    return 0;
}

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

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

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

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

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

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

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

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

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