From 8689140f692592fefb2afcba3d903d0b102078ad Mon Sep 17 00:00:00 2001 From: Wander_Lust Date: Thu, 22 Jan 2026 17:02:44 +0530 Subject: [PATCH] Add DS lab linked list, stack and queue implementations --- doublyLL.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ queueLL.c | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 doublyLL.c create mode 100644 queueLL.c diff --git a/doublyLL.c b/doublyLL.c new file mode 100644 index 0000000..8863e59 --- /dev/null +++ b/doublyLL.c @@ -0,0 +1,126 @@ +#include +#include + + +typedef struct node { + int data; + struct node *prev; + struct node *next; +} NODE; + + +NODE *head = NULL; + + +void insert_front(int value) { + NODE *newNode = (NODE *)malloc(sizeof(NODE)); + + if (newNode == NULL) { + printf("Memory allocation failed\n"); + return; + } + + newNode->data = value; + newNode->prev = NULL; + newNode->next = head; + + if (head != NULL) { + head->prev = newNode; + } + + head = newNode; + printf("Inserted %d\n", value); +} + + +void delete_value(int value) { + NODE *temp = head; + + if (head == NULL) { + printf("List is empty\n"); + return; + } + + while (temp != NULL) { + if (temp->data == value) { + + + if (temp->prev == NULL) { + head = temp->next; + if (head != NULL) + head->prev = NULL; + } + + else { + temp->prev->next = temp->next; + if (temp->next != NULL) + temp->next->prev = temp->prev; + } + + free(temp); + printf("Deleted %d\n", value); + return; + } + temp = temp->next; + } + + printf("Value not found\n"); +} + +void display() { + NODE *temp = head; + + if (head == NULL) { + printf("List is empty\n"); + return; + } + + printf("List: "); + while (temp != NULL) { + printf("%d <-> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); +} + + +int main() { + int choice, value; + + while (1) { + printf("\n--- DOUBLY LINKED LIST MENU ---\n"); + printf("1. Insert at front\n"); + printf("2. Delete by value\n"); + printf("3. Display\n"); + printf("4. Exit\n"); + printf("Enter choice: "); + scanf("%d", &choice); + + switch (choice) { + + case 1: + printf("Enter value to insert: "); + scanf("%d", &value); + insert_front(value); + break; + + case 2: + printf("Enter value to delete: "); + scanf("%d", &value); + delete_value(value); + break; + + case 3: + display(); + break; + + case 4: + printf("Exiting...\n"); + exit(0); + + default: + printf("Invalid choice\n"); + } + } +} + diff --git a/queueLL.c b/queueLL.c new file mode 100644 index 0000000..ec142cc --- /dev/null +++ b/queueLL.c @@ -0,0 +1,98 @@ +#include +#include + +typedef struct node{ + int data; + struct node* next; +}Node; + +Node* front = NULL; +Node* rear = NULL; + +void enqueue(int value) +{ + Node* newnode = (Node*)malloc(sizeof(Node)); + + if(!newnode){ + fprintf(stderr, "Memory allocation failed\n"); + exit(1); + } + + newnode->data = value; + newnode->next = NULL; + + if(front == NULL){ + rear = front = newnode; + }else{ + rear->next = newnode; + rear = newnode; + } + printf("added %d onto rear\n", value); + + +} + +int dequeue(){ + Node* temp = front; + int item; + if(front == NULL){ + printf("stack is empty"); + return -1; + } + item = temp->data; + front = front->next; + free(temp); + + if(front== NULL){ + rear = NULL; + } + return item; +} + +void display(){ + Node* temp = front; + if(front == NULL){ + printf("Stack is empty"); + return; + } + + while(temp!=NULL){ + printf("%d->",temp->data); + temp = temp->next; + + } + 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; +}