From cf8cc6d48d72b3605c329bb73a73b12920336047 Mon Sep 17 00:00:00 2001 From: Swami Patil Date: Sat, 7 Dec 2024 21:39:54 +0530 Subject: [PATCH] Added Queue Data Structure --- Data Structure Using C/queue/queue.c | 89 ++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Data Structure Using C/queue/queue.c diff --git a/Data Structure Using C/queue/queue.c b/Data Structure Using C/queue/queue.c new file mode 100644 index 00000000..9fe87fe9 --- /dev/null +++ b/Data Structure Using C/queue/queue.c @@ -0,0 +1,89 @@ +#include +#define maxsize 10 + +int queue[maxsize], front = -1, rear = -1; + +void enqueue(); +void dequeue(); +void display(); + +int main() +{ + int choice; + + do + { + printf("\n----queue----\n"); + printf("Press 1 for enqueue\n"); + printf("Press 2 for dequeue\n"); + printf("Press 3 for display\n"); + printf("Press 4 for exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch(choice) + { + case 1: enqueue(); break; + case 2: dequeue(); break; + case 3: display(); break; + case 4: break; + default: printf("Invalid choice\n"); + } + } while(choice != 4); + + return 0; +} + +void enqueue() +{ + if (rear == maxsize - 1) + { + printf("Queue is overflow\n"); + } + else + { + if (front == -1) + front = 0; // Initialize front when first element is added + + printf("Enter value: "); + rear++; + scanf("%d", &queue[rear]); + } +} + +void dequeue() +{ + if (front == -1 || front > rear) + { + printf("Queue is empty\n"); + } + else + { + printf("Deleted item: %d\n", queue[front]); + front++; + + // Reset front and rear if queue becomes empty + if (front > rear) + { + front = -1; + rear = -1; + } + } +} + +void display() +{ + if (front == -1 || front > rear) + { + printf("Queue is empty\n"); + } + else + { + printf("Queue elements: "); + for (int i = front; i <= rear; i++) + { + printf("%d\t", queue[i]); + } + printf("\n"); + } +}