In this blog post will study what is singly linked lists, its
applications, operations and implementation in C++.
Please check the blog post if you don't know what is
Linked Lists.
Definition
Generally "linked list" means a singly linked list. It is a
one way list because we can only traverse this list in one
direction, starting from the head node to the end.
Each node in SLL consists of 1 data field and 1 reference
field. As we have only one reference pointer pointing to the
next node, we can only traverse in one direction starting from
the head node to the end.
The link of the last node in the list is NULL, which indicates
the end of the list.
Applications
-
SLL are mostly used in implementing stacks, queues and hash
map.
- Message delivery on network.
- Hyperlinks between different sections of pages in PDFs.
- To evaluate a polynomial expression.
-
Functionality of brain in mugging up answers(We clearly know
what word would come after any specific sentence).
Operations
Do refer the code available the end of this section to
understand the following theory.
Note that the head node's data is used to count the no. of nodes
in the list & it's next pointer points to the first node.
1. Traverse: Iterate through the nodes in the
linked list starting from the head node.
-
Let us assume that the head points to the first node of the
list.
-
Create a temporary node which will point to the same node as
that of head.
-
Follow the pointers and stop when the next pointer points to
NULL.
2. Append: Add a new node to the end of a
list.
-
We need to modify two next pointers (last nodes next pointer
and new nodes next pointer).
-
Traverse to the last node and change the next pointer, to
point to the new node.
- Change the new node's next pointer, to point to NULL.
3. Prepend: Add a new node to the beginning
of a list.
-
Only one next pointer needs to be modified (new node’s next
pointer) and it can be done in two steps.
-
Update the next pointer of new node, to point to the current
head.
- Update head pointer to point to the new node.
4. Insert : Add a new node to a specific
position on the list.
-
If a node has to be inserted at a given position then we
need to modify two next pointers.
-
If we want to add an element at position 3 then we stop at
position 2. That means we traverse 2 nodes and insert the
new node. For simplicity let us assume that the second node
is called position node.
-
Make the new node's next pointer point to the the next
pointer of position node.
-
Change the next pointer of the position node, to point to
the new node.
5. Delete at front: Remove the first node
from the list.
-
Move the head node's pointer(first node) to the next node
and the front node will be unlinked with the list.
5. Delete at rear: Remove the last node from
the list.
-
We need to find the node which is previous of the last node,
so traverse the list and while traversing maintain the
previous node address also.
-
By the time we reach the end of the list, we will have two
pointers, one pointing to the tail node and the other
pointing to the node before the tail node.
-
Update previous node’s next pointer with NULL and the last
node will be unlinked with the list.
5. Delete: Remove a node at specified
position from the list.
-
In this case, the node to be removed is always located
between two nodes. Head and tail links are not updated in
this case and such a removal can be done in two steps
-
Similar to the previous case, maintain the previous node
while traversing the list. Once we find the node to be
deleted, change the previous node’s next pointer to the next
pointer of the node to be deleted.
-
The current node to be deleted will be unlinked with the
list.
8. Display: Traverse and display the data of
all nodes starting from head node.
-
Traverse the linked list(1st operation) and at each node,
display its content
-
This traversing method can also be used to count the number
of nodes in the linked list.