Linked List Data Structure in C
Overview
This article is a detailed resource that introduces linked lists in C. It covers the fundamentals, including node structure and the three main types of linked lists (singly, doubly, and circular). Practical C code for various operations, such as insertion and deletion, is provided.
The article emphasizes the advantages and disadvantages of linked lists in C and explores their real-world applications, including web browsers, operating systems, compilers, databases, and graphics software.
Linked List Data Structure
A linked list is a linear data structure that consists of a sequence of nodes. Each node contains a data field and a reference (link) to the next node in the sequence. The first node in the list is called the head node, and the last node in the list is called the tail node.
Node Structure
A node in a linked list typically consists of two components:
- Data: This field stores the actual value or data associated with the node.
- Next Pointer: This field stores the memory address (reference) of the next node in the sequence.
Head and Tail
The head and tail pointers are used to access the linked list. The head pointer points to the first node in the list, and the tail pointer points to the last node in the list.
Example:
Here is a diagram of a linked list with three nodes:
Types of linked lists
There are three main types of linked lists in C:
1. Singly Linked Lists.
- Singly linked lists in C are the simplest type of linked list.
- Each node in a singly linked list contains a data field and a pointer to the next node in the list.
- The last node in the list points to null, indicating the end of the list.
- Example: Stack, queue, linked list implementation of a dynamic array
2. Doubly Linked Lists.
- Doubly linked lists in C are more complex to implement than singly linked lists, but they are more efficient for certain operations, such as insertion, deletion, and traversal in both directions.
- Each node in a doubly linked list contains a data field, a pointer to the next node in the list, and a pointer to the previous node in the list.
- Example: LRU cache, undo/redo history, doubly linked list implementation of a binary tree
3. Circular Linked Lists
- Circular linked lists are the most complex type of linked list to implement, but they can be very efficient for certain operations, such as traversal and queueing.
- In a circular linked list, the last node in the list points back to the first node in the list, forming a loop.
- Example: Circular buffer, circular queue, circular linked list implementation of a hash table.
Representation of Linked List in C
A linked list in C can be represented in a variety of ways, but the most common representation is to use a node structure. A node structure typically contains two fields:
- Data: This field stores the actual data associated with the node.
- Next: This field contains a pointer to the next node in the list.
The first node in the list is called the head node, and the last node in the list is called the tail node. The head node points to the first node in the list, and the tail node points to null, indicating the end of the list.
Why is Linked List Data Structure needed?
Linked lists in C are needed because they offer a number of advantages over other data structures, such as arrays:
- Dynamic size: Linked lists in C can grow and shrink dynamically as needed. This is because nodes can be added and removed from the list without having to shift the other elements in the list.
- Efficient insertion and deletion: Inserting and deleting elements from a linked list is typically more efficient than doing the same operations on an array.
- Efficient memory usage: Linked lists in C can be more efficient in terms of memory usage than arrays, especially when the list is sparse (contains many empty elements). This is because each node in a linked list only stores the data and the reference to the next node.
Linked List Utility
Linked lists in C are a versatile data structure with a wide range of utilities. Some of the most common utilities of linked lists include:
- Storage: Linked lists in C can be used to store any type of data, including integers, strings, objects, and even other linked lists.
- Queues: Linked lists in C can be used to implement queues, which are FIFO (first-in, first-out) data structures.
- Stacks: Linked lists in C can be used to implement stacks, which are LIFO (last-in, first-out) data structures.
- Graphs: Linked lists in C can be used to implement graphs, which are data structures that represent relationships between entities.
- Undo/redo: Linked lists in C can be used to implement undo/redo functionality. This allows users to reverse their actions if they make a mistake.
Linked List Implementation in C
Output
Linked List Complexity
Operation | Singly Linked List | Doubly Linked List | Circular Linked List |
---|---|---|---|
Insertion at the beginning | O(1) | O(1) | O(1) |
Insertion at the end | O(n) | O(1) | O(1) |
Insertion at a given position | O(n) | O(n) | O(n) |
Deletion at the beginning | O(1) | O(1) | O(1) |
Deletion at the end | O(n) | O(1) | O(1) |
Deletion at a given position | O(n) | O(n) | O(n) |
Searching for an element | O(n) | O(n) | O(n) |
Accessing an element by index | O(n) | O(n) | O(n) |
Traversing the list | O(n) | O(n) | O(n) |
Linked List Applications
- Web browsers use linked lists to store the history of visited websites.
- Operating systems use linked lists to store the process queue, memory management, and file system.
- Compilers use linked lists to store symbol tables, parse trees, and code generation.
- Databases use linked lists to store data in tables and indexes.
- Graphics and animation software use linked lists to store objects and their relationships.
Operations on Linked Lists in C
1. Insertion
Insertion in a linked list in C is the process of adding a new node to the linked list.
insert_begin()
insert_end()
insert_pos()
2. Deletion
Deletion in a linked list is the process of removing a node from the linked list.
delete_begin()
delete_end()
delete_pos()
3. Searching
Searching in a linked list in C involves the process of locating a specific element or value within the list.
To perform a linear search in a linked list in C:
- Traverse the linked list from the beginning. Compare the data in the current node to the data that you are searching for.
- If the data in the current node matches the data that you are searching for, then return the current node.
- If the data in the current node does not match the data that you are searching for, then move to the next node.
- Repeat steps 2-4 until the desired node is found or the end of the linked list is reached.
Advantages of Linked Lists
- Dynamic Size
- Efficient Insertion and Deletion
- Memory Efficiency
- Easy Implementation of Abstract Data Types
- More Efficient Sorting
Disadvantages of Linked Lists
- No Random Access: Linked lists in C do not provide random access to elements like arrays do.
- Extra Memory Usage: Linked lists in C require extra memory compared to arrays. Each element in a linked list requires a reference to the next element.
- More Complex Implementation: Implementing a linked list is more complex than implementing an array because it requires managing pointers and dynamically allocating memory.
Conclusion
- LinkedList is a Linear Data Structure which don’t store elements at contiguous memory locations.
- A node contains two fields data, next field to store the reference of next node.
- Node is just a blueprint of structure.
- LinkedList in C is a preferred Data Structure because of its efficient insertion and deletion.
- Doubly LinkedList, Circular LinkedList are variation of Singly linked list implementation in c.