Linked List
o Linked List can be defined as collection of objects called nodes that are randomly stored in the memory.
o A node contains two fields i.e. data stored at that particular address and the pointer which contains the address of the
next node in the memory.
o The last node of the list contains pointer to the null.
Uses of Linked List
o The list is not required to be contiguously present in the memory. The node can reside anywhere in the memory and
linked together to make a list. This achieves optimized utilization of space.
o list size is limited to the memory size and doesn't need to be declared in advance.
o Empty node cannot be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.
Why use linked list over array?
Till now, we were using array data structure to organize the group of elements that are to be stored individually in the memo
memory.
However, Array has several advantages and disadvantages which must be known in order to decide the data structure which
will be used throughout the program.
Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting any element in the array needs
shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array. Using linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non
non-contiguously
contiguously stored in the memory and linked
together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of declaration. List grows as per the
program's demand and limited to the available memory space.
Singly linked list
Singly linked list can be defined as the collection of ordered set of elements. The number of elements may vary accordi
according to
need of the program. A node in the singly linked list consists of two parts: data part and link part. Data part of the node stores
actual information that is to be represented by the node while the link part of the node stores the address of its imme immediate
successor.
One way chain or singly linked list can be traversed only in one direction. In other words, we can say that each node contain
contains
only next pointer, therefore we cannot traverse the list in the reverse direction.
Consider an example where the marks obtained by the student in three subjects are stored in a linked list as shown in the
figure.
In the above figure, the arrow represents the links. The data part of every node contains the marks obtained by the student iin
the different subject. The last node in the list is identified by the null pointer which is present in the address part of the last
node. We can have as many elements we require, in the data part of the list.
Operations on Singly Linked List
There are various operations which can be performed on singly linked list. A list of all such operations is given below.
Insertion
The insertion into a singly linked list can be performed at different positions. Based on the position of the new node being
inserted, the insertion is categorized into the following categories.
SN Operation Description
1 Insertion at It involves inserting any element at the front of the list. We just need to a few link
beginning adjustments to make the new node as the head of the list.
2 Insertion at end It involves insertion at the last of the linked list. The new node can be inserted as the only
of the list node in the list or it can be inserted as the last one. Different logics are implemented in each
scenario.
3 Insertion after It involves insertion after the specified node of the linked list. We need to skip the desired
specified node number of nodes in order to reach the node after which the new node will be inserted. .
Deletion and Traversing
The Deletion of a node from a singly linked list can be performed at different positions. Based on the position of the node being
deleted, the operation is categorized into the following categories.
SN Operation Description
1 Deletion at It involves deletion of a node from the beginning of the list. This is the simplest operation
beginning among all. It just need a few adjustments in the node pointers.
2 Deletion at the It involves deleting the last node of the list. The list can either be empty or full. Different
end of the list logic is implemented for the different scenarios.
3 Deletion after It involves deleting the node after the specified node in the list. we need to skip the desired
specified node number of nodes to reach the node after which the node will be deleted. This requires
traversing through the list.
4 Traversing In traversing, we simply visit each node of the list at least once in order to perform some
specific operation on it, for example, printing data part of each node present in the list.
5 Searching In searching, we match each element of the list with the given element. If the element is
found on any of the location then location of that element is returned otherwise null is
returned. .
Traversing in singly linked list
Traversing is the most common operation that is performed in almost every scenario of singly linked list. Traversing means
visiting each node of the list once in order to perform some operation on that. This will be done by using the following
statements.
Algorithm
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 7
END OF IF
o STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL
o STEP 5: PRINT PTR→ DATA
o STEP 6: PTR = PTR → NEXT
[END OF LOOP]
o STEP 7: EXIT
Insertion in singly linked list at beginning
Inserting a new element into a singly linked list at beginning is quite simple. We just need to make a few adjustments in the
node links. There are the following steps which need to be followed in order to insert a new node in the list at beginning.
o Allocate the space for the new node and store data into the data part of the node. This will be done by the following
statements.
o Make the link part of the new node pointing to the existing first node of the list. This will be done by using the following
statement.
o At the last, we need to make the new node as the first node of the list this will be done by using the following statement.
Algorithm
o Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
o Step 2: SET NEW_NODE = AVAIL
o Step 3: SET AVAIL = AVAIL → NEXT
o Step 4: SET NEW_NODE → DATA = VAL
o Step 5: SET NEW_NODE → NEXT = HEAD
o Step 6: SET HEAD = NEW_NODE
o Step 7: EXIT
Insertion in singly linked list at the end
Algorithm
o Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
o Step 2: SET NEW_NODE = AVAIL
o Step 3: SET AVAIL = AVAIL - > NEXT
o Step 4: SET NEW_NODE - > DATA = VAL
o Step 5: SET NEW_NODE - > NEXT = NULL
o Step 6: SET PTR = HEAD
o Step 7: Repeat Step 8 while PTR - > NEXT != NULL
o Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
o Step 9: SET PTR - > NEXT = NEW_NODE
o Step 10: EXIT
Insertion in singly linked list after specified Node
o In order to insert an element after the specified number of nodes into the linked list, we need to skip the desired number
of elements in the list to move the pointer at the position after which the node will be inserted.
Algorithm
o STEP 1: IF AVAIL = NULL
WRITE OVERFLOW
GOTO STEP 9
END OF IF
o STEP 2: SET NEW_NODE = AVAIL
o Step 3: SET AVAIL = AVAIL - > NEXT
o STEP 4: NEW_NODE → DATA = VAL
o Step 4: SET PTR = HEAD
o Step 5: Repeat Step 6 while PTR - > DATA != NUM
o Step 6: PTR=PTR→NEXT
o END OF LOOP
o Step 7: NEW_NODE→NEXT= PTR→NEXT
o Step 8: PTR→NEXT = NEW_NODE
o STEP 9: EXIT
Insertion in singly linked list before specified Node
o In order to insert an element after the specified number of nodes into the linked list, we need to skip the desired number
of elements in the list to move the pointer at the position before which the node will be inserted.
Algorithm
o STEP 1: IF AVAIL = NULL
WRITE OVERFLOW
GOTO STEP 12
END OF IF
o STEP 2: SET NEW_NODE = AVAIL
o Step 3: SET AVAIL = AVAIL - > NEXT
o STEP 4: NEW_NODE → DATA = VAL
o Step 5: SET PTR = HEAD
o Step 6: SET PREPTR = HEAD
o
o Step 7: Repeat Step 8 and 9 while PTR - > DATA != NUM
o Step 8: PREPTR=PTR
o Step 9: PTR=PTR→NEXT
o END OF LOOP
o Step 10: NEW_NODE→NEXT= PTR
o Step 11: PREPTR→NEXT = NEW_NODE
o STEP 12: EXIT
Deletion
The Deletion of a node from a singly linked list can be performed at different positions. Based on the position of the node being
deleted, the operation is categorized into the following categories.
Deletion in singly linked list at beginning
Deleting a node from the beginning of the list is the simplest operation of all. It just need a few adjustments in the node
pointers. Since the first node of the list is to be deleted, therefore, we just need to make the head, point to the next of the
head. This will be done by using the following statements.
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
o Step 2: SET PTR = HEAD
o Step 3: SET HEAD = HEAD -> NEXT
o Step 4: FREE PTR
o Step 5: EXIT
Deletion in singly linked list at the end
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
o Step 2: SET PTR = HEAD
o Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
o Step 4: SET PREPTR = PTR
o Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
o Step 6: SET PREPTR -> NEXT = NULL
o Step 7: FREE PTR
o Step 8: EXIT
Deletion in singly linked list after the specified node :
In order to delete the node, which is present after the specified node, we need to skip the desired number of nodes to reach the
node after which the node will be deleted. We need to keep track of the two nodes. The one which is to be deleted the other
one if the node which is present before that node. For this purpose, two pointers are used: ptr and ptr1.
Use the following statements to do so.
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 8
END OF IF
Step 2: SET PTR = HEAD
Step 3: Repeat Step 4 while PTR - > DATA != NUM
STEP 4: PTR=PTR→NEXT
[END OF LOOP]
STEP 5: TEMP = PTR → NEXT
STEP 6: PTR→NEXT=TEMP → NEXT
STEP 7: FREE TEMP
STEP 8: EXIT
Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in
the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence
(next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following image.
In C, structure of a node in doubly linked list can be given as :
In a singly linked list, we could traverse only in one direction, because each node contains address of the next node and it
doesn't have any record of its previous nodes. However, doubly linked list overcome this limitation of singly linked list. Due to
the fact that, each node of the list contains the address of its previous node, we can find all the details about the previous node
as well by using the previous address stored inside the previous part of each node.
Memory Representation of a doubly linked list
Memory Representation of a doubly linked list is shown in the following image. Generally, doubly linked list consumes more
space for every node and therefore, causes more expansive basic operations such as insertion and deletion. However, we can
easily manipulate the elements of the list since the list maintains pointers in both the directions (forward and backward).
In the following image, the first element of the list that is i.e. 13 stored at address 1. The head pointer points to the starting
address 1. Since this is the first element being added to the list therefore the prev of the list contains null. The next node of
the list resides at address 4 therefore the first node contains 4 in its next pointer.
We can traverse the list in this way until we find any node containing null or -1 in its next part.
Insertion in doubly linked list at beginningAlgorithm :
o Step 1: IF head = NULL
Write UNDERFLOW
Go to Step 9
o
[END OF IF]
Step 2: SET PTR = HEAD
o Step 3: SET NEW_NODE -> DATA = VAL
o Step 4: SET NEW_NODE -> PREV = NULL
o Step 5: SET NEW_NODE -> NEXT = ptr
o Step 6: SET PTR=PTR->NEXT
o Step 7: SET PTR->PREV=NEW_NODE
o Step 8: SET head = NEW_NODE
o Step 9: EXIT
As in doubly linked list, each node of the list contain double pointers therefore we have to maintain more number of pointers in
doubly linked list as compare to singly linked list.
There are two scenarios of inserting any element into doubly linked list. Either the list is empty or it contains at least one
element. Perform the following steps to insert a node in doubly linked list at beginning.
Insertion in doubly linked list at the end
In order to insert a node in doubly linked list at the end, we must make sure whether the list is empty or it contains any
element. Use the following steps in order to insert the node in doubly linked list at the end.
Algorithm
o Step 1: IF HEAD = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET NEW_NODE -> DATA = VAL
o Step 3: SET NEW_NODE -> NEXT = NULL
o Step 4: SET TEMP = HEAD
o Step 5: Repeat Step 6 while TEMP -> NEXT != NULL
o Step 6: SET TEMP = TEMP -> NEXT
[END OF LOOP]
o Step 7: SET TEMP -> NEXT = NEW_NODE
o Step 8: SET NEW_NODE -> PREV = TEMP
o Step 9: EXIT
Insertion in doubly linked list after Specified node
In order to insert a node after the specified node in the list, we need to skip the required number of nodes in order to reach the
mentioned node and then make the pointer adjustments as required.
o STEP 1: IF AVAIL = NULL
WRITE OVERFLOW
GOTO STEP 15
END OF IF
o STEP 2: SET NEW_NODE = AVAIL
o Step 3: SET AVAIL = AVAIL - > NEXT
o STEP 4: NEW_NODE → DATA = VAL
o Step 4: SET PTR = HEAD
o Step 5: Repeat Step 6 while PTR - > DATA != NUM
o Step 6: PTR=PTR→NEXT
o END OF LOOP
o Step 11: SET NEW_NODE -> NEXT = PTR -> NEXT
o Step 12: SET NEW_NODE -> PREV = PTR
o Step 13 : SET PTR -> NEXT = NEW_NODE
o Step 14: SET NEW_NODE -> NEXT -> PREV = NEW_NODE
o Step 15: EXIT
o
Insertion in doubly linked list before Specified node
In order to insert a node after the specified node in the list, we need to skip the required number of nodes in order to reach the
mentioned node and then make the pointer adjustments as required.
o STEP 1: IF AVAIL = NULL
WRITE OVERFLOW
GOTO STEP 13
END OF IF
o STEP 2: SET NEW_NODE = AVAIL
o Step 3: SET AVAIL = AVAIL - > NEXT
o STEP 4: NEW_NODE → DATA = VAL
o Step 5: SET PTR = HEAD
o Step 6: Repeat Step 7 while PTR - > DATA != NUM
o Step 7: PTR=PTR→NEXT
o END OF LOOP
o Step 8: SET NEW_NODE -> NEXT = PTR
o Step 9: SET NEW_NODE -> PREV = PTR->PREV
o Step 10 : SET PTR = PTR->PREV
o Step 11: SET PTR->NEXT=NEW_NODE
o Step 12: SET NEW_NODE->NEXT->PREVIOUS=NEW_NODE
o Step 13: EXIT
Deletion in doubly linked
Deletion in doubly linked list at the beginning
Deletion in doubly linked list at the beginning is the simplest operation. We just need to copy the head pointer to pointer ptr
and shift the head pointer to its next.
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
STEP 6: EXIT
Deletion in doubly linked list at the end
Deletion of the last node in a doubly linked list needs traversing the list in order to reach the last node of the list and then make
pointer adjustments at that position.
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL
Step 4: SET TEMP = TEMP->NEXT
[END OF LOOP]
Step 5: SET TEMP ->PREV-> NEXT = NULL
Step 6: FREE TEMP
Step 7: EXIT
Deletion in doubly linked list after the specified node
In order to delete the node after the specified data, we need to perform the following steps.
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
Step 4: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 5: SET PTR = TEMP -> NEXT
Step 6: SET TEMP -> NEXT = PTR -> NEXT
Step 7: SET PTR -> NEXT -> PREV = TEMP
Step 8: FREE PTR
Step 9: EXIT
Deletion in doubly linked list before the specified node
In order to delete the node after the specified data, we need to perform the following steps.
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
Step 4: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 5: SET PTR = TEMP -> PREV
Step 6: SET TEMP -> PREV = PTR -> PREV
Step 7: SET PTR -> PREV -> NEXT = TEMP
Step 8: FREE PTR
Step 9: EXIT
SINGLY LINKED LIST
ADVANTAGES:-
1) Insertion and Deletion can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) It space is not wasted as we can get space according to our requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available,even then we can store the data
in computer.
7) It is less expensive.
DISADVANTAGES:-
1) It requires more space as pointers are also stored with information.
2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go through all those elements that come
before that element.
4) we cannot traverse it from last, only we can traverse from the beginning.
5) It is not easy to sort the elements stored in the linear linked list.
DOUBLY LINKED LIST
ADVANTAGES:-
1) We can traverse in both direction i.e from starting to end & as well as from end to
starting.
2) It is easy to reverse the linked list.
3) If we are at a node,the we can go at any node.But in singly linked list,it is not possible
to reach the previous node.
DISADVANTAGES:-
1) It requires more space per space per node because extra field is required for pointer to
previous node.
20 Insertion and Deletion take more time than linear linked list because more pointer
operations are required than linear linked list.