Singly Linked List
Singly Linked List
DATA STRUCTURE
Topic Name-
Singly Linked List Presenter Name
Yash Malviya
6.12.2022 2
CONTENT
Introduction
Representation
Creation
Traversal
Insertion
Search
Deletion
Summary
SINGLY LINKED LIST
Introduction
What is a linked list?
A linked list is a linear data structure, in which the
elements are not stored at contiguous memory
locations.
6.12.2022 3
4
TOPIC ONE
Singly Linked List
Singly Linked List
What is a singly linked list?
5
Representation Of Linked List
1. Static , Sequential or Array Representation.
The linked list will maintained or represented by two parallel arrays of same size.
One array for the actual data item is called info field of the node.
Other array for the address called the next field of the nodes.
6
Representation of Linked List 2
7
OPERATION ON SINGLY LINKED LIST
Creating Singly Linked List:
1.Create a class Node which has two attributes: data and next. Next is a pointer to the next node.
2.Create another class which has two attributes: head and tail.
3.Add Node() will add a new node to the list:
Create a new node.
It first checks, whether the head is equal to null which means the list is empty.
If the list is empty, both head and tail will point to the newly added node.
If the list is not empty, the new node will be added to end of the list such that tail's next will point to the newly
added node. This new node will become the new tail of the list.
4.display() will display the nodes present in the list:
Define a node current which initially points to the head of the list.
Traverse through the list till current points to null.
Display each node by making current to point to node next to it in each iteration.
9/3/20XX 8
Traversal of 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.
ptr = head;
while (ptr!=NULL)
{
ptr = ptr -> next;
}
9
Insertion
Insertion of an element into the linked list at various position :
Insertion of a node into a linked list require a free node in which the
information can be inserted and then the node can be inserted and then
the node can inserted into a linked list.
We have three types of insertion in a linked list:
Before header or at beginning.
At the end of list.
At the specified position.
Insertion.2
14
Insertion at Specified Position
• To insert a given data at a specified position, the below algorithm is to
be followed:
• Traverse the Linked list up to position-1 nodes.
• Once all the position-1 nodes are traversed, allocate memory and the
given data to the new node.
• Point the next pointer of the new node to the next of current node.
• Point the next pointer of current node to the new node.
15
Algorithm
• STEP 1: IF PTR = NULL
• WRITE OVERFLOW
GOTO STEP 12
END OF IF
• STEP 2: SET NEW_NODE = PTR
• STEP 3: NEW_NODE → DATA = VAL
• STEP 4: SET TEMP = HEAD
• STEP 5: SET I = 0
• STEP 6: REPEAT STEP 5 AND 6 UNTIL I
• STEP 7: TEMP = TEMP → NEXT
• STEP 8: IF TEMP = NULL
• WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
END OF LOOP
16
Deletion of an element in singly liked list
• If the node be deleted, that element should be search all over the list
still the node find, Then it should be deleted.
17
Deletion 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.
1.ptr = head;
2. head = ptr->next;
• Now, free the pointer ptr which was pointing to the head node of the list. This will be
done by using the following statement.
1.free(ptr)
18
Algorithm
• Step 1: IF HEAD = NULL
• Write UNDERFLOW
Go to Step 5
[END OF IF]
• Step 2: SET PTR = HEAD
• Step 3: SET HEAD = HEAD ->
NEXT
• Step 4: FREE PTR
• Step 5: EXIT
19
Deletion at End
• Steps required for deleting the node:-
• If the Linked list has only one node then make head node null
• Else traverse to the end of the linked list
• While traversing store the previous node i.e. 2nd last node
• Change the next of 2nd last node to null
• Free/delete memory of the the last node
• Now, 2nd last node becomes the last node.
20
Algorithm
• Step 1: IF HEAD = NULL
• Write UNDERFLOW
Go to Step 8
[END OF IF]
• Step 2: SET PTR = HEAD
• Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!=
NULL
• Step 4: SET PREPTR = PTR
• Step 5: SET PTR = PTR -> NEXT
• [END OF LOOP]
• Step 6: SET PREPTR -> NEXT = NULL
• Step 7: FREE PTR
• Step 8: EXIT
21
Deletion at specific position
• If the head is NULL, return.
• Create a node temp and make it point to the head.
• If the position is 0, it means that we have to delete the head of the list. We can easily do that by head =
temp → next and free(temp). As temp was pointing to the head, we have incremented the head and
freed temp.
• If the position is not 0, we will proceed. Now, we will write a loop with i = 0 to i < (position -1). As we
have discussed, to delete a node, we need a pointer to its previous node. So while traversing the loop,
we will increment temp.
• Now, after the loop ends, if the temp is NULL or temp → next is NULL, we will simply return as the
position is more than the number of nodes in the linked list.
• If the above condition fails, we will have temp pointing to the (position – 1)th node. Now, we simply
have to change the required pointers.
• We will save the temp → next → next in a new node next1. Now, we will free temp → next, as it is the
node at the given position. In the end, temp → next = next1. – In this way, there is no unnecessary
memory leak and the node at the given position is getting deleted successfully.
22
Algorithm
• STEP 1: IF HEAD = NULL
• WRITE UNDERFLOW
GOTO STEP 10
END OF IF
• STEP 2: SET TEMP = HEAD
• STEP 3: SET I = 0
• STEP 4: REPEAT STEP 5 TO 8 UNTIL I
• STEP 5: TEMP1 = TEMP
• STEP 6: TEMP = TEMP → NEXT
• STEP 7: IF TEMP = NULL
• WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
• STEP 8: I = I+1
• END OF LOOP
• STEP 9: TEMP1 → NEXT = TEMP → NEXT
• STEP 10: FREE TEMP
• STEP 11: EXIT 23
Summary
24
6.12.2022 25
THANK YOU