diff --git a/C/Singly Linked List b/C/Singly Linked List new file mode 100644 index 000000000..7d99f5025 --- /dev/null +++ b/C/Singly Linked List @@ -0,0 +1,75 @@ +#include +#include + +struct Node { + int data; + struct Node* next; +}; + +struct Node* createNode(int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + return newNode; +} + +void insertAtBeginning(struct Node** head, int data) { + struct Node* newNode = createNode(data); + newNode->next = *head; + *head = newNode; +} + +void insertAtEnd(struct Node** head, int data) { + struct Node* newNode = createNode(data); + if (*head == NULL) { + *head = newNode; + } else { + struct Node* temp = *head; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = newNode; + } +} + +void deleteNode(struct Node** head, int key) { + struct Node* temp = *head, *prev = NULL; + if (temp != NULL && temp->data == key) { + *head = temp->next; + free(temp); + return; + } + while (temp != NULL && temp->data != key) { + prev = temp; + temp = temp->next; + } + if (temp == NULL) return; + prev->next = temp->next; + free(temp); +} + +void displayList(struct Node* head) { + while (head != NULL) { + printf("%d -> ", head->data); + head = head->next; + } + printf("NULL\n"); +} + +int main() { + struct Node* head = NULL; + + insertAtBeginning(&head, 10); + insertAtBeginning(&head, 20); + insertAtEnd(&head, 30); + insertAtEnd(&head, 40); + + printf("Linked List: "); + displayList(head); + + deleteNode(&head, 20); + printf("After deletion: "); + displayList(head); + + return 0; +}