0% found this document useful (0 votes)
28 views

Linked List3

This document discusses linked lists and algorithms for insertion and deletion of nodes in linked lists. It begins with an overview of insertion, including inserting a node at the beginning, middle, and end of a linked list. Deletion algorithms are then covered for removing the first, last, and node at a given position from a linked list. Pseudocode is provided to demonstrate the insertion and deletion algorithms. References for further reading on data structures and linked lists are listed at the end.

Uploaded by

karamjot kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Linked List3

This document discusses linked lists and algorithms for insertion and deletion of nodes in linked lists. It begins with an overview of insertion, including inserting a node at the beginning, middle, and end of a linked list. Deletion algorithms are then covered for removing the first, last, and node at a given position from a linked list. Pseudocode is provided to demonstrate the insertion and deletion algorithms. References for further reading on data structures and linked lists are listed at the end.

Uploaded by

karamjot kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: CSH-231
Chapter: Linked List
Subject Coordinator:
Mr. Vishal Kumar
(E12820)

Linked List DISCOVER . LEARN .


Lecture No. 2.1.3 EMPOWER
Index
• Insertion in linked list

• Deletion from linked list

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

2. [Remove first node from AVAIL list]


Set NEW: = AVAIL and AVAIL:=LINK[AVAIL].

3. Set INFO[NEW]=:ITEM [Copies new data into new node]

4. Set LINK[NEW]:=START. [New node now points to original first node]

5. Set START:=NEW [changes START so it points to the new node]

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.

1. [OVERFLOW?] If AVAIL=NULL, then write OVERFLOE, and exit.

2. [REMOVE first node from AVAIL list.]


Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]

3. Set INFO[NEW]:= ITEM [Copies new data into new node.]

4. If LOC:=NULL, then [Insert as first node]


Set LINK[NEW]:= NULL and START := NEW
Else [Insert after node with location LOC]
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW
End of If structure

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy