Linked Lists
Linked Lists
Linked List is a linear data structure, in which elements are not stored at a
contiguous location, rather they are linked using pointers. Linked List forms a
series of connected nodes, where each node stores the data and the address of
the next node.
A Linked List is a linear data structure that is used to store a collection of data
with the help of nodes. Please remember the following points before moving
forward.
• The consecutive elements are connected by pointers / references.
• The last node of the linked list points to null.
• The entry point of a linked list is known as the head.
• The common variations of linked lists are Singly, Doubly, Singly
Circular and Doubly Circular.
Advantages of Linked Lists (or Most Common Use Cases):
• Linked Lists are mostly used because of their effective insertion and
deletion. We only need to change few pointers (or references) to insert
(or delete) an item in the middle
• Insertion and deletion at any point in a linked list take O(1) time.
Whereas in an array data structure, insertion / deletion in the middle
takes O(n) time.
• This data structure is simple and can be also used to implement a
stack, queues, and other abstract data structures.
• Implementation of Queue and Deque data structures : Simple array
implementation is not efficient at all. We must use circular array to
efficiently implement which is complex. But with linked list, it is easy
and straightforward. That is why most of the language libraries use
Linked List internally to implement these data structures..
• Linked List might turn out to be more space efficient compare to
arrays in cases where we cannot guess the number of elements in
advance. In case of arrays, the whole memory for items is allocated
together. Even with dynamic sized arrays like vector in C++ or list in
Python or ArrayList in Java. the internal working involves de-allocation
of whole memory and allocation of a bigger chunk when insertions
happen beyond the current capacity.
Applications of Linked Lists:
• Linked Lists can be used to implement stacks, queue, deque, sparse
matrices and adjacency list representation of graphs.
• Dynamic memory allocation in operating systems and compilers
(linked list of free blocks).
• Manipulation of polynomials
• Arithmetic operations on long integers.
• In operating systems, they can be used in Memory management,
process scheduling (for example circular linked list for round robin
scheduling) and file system.
• Algorithms that need to frequently insert or delete items from large
collections of data.
• LRU cache, which uses a doubly linked list to keep track of the most
recently used items in a cache.
Applications of Linked Lists in real world:
• The list of songs in the music player are linked to the previous and
next songs.
• In a web browser, previous and next web page URLs can be linked
through the previous and next buttons (Doubly Linked List)
• In image viewer, the previous and next images can be linked with the
help of the previous and next buttons (Doubly Linked List)
• Circular Linked Lists can be used to implement things in round
manner where we go to every element one by one.
• Linked List are preferred over arrays for implementations of Queue
and Deque data structures because of fast deletions (or insertions) from
the front of the linked lists.
Disadvantages of Linked Lists:
Linked lists are a popular data structure in computer science, but like any other
data structure, they have certain disadvantages as well. Some of the key
disadvantages of linked lists are:
• Slow Access Time: Accessing elements in a linked list can be slow, as
you need to traverse the linked list to find the element you are looking
for, which is an O(n) operation. This makes linked lists a poor choice
for situations where you need to access elements quickly.
• Pointers or References: Linked lists use pointers or references to
access the next node, which can make them more complex to
understand and use compared to arrays. This complexity can make
linked lists more difficult to debug and maintain.
• Higher overhead: Linked lists have a higher overhead compared to
arrays, as each node in a linked list requires extra memory to store the
reference to the next node.
• Cache Inefficiency: Linked lists are cache-inefficient because the
memory is not contiguous. This means that when you traverse a linked
list, you are not likely to get the data you need in the cache, leading to
cache misses and slow performance.
Linked List vs Array
Linked List: Linked lists are less rigid in their storage structure and elements are
usually not stored in contiguous locations, hence they need to be stored with
additional tags giving a reference to the next element.
Types Of Linked Lists:
The node contains a pointer to the next node means that the node stores the
address of the next node in the sequence. A single linked list allows the traversal
of data only in one way
Therefore, it contains three parts of data, a pointer to the next node, and a
pointer to the previous node. This would enable us to traverse the list in the
backward direction as well.
3. Circular Linked List
A circular linked list is a type of linked list in which the last node’s next pointer
points back to the first node of the list, creating a circular structure. This design
allows for continuous traversal of the list, as there is no null to end the list.
While traversing a circular linked list, we can begin at any node and traverse the
list in any direction forward and backward until we reach the same node we
started. Thus, a circular linked list has no beginning and no end. Below is the
image for the same:
A header linked list is a special type of linked list which consists of a header node
in the beginning of the list. The header node can store meta data about the
linked list. This type of list is useful when information other than that found in
each node is needed. For example, suppose there is an application in which
the number of items in a list is often calculated. Usually, a list is always
traversed to find the length of the list. However, information can be easily
obtained if the current length is maintained in an additional header node.
It is a list whose last node contains the NULL pointer. In the header linked list
the start pointer always points to the header node. start -> next =
NULL indicates that the header linked list is empty. The operations that are
possible on this type of linked list are Insertion, Deletion, and Traversing. This
structure can be implemented using a singly linked list or a doubly linked
list.When using a doubly linked list, the header node
simplifies bidirectional traversal and manipulation of the list, further
enhancing the efficiency of operations like insertion and deletion.
2. Singly or Doubly Circular Header Linked List
A list in which the last node points back to the header node is called circular
header linked list. The chains do not indicate first or last nodes. In this case,
external pointers provide a frame of reference because last node of a circular
linked list does not contain the NULL pointer. The possible operations on this
type of linked list are Insertion, Deletion and Traversing.