C Program To Implement Singly Linked List in Data Structures
Learn How To Create a Singly Linked List in C Programming Language. This is a simple C Program for Linked List in Data Structures using Functions. We have defined different operations here including:
- Insertion of Element in the List
- Traverse or Display Elements
- Deletion of a Node
- Count the Number of Nodes
- Searching an Element
What is a Linked List?
A Singly Linked List is made up of Nodes where every node has two parts viz., the information part and the link part. The information part contains the actual data to be stored in the list and the link part contains the pointer to the next node in the list. The list begins with a special pointer called as start. The final node in the list last has its link pointing to NULL.
In other words, a linked list is an array of nodes that makes it easy to re-arrange data without shifting data within the system memory.

Must Read: C Program To Implement Banker’s Algorithm
C Program To Implement Singly Linked List in Data Structures
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | #include<stdlib.h> #include<stdio.h> struct node { int information; struct node *link; }; struct node *create_list(struct node *starting_node); void display(struct node *starting_node); void count(struct node *starting_node); void search(struct node *starting_node, int data); struct node *addatbeginning(struct node *starting_node, int data); struct node *addatend(struct node *starting_node, int data); struct node *addafter(struct node *starting_node, int data, int item); struct node *addbefore(struct node *starting_node, int data, int item); struct node *addatposition(struct node *starting_node, int data, int position); struct node *deletenode(struct node *starting_node, int data); struct node *reverselist(struct node *starting_node); int main() { struct node *starting_node = NULL; int option, data, item, position; while(1) { printf("\n1. Create a new linked list"); printf("\n2. Display the elements of the linked list"); printf("\n3. Count the total number of elements in the linked list"); printf("\n4. Search an element in the linked list"); printf("\n5. Add an element to beginning"); printf("\n6. Add an element at the end"); printf("\n7. Add an element after a node"); printf("\n8. Add an element before a node"); printf("\n9. Add an element at a position"); printf("\n10. Delete an element in the linked list"); printf("\n11. Reverse the singly linked list"); printf("\n12. Quit"); printf("\nEnter Your Choice:\t"); scanf("%d", &option); switch(option) { case 1: starting_node = create_list(starting_node); break; case 2: display(starting_node); break; case 3: count(starting_node); break; case 4: printf("\nEnter the element to search:\t"); scanf("%d", &data); search(starting_node, data); break; case 5: printf("\nEnter the element to add at beginning:\t"); scanf("%d", &data); starting_node = addatbeginning(starting_node, data); break; case 6: printf("\nEnter the element to insert at the end:\t"); scanf("%d", &data); starting_node = addatend(starting_node, data); break; case 7: printf("\nEnter the element to be inserted after a node:\t"); scanf("%d", &data); printf("\nEnter the element after which to insert:\t"); scanf("%d", &item); starting_node = addafter(starting_node, data, item); break; case 8: printf("\nEnter the element to be insert before a nide:\t"); scanf("%d", &data); printf("\nEnter the element before which to insert:\t"); scanf("%d", &item); starting_node = addbefore(starting_node, data, item); break; case 9: printf("\nEnter the element to be inserted at a position:\t"); scanf("%d", &data); printf("\nEnter the position at which to insert:\t"); scanf("%d", &position); starting_node = addatposition(starting_node, data, position); break; case 10: printf("\nEnter the element to be deleted from the linked list:\t"); scanf("%d", &data); starting_node = deletenode(starting_node, data); break; case 11: starting_node = reverselist(starting_node); break; case 12: exit(1); default: printf("\nWrong Input. Enter a proper Input\n"); } } return 0; } struct node *create_list(struct node *starting_node) { int count, limit, data; printf("\nEnter the number of nodes:\t"); scanf("%d", &limit); starting_node = NULL; if(limit == 0) { return starting_node; } printf("\nEnter the element to insert:\t"); scanf("%d", &data); starting_node = addatbeginning(starting_node, data); for(count = 2; count <= limit; count++) { printf("\nEnter the element to insert:\t"); scanf("%d", &data); starting_node = addatend(starting_node, data); } return starting_node; } void display(struct node *starting_node) { struct node *new_node; if(starting_node == NULL) { printf("\nLinked list is empty"); return; } new_node = starting_node; printf("\nLinked list elements:\n"); while(new_node != NULL) { printf("%d\t", new_node -> information); new_node = new_node -> link; } } void count(struct node *starting_node) { struct node *new_node; int total = 0; new_node = starting_node; while(new_node != NULL) { new_node = new_node -> link; total++; } printf("\nTotal number of elements in linked list:\t%d\n", total); } void search(struct node *starting_node, int item) { struct node *new_node; int position = 1; new_node = starting_node; while(new_node != NULL) { if(new_node -> information == item) { printf("\nItem %d found at position no. %d", item, position); return; } new_node = new_node -> link; position++; } printf("\nItem %d not found in linked list", item); } struct node *deletenode(struct node *starting_node, int data) { struct node *new_node, *temp; if(starting_node == NULL) { printf("\nLinked list is empty"); return starting_node; } if(starting_node -> information == data) { temp = starting_node; starting_node = starting_node -> link; free(temp); return starting_node; } new_node = starting_node; while(new_node -> link != NULL) { if(new_node -> link -> information == data) { temp = new_node -> link; new_node -> link = temp -> link; free(temp); return starting_node; } new_node = new_node -> link; } printf("\nElement %d not found in linked list", data); return starting_node; } struct node *reverselist(struct node *starting_node) { struct node *prev, *next, *ptr; prev = NULL; ptr = starting_node; while(ptr != NULL) { next = ptr -> link; ptr -> link = prev; prev = ptr; ptr = next; } starting_node = prev; return starting_node; } struct node *addafter(struct node *starting_node, int data, int item) { struct node *new_node, *temp; new_node = starting_node; while(new_node != NULL) { if(new_node -> information == item) { temp = (struct node *)malloc(sizeof(struct node)); temp -> information = data; temp -> link = new_node -> link; new_node -> link = temp; return starting_node; } new_node = new_node -> link; } printf("\n%d Element not present in linked list", item); return starting_node; } struct node *addatend(struct node *starting_node, int data) { struct node *new_node, *temp; temp = (struct node *)malloc(sizeof(struct node)); temp -> information = data; new_node = starting_node; while(new_node -> link != NULL) { new_node = new_node -> link; } new_node -> link = temp; temp -> link = NULL; return starting_node; } struct node *addatbeginning(struct node *starting_node, int data) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp -> information = data; temp -> link = starting_node; starting_node = temp; return starting_node; } struct node *addbefore(struct node *starting_node, int data, int item) { struct node *new_node, *temp; if(starting_node == NULL) { printf("\nLinked list is empty"); return starting_node; } if(item == starting_node -> information) { temp = (struct node *)malloc(sizeof(struct node)); temp -> information = data; temp -> link = new_node -> link; new_node -> link = temp; return starting_node; } new_node = new_node -> link; printf("\n%d element not present", item); return starting_node; } struct node *addatposition(struct node *starting_node, int data, int position) { struct node *temp, *new_node; int count; temp = (struct node *)malloc(sizeof(struct node)); temp -> information = data; if(position == 1) { temp -> link = starting_node; starting_node = temp; return starting_node; } new_node = starting_node; for(count = 1; count < position - 1 && new_node != NULL; count++) { new_node = new_node -> link; } if(new_node != NULL) { printf("There are less than %d element", position); } else { temp -> link = new_node -> link; new_node -> link = temp; } return starting_node; } |
Must Read: C Program For Warshall’s Algorithm
Output

