|
| 1 | +/** |
| 2 | + * @file singly_linked_list.c |
| 3 | + * @brief Implementation of a singly linked list in C. |
| 4 | + * |
| 5 | + * This file implements a singly linked list with the following operations: |
| 6 | + * 1. Insert at the beginning |
| 7 | + * 2. Insert at the end |
| 8 | + * 3. Insert at a specific position |
| 9 | + * 4. Delete from the beginning |
| 10 | + * 5. Delete from the end |
| 11 | + * 6. Delete from a specific position |
| 12 | + * 7. Display the linked list |
| 13 | + * |
| 14 | + * Time Complexity: |
| 15 | + * - Insert at beginning: O(1) |
| 16 | + * - Insert at end: O(n) |
| 17 | + * - Insert at position: O(n) |
| 18 | + * - Delete at beginning: O(1) |
| 19 | + * - Delete at end: O(n) |
| 20 | + * - Delete at position: O(n) |
| 21 | + * - Display: O(n) |
| 22 | + * |
| 23 | + * Author: Rahul Guggilam |
| 24 | + */ |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | + |
| 29 | +/** Node structure for singly linked list */ |
| 30 | +typedef struct Node |
| 31 | +{ |
| 32 | + int data; |
| 33 | + struct Node* next; |
| 34 | +} Node; |
| 35 | + |
| 36 | +/** Function to create a new node with given data */ |
| 37 | +Node* create_node(int data) |
| 38 | +{ |
| 39 | + Node* new_node = (Node*)malloc(sizeof(Node)); |
| 40 | + if (!new_node) |
| 41 | + { |
| 42 | + printf("Memory allocation failed\n"); |
| 43 | + exit(EXIT_FAILURE); |
| 44 | + } |
| 45 | + new_node->data = data; |
| 46 | + new_node->next = NULL; |
| 47 | + return new_node; |
| 48 | +} |
| 49 | + |
| 50 | +/** Insert a node at the beginning of the linked list */ |
| 51 | +void insert_begin(Node** head, int data) |
| 52 | +{ |
| 53 | + Node* new_node = create_node(data); |
| 54 | + new_node->next = *head; |
| 55 | + *head = new_node; |
| 56 | +} |
| 57 | + |
| 58 | +/** Insert a node at the end of the linked list */ |
| 59 | +void insert_end(Node** head, int data) |
| 60 | +{ |
| 61 | + Node* new_node = create_node(data); |
| 62 | + if (*head == NULL) |
| 63 | + { |
| 64 | + *head = new_node; |
| 65 | + return; |
| 66 | + } |
| 67 | + Node* temp = *head; |
| 68 | + while (temp->next != NULL) |
| 69 | + { |
| 70 | + temp = temp->next; |
| 71 | + } |
| 72 | + temp->next = new_node; |
| 73 | +} |
| 74 | + |
| 75 | +/** Insert a node at a specific position (1-based index) */ |
| 76 | +void insert_position(Node** head, int data, int position) |
| 77 | +{ |
| 78 | + if (position < 1) |
| 79 | + { |
| 80 | + printf("Invalid position\n"); |
| 81 | + return; |
| 82 | + } |
| 83 | + if (position == 1) |
| 84 | + { |
| 85 | + insert_begin(head, data); |
| 86 | + return; |
| 87 | + } |
| 88 | + Node* temp = *head; |
| 89 | + for (int i = 1; i < position - 1 && temp != NULL; i++) |
| 90 | + { |
| 91 | + temp = temp->next; |
| 92 | + } |
| 93 | + if (temp == NULL) |
| 94 | + { |
| 95 | + printf("Position exceeds list length, inserting at end\n"); |
| 96 | + insert_end(head, data); |
| 97 | + return; |
| 98 | + } |
| 99 | + Node* new_node = create_node(data); |
| 100 | + new_node->next = temp->next; |
| 101 | + temp->next = new_node; |
| 102 | +} |
| 103 | + |
| 104 | +/** Delete the first node of the linked list */ |
| 105 | +void delete_begin(Node** head) |
| 106 | +{ |
| 107 | + if (*head == NULL) |
| 108 | + { |
| 109 | + printf("List is empty\n"); |
| 110 | + return; |
| 111 | + } |
| 112 | + Node* temp = *head; |
| 113 | + *head = (*head)->next; |
| 114 | + free(temp); |
| 115 | +} |
| 116 | + |
| 117 | +/** Delete the last node of the linked list */ |
| 118 | +void delete_end(Node** head) |
| 119 | +{ |
| 120 | + if (*head == NULL) |
| 121 | + { |
| 122 | + printf("List is empty\n"); |
| 123 | + return; |
| 124 | + } |
| 125 | + if ((*head)->next == NULL) |
| 126 | + { |
| 127 | + free(*head); |
| 128 | + *head = NULL; |
| 129 | + return; |
| 130 | + } |
| 131 | + Node* temp = *head; |
| 132 | + while (temp->next->next != NULL) |
| 133 | + { |
| 134 | + temp = temp->next; |
| 135 | + } |
| 136 | + free(temp->next); |
| 137 | + temp->next = NULL; |
| 138 | +} |
| 139 | + |
| 140 | +/** Delete a node at a specific position (1-based index) */ |
| 141 | +void delete_position(Node** head, int position) |
| 142 | +{ |
| 143 | + if (*head == NULL || position < 1) |
| 144 | + { |
| 145 | + printf("Invalid position or empty list\n"); |
| 146 | + return; |
| 147 | + } |
| 148 | + if (position == 1) |
| 149 | + { |
| 150 | + delete_begin(head); |
| 151 | + return; |
| 152 | + } |
| 153 | + Node* temp = *head; |
| 154 | + for (int i = 1; i < position - 1 && temp->next != NULL; i++) |
| 155 | + { |
| 156 | + temp = temp->next; |
| 157 | + } |
| 158 | + if (temp->next == NULL) |
| 159 | + { |
| 160 | + printf("Position exceeds list length\n"); |
| 161 | + return; |
| 162 | + } |
| 163 | + Node* del_node = temp->next; |
| 164 | + temp->next = del_node->next; |
| 165 | + free(del_node); |
| 166 | +} |
| 167 | + |
| 168 | +/** Display all nodes in the linked list */ |
| 169 | +void display(Node* head) |
| 170 | +{ |
| 171 | + if (head == NULL) |
| 172 | + { |
| 173 | + printf("List is empty\n"); |
| 174 | + return; |
| 175 | + } |
| 176 | + Node* temp = head; |
| 177 | + printf("Linked list: "); |
| 178 | + while (temp != NULL) |
| 179 | + { |
| 180 | + printf("%d -> ", temp->data); |
| 181 | + temp = temp->next; |
| 182 | + } |
| 183 | + printf("NULL\n"); |
| 184 | +} |
| 185 | + |
| 186 | +/** Main function demonstrating linked list operations */ |
| 187 | +int main() |
| 188 | +{ |
| 189 | + Node* head = NULL; |
| 190 | + int choice, data, pos; |
| 191 | + |
| 192 | + while (1) |
| 193 | + { |
| 194 | + printf( |
| 195 | + "\n1. Insert at beginning\n2. Insert at end\n3. Insert at " |
| 196 | + "position\n"); |
| 197 | + printf( |
| 198 | + "4. Delete at beginning\n5. Delete at end\n6. Delete at " |
| 199 | + "position\n7. Display\n8. Exit\n"); |
| 200 | + printf("Enter your choice: "); |
| 201 | + scanf("%d", &choice); |
| 202 | + |
| 203 | + switch (choice) |
| 204 | + { |
| 205 | + case 1: |
| 206 | + printf("Enter data: "); |
| 207 | + scanf("%d", &data); |
| 208 | + insert_begin(&head, data); |
| 209 | + break; |
| 210 | + case 2: |
| 211 | + printf("Enter data: "); |
| 212 | + scanf("%d", &data); |
| 213 | + insert_end(&head, data); |
| 214 | + break; |
| 215 | + case 3: |
| 216 | + printf("Enter data and position: "); |
| 217 | + scanf("%d %d", &data, &pos); |
| 218 | + insert_position(&head, data, pos); |
| 219 | + break; |
| 220 | + case 4: |
| 221 | + delete_begin(&head); |
| 222 | + break; |
| 223 | + case 5: |
| 224 | + delete_end(&head); |
| 225 | + break; |
| 226 | + case 6: |
| 227 | + printf("Enter position to delete: "); |
| 228 | + scanf("%d", &pos); |
| 229 | + delete_position(&head, pos); |
| 230 | + break; |
| 231 | + case 7: |
| 232 | + display(head); |
| 233 | + break; |
| 234 | + case 8: |
| 235 | + printf("Exiting...\n"); |
| 236 | + exit(0); |
| 237 | + default: |
| 238 | + printf("Invalid choice\n"); |
| 239 | + } |
| 240 | + } |
| 241 | + return 0; |
| 242 | +} |
0 commit comments