In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. The six operations of linked list is explained in this slide
2. Inserting a node into a SLL
There are many ways you might want to
insert a new node into a list:
As the new first element
As the new last element
In the middle of the two nodes or in some
designated position.
Shashankshetty@nitte.edu.in
3. 25
1) Insert a node at the beginning of the list
// Algorithm : Insertion (Item)
// Description : Inserts a node at the beginning of
the SLL
Step1: Start
Step 2: NewNode=getnode()
NewNode -> link = NULL
NewNode -> info = Item
Step 3: [ If the Linked list is empty, then the New
Node Created is a first node or Head Node]
if(Head==NULL)
Head=NewNode
return
End if
Step 4: [If the linked list is not empty]
NewNode -> link= Head
Head= NewNode
Free(NewNode)
Step 5: Return (Stop)
Item NULL
NewNode
Head
NULL
4 6 NULL
Head
254 6 NULLItem
Head
Shashankshetty@nitte.edu.in
4. 2) Insert a node at the End of the list
// Algorithm : Insertion (Item)
// Description : Inserts a node at the End of
the SLL
Step1: Start
Step 2: NewNode=getnode()
NewNode -> link = NULL
NewNode -> info = Item
Step 3: [ If the Linked list is empty, then the
New Node Created is a first node or Head
Node]
if(Head==NULL)
Head=NewNode
return
End if
Step 4: [If the linked list is not empty]
Cur= Head
While (Cur-> link !=NULL)
Cur=Cur->link
end while
Step 5: Cur -> link= NewNode
Step 6: Free(NewNode)
Free (Cur)
Step 5: Return (Stop)
25
Item NULL
NewNode
Head
NULL
4 6 NULL
Head
625 item NULL4
Head
Cur Cur
Shashankshetty@nitte.edu.in
5. Shashankshetty@nitte.edu.in
3) Insert a node at the middle of two nodes or at
the designated position
// Algorithm : Insertion (Item, position)
// Description : Inserts a node at the middle or at the
designated position of the SLL
Step1: Start
Step 2: NewNode=getnode()
NewNode -> link = NULL
NewNode -> info = Item
Step 3: [ If the Linked list is empty, then the New Node
Created is a first node or Head Node]
if(Head==NULL)
Head=NewNode
return
End if
Step 4: [If the linked list is not empty]
Cur= Head
While (Cur!=position-1)
Cur=Cur->link
end while
Cur1=Cur->link
Step 5: Cur->link= NewNode
NewNode->link= Cur1
Step 6: Free(NewNode)
Free (Cur)
Free(Cur1)
Step 5: Return (Stop)
25
Item NULL
NewNode
Head
NULL
4 6 NULL
Head
625 item NULL4
Head
Cur Cur
1 2 3
Let Us insert
the new
node at
position 3!!!
Cur1
6. Shashankshetty@nitte.edu.in
Deleting a node from SLL
There are many ways you might want to
delete a new node from a list:
Delete first element
Delete last element
From the middle of the two nodes or in some
designated position.
7. Shashankshetty@nitte.edu.in
25
1) Delete a node from the beginning of the list
// Algorithm : Deletion (Item)
// Description : Delete a node from the beginning of
the SLL
Step1: Start
Step 2: [If Empty List]
If (Head==NULL)
Display (“List Empty”)
Return
EndIf
Step 3: [If the linked list is not empty]
Temp=Head
Head=Head-> link
display (temp->info)
Free(Temp)
Step 4: Return (Stop)
Head
NULL
4 6 NULL
Head
25 6 NULL
Head
List
Empty
Temp
8. Shashankshetty@nitte.edu.in
25
2) Delete a node from the End of the list
// Algorithm : Deletion (Item)
// Description : Delete a node from the end of the
SLL
Step1: Start
Step 2: [If Empty List]
If (Head==NULL)
Display (“List Empty”)
Return
EndIf
Step 3: [If the linked list is not empty]
Temp=Head
While(temp->link!=NULL)
temp1=temp
temp=temp->link
end while
temp1->next=NULL
display (temp->info)
Free(Temp)
Step 4: Return (Stop)
Head
NULL
4 6 NULL
Head
NULL
Temp1
List
Empty
Temp TempTemp1 Temp
NULL
25
Head
4
9. Shashankshetty@nitte.edu.in
3) Delete a node from the Middle positionor from
specified position
// Algorithm : Deletion (Item, position)
// Description : Delete a node from the
specified position of the SLL
Step1: Start
Step 2: If(Position<=0 or position>length)
Display(“Node position doenot exist”)
EndIf
Step 3: [If Empty List]
If (Head==NULL)
Display (“List Empty”)
Return
EndIf
Step 3: [If the linked list is not empty]
Temp=Head
While(temp !=pos)
temp1=temp
temp=temp->link
end while
temp1->next=temp->next
display (temp->info)
Free(Temp)
Step 4: Return (Stop)