If you have any compilation errors or doubts in this C Program to Create A Singly Linked List in Data Structures, let us know about it in the Comment Section below.
What a fanstastic code for Linked List program in C language. Thanks for so many functions in Linked List code.
Yes. Singly Linked with functions has made this code easy to understand.
Linked Programs are a little hard to understand and this one is too long actually. You could have written a shorter piece of code.
Yes. It is a little difficult to grasp. But, once you understand the logic, it is really simple to convert it into code. This particular program focuses on the complete operations of a Singly Linked List. Hence, we had to include all the operations which made the code length longer. You can take your required piece of code from this program
Is this Structure Self Referential? I am confused with its meaning. Can you please explain it?
A Structure which has a Pointer Element to itself is known as a Self Referential Structure. It is used to point to the next element of the same structure using Deference Pointer Notation.
This linked list code provides so many functions for Linked List Operations in C. I think these are the primary operations that can be done in a linked list. Thanks CodingAlpha.
Can you list other types of Linked Lists in C Programming?
Sure. The different types of Linked Lists that you can implement in C Programming are:
Which functions are required in the above program to insert node in a linked list?
For Insertion in Linked List, you should include the create node function and insert node function in your Linked List Program in C Language.
This is like all in one code for Linked Lists in C Programming. Just perfect.
This singly linked list program in c is one of the best codes i found for all the operations on a linked list in C programming. Thanks for the efforts.