Linked List3
Linked List3
2 2
Insertion algorithms
• Inserting node at the beginning of the list (at first position)
• Insert a node after the node with a given location (in between)
• Insert a node at the end of the list (last position)
• ITEM – new information to be added to the list.
• Steps:
(a) Check is space available in the AVAIL list. If AVAIL = NULL print
Overflow
(b) Else remove first node from the AVAIL list. Assign it to he NEW
NEW:=AVAIL AVAIL:=LINK[AVAIL]
(c ) Copy new information into the new node. INFOR[NEW]:= ITEM
NEW
ITEM
AVAIL
Inserting at the beginning of a List
• INSFIRST(INFO,LINK,START,AVAIL,ITEM): This algorithm
inserts ITEM as the first node in the list.
1. [OVERFLOW?] If AVAIL = NULL then: write overflow and exit
6. Exit.
Inserting after a given node
• To insert after particular location (LOC) in a linked list.
• If LOC = NULL then list is empty and item is inserted as first node.
• Else LINK[NEW]:=LINK[LOC]
• LINK[LOC]:=NEW
Inserting after a given node
• INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM): This algorithm inserts ITEM so
that ITEM follows the node with location LOC or inserts ITEM as the first node when
LOC=NULL.
5. Exit.
Inserting into a sorted linked list
• Item must be inserted between nodes A and B so that
INFO(A) < ITEM <= INFO(B)
Procedure finds the location LOC of node A, that is, finds the location
LOC of the last node in LIST whose value is less than ITEM.
Inserting into a sorted linked list
FINDA(INFO, LINK, START, ITEM, LOC): This procedure finds the
location LOC of the last node in a sorted list such that INFO[LOC] < ITEM
or sets LOC = NULL
1. [List empty?] If START = NULL, then: Set LOC:=NULL and return
2. [Special case?] If ITEM < INFO[START], then set LOC:= NULL and
return
3. Set SAVE:= START and PTR:=LINK[START]. [Initializes pointer]
4. Repeat Steps 5 and 6 while PTR ≠ NULL
5. If ITEM < INFO[PTR], then Set LOC:= SAVE and return
[End of If Structure]
6. Set SAVE:=PTR and PTR:=LINK[PTR]. [Update Pointers.]
7. [End of Step 4 loop].
8. Set LOC:=SAVE
9. Return
Inserting into a sorted linked list
INSERT(INFO, LINK,START, AVAIL, ITEM): This algorithm inserts
ITEM into a sorted linked list.
1. [Use procedure FINDA to find the location of the node preceding
ITEM] Call FINDA(INFO, LINK, START, ITEM, LOC)
2. [OVERFLOW?] If AVAIL=NULL, then: write: Overflow and Exit
3. [Remove first node from AVAIL list] Set NEW:=AVAIL and
AVAIL:= LINK[AVAIL]
4. Set INFO[NEW]:=ITEM
5. If LOC:=NULL, then set LINK[NEW]:=START and
START:=NEW
6. Else Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW
7. [End of If structure]
8. Exit
Deletion Algorithms
• Deletion Algorithms:
•Delete the node in beginning of the list (first node).
•Delete the last node of the list.
•Delete the node with the given position.
Algorithm for deletion of the first
node:
delete_first(shead)
Step 1. IF shead == NULL
Step 2. PRINT “The linked list is empty”
Step 3. END OF IF
Step 4. ELSE
Step 5. temp = shead
Step 6. shead = ADDRESS(shead)
Step 7. DEALLOCATE MEMORY FOR temp
Step 8. END OF ELSE
Example:
• In figure, the first node with memory address “801”
is deleted from the singly linked list.
Algorithm for deletion of the last node:
delet_last(shead)
Step 1. IF shead==NULL
Step 2. PRINT “The linked list is empty”
Step 3. END OF IF
Step 4. ELSE
Step 5. temp1 = shead;
Step 6. WHILE ADDRESS(temp1) != NULL
Step 7. temp2 = temp1
Step 8. temp1 = ADDRESS(temp1)
Step 9. END OF WHILE
Step 10. IF temp1 == shead
Step 11. shead = NULL
Step 12. END OF IF
Step 13. ADDRESS(temp2) = NULL
Step 14. DEALLOCATE MEMORY FOR temp1
Step 15. END OF ELSE
Example:
• In figure, the last node with memory address “601”
is deleted from the singly linked list.
Algorithm for deletion of the node whose position is
inputed by the user:
delet_p(shead,pos)
Step 1. IF shead == NULL
Step 2. PRINT “The linked list is empty “
Step 3. END OF IF
Step 4. ELSE
Step 5. temp1 = shead;
Step 6. IF pos == 1
Step 7. shead = ADDRESS(shead)
Step 8. DEALLOCATE MEMORY FOR temp1
Step 9. END OF IF
Step 10. ELSE
Step 11. WHILE count < pos AND ADDRESS(temp1)!= NULL
Continue..
Step 12. temp2 = temp1
Step 13. temp1 = ADDRESS(temp1)
Step 14. count = count + 1
Step 15. END OF WHILE
Step 16. IF pos == count
Step 17. ADDRESS(temp2) = ADDRESS(temp1)
Step 18. DEALLOCATE MEMORY FOR temp1
Step 19. END OF IF
Step 20. ELSE
Step 21. PRINT “Wrong input for the position”
Step 22. END OF ELSE
Step 23. END OF ELSE
Step 24. END OF ELSE
Example:
• In figure, the 2nd node with memory address “101”
is deleted from the singly linked list.
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU
For queries
Email: vishal.e12820@cumail.in
19