From d7eeba8a481d4cb6f019f588a21bc875996e1a35 Mon Sep 17 00:00:00 2001 From: Wander_lust Date: Thu, 22 Jan 2026 17:38:03 +0530 Subject: [PATCH] Refactor queue implementation in QueueInLL.c --- QueueInLL.c | 180 ++++++++++++++++++++++------------------------------ 1 file changed, 75 insertions(+), 105 deletions(-) diff --git a/QueueInLL.c b/QueueInLL.c index 7a81672..ec142cc 100644 --- a/QueueInLL.c +++ b/QueueInLL.c @@ -1,128 +1,98 @@ -#include -#include +#include +#include +typedef struct node{ + int data; + struct node* next; +}Node; -typedef int element; -typedef struct queue *queueptr; +Node* front = NULL; +Node* rear = NULL; +void enqueue(int value) +{ + Node* newnode = (Node*)malloc(sizeof(Node)); -typedef struct queue { - element item; - queueptr link; -} queue; - - -queueptr front = NULL; -queueptr rear = NULL; - -int ISEMPTY(); -void addq(element item); -element deleteq(); -void display(); - - -int main() { - int choice, item; - - while (1) { - printf("\n--- Single Queue Menu ---\n"); - printf("1. Add to Queue\n"); - printf("2. Delete from Queue\n"); - printf("3. Display Queue\n"); - printf("4. Exit\n"); - printf("Enter your choice: "); - - scanf("%d", &choice); - - - switch (choice) { - case 1: - printf("Enter item to add: "); - scanf("%d", &item); - addq(item); - printf("Item %d added to queue.\n", item); - break; - - case 2: - - item = deleteq(); - printf("Deleted item %d from queue.\n", item); - break; - - case 3: - display(); - break; - - case 4: - printf("Exiting program.\n"); - exit(0); - - default: - printf("Invalid choice. Please try again.\n"); - } - } - - return 0; -} - -int ISEMPTY() { - return (front == NULL); -} - - -void addq(element item) { - - queueptr temp = (queueptr)malloc(sizeof(queue)); - - - if (temp == NULL) { - fprintf(stderr, "Memory allocation failed. The queue is full!\n"); + if(!newnode){ + fprintf(stderr, "Memory allocation failed\n"); exit(1); } - temp->item = item; - temp->link = NULL; + newnode->data = value; + newnode->next = NULL; - if (ISEMPTY()) { - front = temp; - } else { - rear->link = temp; + if(front == NULL){ + rear = front = newnode; + }else{ + rear->next = newnode; + rear = newnode; } - rear = temp; + printf("added %d onto rear\n", value); + + } - -element deleteq() { - queueptr temp = front; - element item; - - if (ISEMPTY()) { - fprintf(stderr, "The queue is empty, cannot delete.\n"); - exit(1); +int dequeue(){ + Node* temp = front; + int item; + if(front == NULL){ + printf("stack is empty"); + return -1; } - - item = temp->item; - front = temp->link; + item = temp->data; + front = front->next; free(temp); - if (ISEMPTY()) { + + if(front== NULL){ rear = NULL; } return item; } - -void display() { - queueptr temp = front; - - if (ISEMPTY()) { - printf("Queue is empty.\n"); +void display(){ + Node* temp = front; + if(front == NULL){ + printf("Stack is empty"); return; } - printf("Queue contents (front to rear): "); - while (temp != NULL) { - printf("%d ", temp->item); - temp = temp->link; + while(temp!=NULL){ + printf("%d->",temp->data); + temp = temp->next; + } - printf("\n"); + printf("NULL\n"); +} +int main(){ + int choice,value; + while(1){ + printf("\n---queue menu---"); + printf("\n1. enqueue"); + printf("\n2. dequeue"); + printf("\n3. display"); + printf("\n4. exit"); + printf("\nEnter your choice: "); + scanf("%d", &choice); + + switch(choice){ + case 1: printf("Enter item to be added: "); + scanf("%d", &value); + enqueue(value); + break; + case 2: value = dequeue(); + if(value!=-1) + printf("popped %d", value); + break; + case 3: display(); + break; + case 4: printf("exiting\n"); + exit(0); + default: + printf("enter valid choice bruh"); + + + } + + } + return 0; }