From 9ca6b1b2a958085f206bb33a4f0f02ff4a0fdafd Mon Sep 17 00:00:00 2001 From: Nihhaar Saini Date: Thu, 23 Oct 2025 13:24:31 +0530 Subject: [PATCH 1/3] Add doubly linked list implementation in C --- data_structures/doubly_linked_list.c | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 data_structures/doubly_linked_list.c diff --git a/data_structures/doubly_linked_list.c b/data_structures/doubly_linked_list.c new file mode 100644 index 0000000000..62e73604dd --- /dev/null +++ b/data_structures/doubly_linked_list.c @@ -0,0 +1,32 @@ +void ins_pos() { + int pos; + printf("\nEnter the position where the node should be inserted: "); + scanf("%d", &pos); + + if (pos < 1 || pos > count() + 1) { + printf("\nInvalid position!\n"); + return; + } + + if (pos == 1) { + ins_beg(); + return; + } + + struct node *newnode = create_node(); // <<< ADD THIS LINE + + temp = head; + for (int i = 1; i < pos - 1; i++) { + temp = temp->next; + } + + newnode->next = temp->next; + newnode->prev = temp; + + if (temp->next != NULL) { + temp->next->prev = newnode; + } + + temp->next = newnode; + printf("\n%d is inserted at position %d\n", newnode->data, pos); +} From 6cd7e7ecfbdb2bbb24a54a0c14bc9c4bea5d8450 Mon Sep 17 00:00:00 2001 From: Nihhaar Saini Date: Thu, 23 Oct 2025 13:29:06 +0530 Subject: [PATCH 2/3] Circular linked list implementation in C --- data_structures/circular_linked_list.c | 222 +++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 data_structures/circular_linked_list.c diff --git a/data_structures/circular_linked_list.c b/data_structures/circular_linked_list.c new file mode 100644 index 0000000000..07dfa70ec8 --- /dev/null +++ b/data_structures/circular_linked_list.c @@ -0,0 +1,222 @@ +#include +#include + +struct node { + int data; + struct node* next; +} *head = NULL, *temp = NULL, *newnode = NULL; + +// Create a new node +struct node* create_node() { + newnode = (struct node*)malloc(sizeof(struct node)); + printf("\nEnter the data: "); + scanf("%d", &newnode->data); + newnode->next = NULL; + return newnode; +} + +// Count nodes in the circular linked list +int count() { + int cnt = 0; + if (head == NULL) return 0; + + temp = head; + do { + cnt++; + temp = temp->next; + } while (temp != head); + + return cnt; +} + +// Insert at beginning +void ins_begin() { + newnode = create_node(); + if (head == NULL) { + head = newnode; + newnode->next = head; + } else { + temp = head; + while (temp->next != head) temp = temp->next; + newnode->next = head; + head = newnode; + temp->next = head; + } + printf("\n%d is inserted at beginning\n", newnode->data); +} + +// Insert at given position +void ins_pos(int pos) { + if (pos < 1 || pos > count() + 1) { + printf("\nInvalid position\n"); + return; + } + + if (pos == 1) { + ins_begin(); + return; + } + + newnode = create_node(); + temp = head; + for (int i = 1; i < pos - 1; i++) temp = temp->next; + + newnode->next = temp->next; + temp->next = newnode; + printf("\n%d is inserted at position %d\n", newnode->data, pos); +} + +// Insert at end +void ins_end() { + newnode = create_node(); + if (head == NULL) { + head = newnode; + newnode->next = head; + } else { + temp = head; + while (temp->next != head) temp = temp->next; + temp->next = newnode; + newnode->next = head; + } + printf("\n%d is inserted at end\n", newnode->data); +} + +// Delete from beginning +void del_beg() { + if (head == NULL) { + printf("\nEmpty list\n"); + return; + } + + struct node* to_delete = head; + if (head->next == head) { + printf("\n%d is deleted\n", head->data); + free(head); + head = NULL; + return; + } + + temp = head; + while (temp->next != head) temp = temp->next; + head = head->next; + temp->next = head; + + printf("\n%d is deleted\n", to_delete->data); + free(to_delete); +} + +// Delete from a position +void del_pos(int pos) { + if (head == NULL) { + printf("\nEmpty list\n"); + return; + } + int total = count(); + if (pos < 1 || pos > total) { + printf("\nInvalid position\n"); + return; + } + + if (pos == 1) { + del_beg(); + return; + } + + temp = head; + for (int i = 1; i < pos - 1; i++) temp = temp->next; + + struct node* to_delete = temp->next; + temp->next = to_delete->next; + + printf("\n%d is deleted\n", to_delete->data); + free(to_delete); +} + +// Delete from end +void del_end() { + if (head == NULL) { + printf("\nEmpty list\n"); + return; + } + + if (head->next == head) { + printf("\n%d is deleted\n", head->data); + free(head); + head = NULL; + return; + } + + temp = head; + while (temp->next->next != head) temp = temp->next; + + struct node* to_delete = temp->next; + temp->next = head; + + printf("\n%d is deleted\n", to_delete->data); + free(to_delete); +} + +// Display the list +void display() { + if (head == NULL) { + printf("\nEmpty list\n"); + return; + } + temp = head; + do { + printf("%d -> ", temp->data); + temp = temp->next; + } while (temp != head); + printf("(head)\n"); +} + +// Main menu +int main() { + int n; + while (1) { + printf("\nSELECT OPERATION\n"); + printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit\nEnter your choice: "); + scanf("%d", &n); + + switch (n) { + case 1: { + int x, pos; + printf("SELECT INSERTION TYPE:\n1. Insert at beginning\n2. Insert at position\n3. Insert at end\nEnter your choice: "); + scanf("%d", &x); + + switch (x) { + case 1: ins_begin(); break; + case 2: + printf("Enter the position to insert node: "); + scanf("%d", &pos); + ins_pos(pos); + break; + case 3: ins_end(); break; + default: printf("\nInvalid choice for insertion!\n"); + } + break; + } + case 2: { + int z, pos; + printf("SELECT DELETION TYPE:\n1. Delete from beginning\n2. Delete from position\n3. Delete from end\nEnter your choice: "); + scanf("%d", &z); + + switch (z) { + case 1: del_beg(); break; + case 2: + printf("Enter the position to delete node: "); + scanf("%d", &pos); + del_pos(pos); + break; + case 3: del_end(); break; + default: printf("\nInvalid choice for deletion!\n"); + } + break; + } + case 3: display(); break; + case 4: printf("\nExiting\n"); exit(0); + default: printf("\nInvalid choice\n"); + } + } + return 0; +} From 91806b757182a46fe711309cdfcb0698d8b65b32 Mon Sep 17 00:00:00 2001 From: Nihhaar Saini Date: Thu, 23 Oct 2025 13:39:44 +0530 Subject: [PATCH 3/3] Add linked list with sorting and utility functions in C --- data_structures/sorting_linked_list.c | 307 ++++++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 data_structures/sorting_linked_list.c diff --git a/data_structures/sorting_linked_list.c b/data_structures/sorting_linked_list.c new file mode 100644 index 0000000000..94722ba03b --- /dev/null +++ b/data_structures/sorting_linked_list.c @@ -0,0 +1,307 @@ +#include +#include +#include + +struct node { + int data; + struct node* next; +} *head = NULL, *p = NULL, *q = NULL; + +// Check if list is empty +bool is_empty() { + return (head == NULL); +} + +// Count nodes in the list +int count() { + int length = 0; + if (is_empty()) return 0; + + p = head; + while (p != NULL) { + length++; + p = p->next; + } + return length; +} + +// Create a new node +struct node* create_node() { + struct node* newnode = (struct node*)malloc(sizeof(struct node)); + printf("\nEnter the data: "); + scanf("%d", &newnode->data); + newnode->next = NULL; + return newnode; +} + +// Insert at beginning +void ins_beg() { + struct node* newnode = create_node(); + if (is_empty()) { + head = newnode; + } else { + newnode->next = head; + head = newnode; + } + printf("\n%d is inserted at beginning\n", newnode->data); +} + +// Insert at position +void ins_pos(int po) { + if (po < 1 || po > count() + 1) { + printf("\nInvalid position!\n"); + return; + } + + if (po == 1) { + ins_beg(); + return; + } + + struct node* newnode = create_node(); + p = head; + for (int i = 1; i < po - 1; i++) p = p->next; + + newnode->next = p->next; + p->next = newnode; + + printf("\n%d is inserted at position %d\n", newnode->data, po); +} + +// Insert at end +void ins_end() { + struct node* newnode = create_node(); + if (is_empty()) { + head = newnode; + } else { + p = head; + while (p->next != NULL) p = p->next; + p->next = newnode; + } + printf("\n%d is inserted at end\n", newnode->data); +} + +// Delete from beginning +void del_begin() { + if (is_empty()) { + printf("\nEmpty list. Deletion not possible.\n"); + return; + } + p = head; + head = p->next; + printf("\n%d is deleted\n", p->data); + free(p); +} + +// Delete from end +void del_end() { + if (is_empty()) { + printf("\nEmpty list. Deletion not possible.\n"); + return; + } + if (head->next == NULL) { + printf("\n%d is deleted\n", head->data); + free(head); + head = NULL; + return; + } + p = head; + while (p->next != NULL) { + q = p; + p = p->next; + } + printf("\n%d is deleted\n", p->data); + free(p); + q->next = NULL; +} + +// Delete from position +void del_pos(int pos) { + if (pos < 1 || pos > count()) { + printf("\nInvalid position!\n"); + return; + } + if (pos == 1) { + del_begin(); + return; + } + + p = head; + for (int i = 1; i < pos; i++) { + q = p; + p = p->next; + } + q->next = p->next; + printf("\n%d is deleted\n", p->data); + free(p); +} + +// Display the list +void display() { + if (is_empty()) { + printf("\nEmpty list\n"); + return; + } + p = head; + while (p != NULL) { + printf("%d -> ", p->data); + p = p->next; + } + printf("NULL\n"); +} + +// Find maximum element +void max() { + if (is_empty()) { + printf("\nEmpty list\n"); + return; + } + p = head; + int m = p->data; + while (p != NULL) { + if (p->data > m) m = p->data; + p = p->next; + } + printf("\n%d is the maximum element\n", m); +} + +// Find minimum element +void min() { + if (is_empty()) { + printf("\nEmpty list\n"); + return; + } + p = head; + int m = p->data; + while (p != NULL) { + if (p->data < m) m = p->data; + p = p->next; + } + printf("\n%d is the minimum element\n", m); +} + +// Sort the linked list (Bubble Sort) +void sort() { + if (is_empty()) { + printf("\nEmpty list\n"); + return; + } + + int size = count(); + int temp; + for (int i = 0; i < size - 1; i++) { + p = head; + for (int j = 0; j < size - i - 1; j++) { + if (p->data > p->next->data) { + temp = p->data; + p->data = p->next->data; + p->next->data = temp; + } + p = p->next; + } + } + printf("\nSorted list:\n"); + display(); +} + +// Search for an element +void search(int key) { + if (is_empty()) { + printf("\nEmpty list\n"); + return; + } + p = head; + int pos = 0; + bool found = false; + while (p != NULL) { + pos++; + if (p->data == key) { + found = true; + break; + } + p = p->next; + } + if (found) + printf("\nElement found at position %d\n", pos); + else + printf("\nElement not found\n"); +} + +// Reverse the list +void reverse_list() { + if (is_empty()) { + printf("\nEmpty list\n"); + return; + } + struct node *current = head, *prev = NULL, *next = NULL; + while (current != NULL) { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + head = prev; + printf("\nReversed list:\n"); + display(); +} + +// Main menu +int main() { + int n; + while (1) { + printf("\nSELECT OPERATION\n"); + printf("1. Insertion\n2. Deletion\n3. Display\n4. Maximum\n5. Minimum\n6. Sort\n7. Length\n8. Search\n9. Reverse the list\n10. Exit\nEnter your choice: "); + scanf("%d", &n); + + switch (n) { + case 1: { + int x, pos; + printf("SELECT INSERTION TYPE:\n1. Insert at beginning\n2. Insert at position\n3. Insert at end\nEnter your choice: "); + scanf("%d", &x); + switch (x) { + case 1: ins_beg(); break; + case 2: + printf("Enter the position to insert node: "); + scanf("%d", &pos); + ins_pos(pos); + break; + case 3: ins_end(); break; + default: printf("\nInvalid choice for insertion!\n"); + } + break; + } + case 2: { + int z, pos; + printf("SELECT DELETION TYPE:\n1. Delete from beginning\n2. Delete from position\n3. Delete from end\nEnter your choice: "); + scanf("%d", &z); + switch (z) { + case 1: del_begin(); break; + case 2: + printf("Enter the position to delete node: "); + scanf("%d", &pos); + del_pos(pos); + break; + case 3: del_end(); break; + default: printf("\nInvalid choice for deletion!\n"); + } + break; + } + case 3: display(); break; + case 4: max(); break; + case 5: min(); break; + case 6: sort(); break; + case 7: printf("\nNumber of nodes: %d\n", count()); break; + case 8: { + int key; + printf("\nEnter key element: "); + scanf("%d", &key); + search(key); + break; + } + case 9: reverse_list(); break; + case 10: printf("\nExiting\n"); exit(0); + default: printf("\nInvalid choice\n"); + } + } + return 0; +}