Linked List - Tomekske/algorithms GitHub Wiki
A linked list is a linear data structure where elements, called nodes, are stored in separate objects. Each node contains a value and a reference or pointer to the next node in the sequence. The last node in the list points to null, indicating the end of the list.
- Head: The linked list is accessed through the head node, which points to the first node in the list
- Tail: The last node in the list points to NULL, indicating the end of the list. This node is known as the tail node.
- Node: A node in a linked list typically consists of two components
- Data: It holds the actual value or data associated with the node
- Next: It stores the memory address (reference) of the next node in the sequence
Usage
- Dynamic Size: Linked lists are ideal when you need a data structure that can grow or shrink dynamically based on runtime conditions
- Efficient Insertion and Deletion: If your application involves frequent insertions or deletions at the beginning or end of a sequence, linked lists provide efficient operations
- Memory Efficiency: Linked lists are memory-efficient when the size of the data is uncertain or varies. Each node in a linked list only requires memory for the value it holds and the pointers to the next (and possibly previous) nodes. This dynamic allocation makes linked lists suitable for situations where memory utilization is a concern.
- Implementation of Stacks and Queues: Linked lists are commonly used to implement stack and queue data structures
- Implementation of Hash Tables: Hash tables often use linked lists to handle collisions. When two or more elements map to the same hash value, linked lists are used to create a chain of elements at that location, allowing efficient retrieval and update operations.
- Educational Purposes: Linked lists are commonly taught as a fundamental data structure in computer science education.
Pros and Cons
Pros:
- Dynamic Size: Linked lists can grow or shrink dynamically, accommodating changes in the number of elements without requiring reallocation or resizing
- Efficient Insertion and Deletion: Adding or removing elements at the beginning or end of a linked list can be done in constant time O(1), as it only involves updating the appropriate pointers. This makes linked lists efficient for scenarios that involve frequent insertion or deletion operations.
- Memory Efficiency: Linked lists utilize memory efficiently, as they only require memory for the nodes themselves, along with the pointers. This dynamic allocation allows for efficient memory usage when the size of the list is uncertain or can change.
- Implementation Flexibility: Linked lists offer flexibility in terms of implementation variations. They can be implemented as singly linked lists, doubly linked lists, or circular linked lists, ... . Each variation provides different advantages and trade-offs depending on the specific requirements.
Cons:
- Lack of Random Access: Unlike arrays, linked lists do not provide direct random access to elements by index. Accessing an element at a specific position in a linked list requires traversing the list from the beginning, resulting in linear time complexity O(n) for access operations.
- Inefficient Search: Linked lists are not optimized for searching operations. To find an element within a linked list, you would need to traverse the list sequentially, comparing values until a match is found or the end of the list is reached.
- Additional Memory Overhead: Linked lists require extra memory for storing the pointers or references to the next (and possibly previous) nodes. This additional memory overhead can be a disadvantage in memory-constrained environments or scenarios where memory usage needs to be minimized.
- Sequential Access: Linked lists provide efficient sequential access to elements, allowing traversal from the head to the tail. However, accessing elements in a random or arbitrary order requires iterating through the list, which can be slower compared to direct random access provided by arrays.
Operations
- Insertion: Adds a new node to the linked list
- AddFirst: Adds a new node to the head of the linked list
- AddLast: Adds a new node at the end of the linked list
- AddAfter: Adds a new node or value after an existing node in the linked list
- AddBefore: Adds a new node or value before an existing node in the linked list
- Deletion: Removes a node
- Remove: Removes the specified node
- RemoveFirst: Removes the head node
- RemoveLast: Removes the tail node
- Update: Update the value of a node
- Traversal: Access each element of the linked list
- Search: Find a node in the linked list
- Sort: Sort the nodes of the linked list
Types of Lined Lists
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Doubly Circular Linked List
- Header Linked List