What is a Singly Linked List in C?
A singly linked list is a linear data structure where each node contains two parts:
- Data – the value or information stored.
- Pointer (next) – a reference to the next node in the list.
Here’s how a single node looks in C:
struct Node { int data; struct Node* next; };
The list starts from a special pointer called head, which points to the first node. If the list is empty, head is NULL.
Types of Singly Linked Lists
- Unordered Singly Linked List – No order is maintained while inserting.
- Ordered Singly Linked List – Elements are inserted in sorted (ascending/descending) order.
Unordered Singly Linked List
An unordered singly linked list does not maintain any specific order among its elements.
Insertion at Beginning
void insert_unordered(struct quiz **head, int value) { struct quiz *newNode = (struct quiz *)malloc(sizeof(struct quiz)); newNode->data = value; newNode->next = *head; *head = newNode; }
Traversal
advertisement
void traverse_list(struct quiz *head) { while (head != NULL) { printf("%d -> ", head->data); head = head->next; } printf("NULL\n"); }
Deletion of a Node
void delete_unordered(struct quiz **head, int key) { struct quiz *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); }
Example – Unordered Singly Linked List
Free 30-Day Python Certification Bootcamp is Live. Join Now!
int main() { struct quiz *certification = NULL; insert_unordered(&certification, 30); insert_unordered(&certification, 10); insert_unordered(&certification, 20); printf("Unordered Linked List:\n"); traverse_list(certification); delete_unordered(&certification, 10); printf("After Deletion:\n"); traverse_list(certification); return 0; }
Ordered Singly Linked List (Ascending)
An ordered singly linked list maintains its elements in a sorted manner, typically in ascending order.
Insertion in Sorted Order
void insert_ordered(struct quiz **head, int value) { struct quiz *newNode = (struct quiz *)malloc(sizeof(struct quiz)); newNode->data = value; newNode->next = NULL; if (*head == NULL || (*head)->data >= value) { newNode->next = *head; *head = newNode; } else { struct quiz *current = *head; while (current->next != NULL && current->next->data < value) { current = current->next; } newNode->next = current->next; current->next = newNode; } }
Traversal (Same as Before)
void traverse_list(struct quiz *head) { while (head != NULL) { printf("%d -> ", head->data); head = head->next; } printf("NULL\n"); }
Example – Ordered Singly Linked List
advertisement
int main() { struct quiz *test = NULL; insert_ordered(&test, 50); insert_ordered(&test, 10); insert_ordered(&test, 30); printf("Ordered Linked List:\n"); traverse_list(test); return 0; }
Ordered vs. Unordered Singly Linked Lists
Feature | Ordered List | Unordered List |
---|---|---|
Order of elements | Sorted (e.g., ascending order) | No specific order |
Insertion logic | Insert in sorted position | Insert at head or end |
Search efficiency | Can break early during search | Must scan full list |
Use case | When order matters | When speed of insert matters |
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
If you wish to look at all C Tutorials, go to C Tutorials.
Related Posts:
- Check C Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs