Singly Linked List Program in C
Here you will learn and get the example code of Singly linked list program in c programming using data structure.
Singly Linked List
Singly Linked List Program in C example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | //singly linked list program in c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation fail\n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } void insertAtEnd(struct Node** head, int data) { struct Node* newNode = createNode(data); if (*head == NULL) { *head = newNode; } else { struct Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } void displayList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } void freeList(struct Node* head) { struct Node* current = head; while (current != NULL) { struct Node* temp = current; current = current->next; free(temp); } } void deleteNode(struct Node** head, int value) { if (*head == NULL) { printf("List is empty.\n"); return; } if ((*head)->data == value) { struct Node* temp = *head; *head = (*head)->next; free(temp); printf("Successfully Node deleted.\n"); return; } struct Node* current = *head; while (current->next != NULL && current->next->data != value) { current = current->next; } if (current->next == NULL) { printf("Node with value %d not found.\n", value); return; } struct Node* temp = current->next; current->next = temp->next; free(temp); printf("Node deleted successfully.\n"); } int main() { struct Node* head = NULL; int ch, data, value; while (1) { printf("\nLinked List Menu:\n"); printf("1. Insert node\n"); printf("2. Display list\n"); printf("3. Delete a node\n"); printf("4. Exit\n"); printf("Enter your choice: "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter data to insert: "); scanf("%d", &data); insertAtEnd(&head, data); printf("Data inserted successfully.\n"); break; case 2: if (head == NULL) { printf("The list is empty.\n"); } else { printf("Linked List: "); displayList(head); } break; case 3: if (head == NULL) { printf("The list is empty.\n"); } else { printf("Enter value to delete: "); scanf("%d", &value); deleteNode(&head, value); } break; case 4: freeList(head); printf("Exiting\n"); exit(0); default: printf("Invalid choice! Try again.\n"); } } return 0; } |
Output
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 1
Enter data to insert: 5
Data inserted successfully.
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 1
Enter data to insert: 10
Data inserted successfully.
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 1
Enter data to insert: 15
Data inserted successfully.
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 1
Enter data to insert: 20
Data inserted successfully.
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 2
Linked List: 5 -> 10 -> 15 -> 20 -> NULL
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 3
Enter value to delete: 15
Node deleted successfully.
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 2
Linked List: 5 -> 10 -> 20 -> NULL
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 5
Invalid choice! Try again.
Linked List Menu:
1. Insert node
2. Display list
3. Delete a node
4. Exit
Enter your choice: 4
Exiting