Unit 3
Unit 3
Linked List
1. Linked List:
A linked list is a sequence of data structures (node), which are connected together
via links.
Linked List is linear data structures where the elements are not stored in
contiguous locations and every element is a separate object with two fields.
Each element is known as a node. Every node in linked list has two fields:
1. Data
2. Next
Data part of the node stores actual information that is to be represented by the node
The next field of every node is contains address of the next node in list. The next
field of last node is always NULL.
The last node in the list is identified by the null pointer which is present in the
address part of the last node.
1. Create A node
2. Insert at beginning
3. Insert at end
4. Insert after specific node
5. Delete first node
6. Delete last node
7. Delete specific node
8. Traverse a list
9. Searching in a list
Create a node:
Structure of node with two fields can be defined using class as follows:
Class node
int data;
node next;
public : node()
{
Next=null;
};
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.
There are the following steps which need to be followed in order to inser 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.
Algorithm
o Step 1: IF NEW = NULL
Write OVERFLOW
Go to Step 5
[END OF IF]
In order to insert a new node at the last, there are two following scenarios which
need to be mentioned.
Algorithm
o Step 1: IF NEW = NULL
Write OVERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
Algorithm
o STEP 1: IF NEW = NULL
WRITE OVERFLOW
GOTO STEP 12
END OF IF
Deletion
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]
There are two scenarios in which, a node is deleted from the end of the linked list.
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be
deleted.
Assign head → next = NULL will survive and therefore, the only node head of the
list will be assigned to null.
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
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.
Algorithm
o STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
o STEP 8: I = I+1
END OF LOOP
Traversing :
Algorithm
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL
[END OF LOOP]
o STEP 7: EXIT
Searching :
Algorithm
o Step 1: SET PTR = HEAD
o Step 2: Set I = 0
o STEP 3: IF PTR = NULL
write i+1
End of IF
o STEP 6: I = I + 1
o STEP 7: PTR = PTR → NEXT
[END OF LOOP]
o STEP 8: EXIT
Doubly linked list is a complex type of linked list in which a node contains a
link 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,
link to the next node in sequence, link to the previous node.
A sample node in a doubly linked list is shown in the figure.
We can traverse the list in this way until we find any node containing null or -1 in
its next part
[END OF IF]
Insertion at end:
Algorithm
o Step 1: IF NEW_NODE = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
[END OF LOOP]
Write OVERFLOW
Go to Step 12
[END OF IF]
o Step 2: SET NEW_NODE . DATA = VAL
o Step 3: SET TEMP = HEAD
o Step 4: SET I = 0
o Step 5: REPEAT 8 and 9 until I<="" Specified value "">
o Step 6: SET TEMP = TEMP . NEXT
o Step 7: I++
o Step 8: SET NEW_NODE . NEXT = TEMP . NEXT
o Step 9: SET NEW_NODE . PREV = TEMP
o Step 10 : SET TEMP . NEXT = NEW_NODE
o Step 11: SET TEMP . NEXT . PREV = NEW_NODE
o Step 12: EXIT
Deleting at Beginning:
Algorithm
o STEP 1: IF HEAD = NULL
WRITE EMPTY_LIST
GOTO STEP 6
Deleting at End:
Algorithm:
o Step 1: IF HEAD = NULL
Write EMPTY_LIST
Go to Step 7
[END OF IF]
[END OF LOOP]
o Step 5: SET TEMP .PREV. NEXT = NULL
o Step 6: FREE TEMP
o Step 7: EXIT
Write UNDERFLOW
Go to Step 9
[END OF IF]
[END OF LOOP]
WRITE "UNDERFLOW"
GOTO STEP 8
[END OF IF]
return i
[END OF IF]
o Step 6: i = i + 1
o Step 7: PTR = PTR → next
o Step 8: Exit
Traversing:
Algorithm:
o Step 1: IF HEAD == NULL
WRITE "UNDERFLOW"
GOTO STEP 6
[END OF IF]
Insertion aafter specific node and Delting a specific node operations are
same as that of singly linked list. Refer given earlier notes for these
operation.
SN Operation Description
1 Insertion at Adding a node into circular singly linked list at the beginning.
beginning
2 Insertion at the end Adding a node into circular singly linked list at the end.
3 Deletion at beginning Removing the node from circular singly linked list at the beginning.
4 Deletion at the end Removing the node from circular singly linked list at the end.
5 Searching Compare each element of the node with the given item and return
the location at which the item is present in the list otherwise return null.
6 Traversing Visiting each element of the list at least once in order to perform some
specific operation.
Insertion at beginning:
Algorithm:
o Step 1: IF NEW_NODE = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
[END OF LOOP]
Insertion at end:
Algorithm:
o Step 1: IF NEW_NODE = NULL
Write OVERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
Deleting at end:
Algorithm:
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
Traversing:
Algorithm:
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL
[END OF LOOP]
o STEP 7: EXIT
A linked list can typically only be accessed via its head node. From there you can
only traverse from node to node until you reach the node you seek. Thus traverse
is O(n).
Searching for a given value in a linked list similarly requires traversing all the
elements until you find that value. Thus search is O(n).
Inserting into a linked list requires re-pointing the previous node (the node before
the insertion point) to the inserted node, and pointing the newly-inserted node to
the next node. Thus insertion is O(1).
Deleting from a linked list requires re-pointing the previous node (the node before
the deleted node) to the next node (the node after the deleted node). Thus deletion
is O(1).
8.