Linked List, Types of Linked LIst, Various Operations, Applications of Linked List
U2.linked list
-
Upload
ssankett-negi -
Category
Technology
-
view
854 -
download
0
description
Transcript of U2.linked list
![Page 1: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/1.jpg)
![Page 2: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/2.jpg)
List ADT List is an ordered collection of items. Items
in the collection may be integers, Strings, characters or any kind of objects.
Basic operations are Adding an item to the collection Deleting an item from the collection Testing if list is empty Testing if list is full Getting an item on specified location Getting an item with specified key value
![Page 3: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/3.jpg)
Linked List Collection of links with reference to the first. Each link has
part to store data link that refers to the next link in the list.
Data part of the link can be an integer, a character, a String or an object of any kind.
![Page 4: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/4.jpg)
Linked Lists
A linked list is a linear collection of data elements called nodes where linear order is given by means of pointers.
Each node has two parts1) Data Field – Stores data of the node2) Link Field – Store address of the next node
( i.e. Link to the next node)
LINK
DATA
NODE :
![Page 5: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/5.jpg)
Linked Lists
link data
A B C D
x
link link link Data data data
start
- START is List pointer contains address of the first node in the List
- All nodes are connected to each other through Link fields
- Link of the last node is NULL pointer denoted by ‘X’ sign
- Null pointer indicated end of the list
![Page 6: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/6.jpg)
Algorithms
Lets consider,
• START is the 1st position in Linked List • NewNode is the new node to be created • DATA is the element to be inserted in new node• POS is the position where the new node to be
inserted• TEMP and HOLD are temporary pointers to hold
the node address
![Page 7: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/7.jpg)
Algorithm to Insert a Node at the beginning
1. Input DATA to be inserted 2. Create NewNode 3. NewNode -> DATA = DATA 4. If START is equal to NULL a) NewNode -> LINK =
NULL 5. Else a) NewNode -> LINK =
START 6. START = NewNode 7. Exit
![Page 8: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/8.jpg)
Insert a Node at the beginning
x
link link link data data data
start
NewNode
![Page 9: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/9.jpg)
Algorithm to Insert a Node at the end
1. Input DATA to be inserted2. Create NewNode3. NewNode -> DATA = DATA4. NewNode -> LINK = NULL5. If START is equal to NULL a) START = NewNode 6. Else a) TEMP = START b) while (TEMP -> LINK not
equal to NULL) i) TEMP = TEMP -> LINK 7. TEMP -> Link = NewNode 8. Exit
![Page 10: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/10.jpg)
Insert a Node at the end
link data
A B C
link link data data
start
P
X
NewNode
![Page 11: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/11.jpg)
Algorithm to Insert a Node at any specified position
1. Input DATA to be inserted and POS, the position to be inserted.
2. Initialize TEMP = START and K=13. Repeat step 3 while ( K is less than POS) a) TEMP = TEMP -> LINK b) If TEMP -> LINK = NULL i) Exit c) K = K + 1 4. Create a Newnode 5. Newnode -> DATA = DATA 6. Newnode -> LINK = TEMP -> LINK 7. TEMP -> LINK = NewNode 8. Exit
![Page 12: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/12.jpg)
Insert a Node at middle position
P
link data
A B
C
D
x
link link link data data data
start
NewNode
4 3 2 1
Lets consider, POS = 3
![Page 13: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/13.jpg)
Algorithm to Delete a Node
1. Input DATA to be deleted 2. If START is equal to DATA a) TEMP = START b) START = START -> LINK c) Set free node TEMP - which
is deleted d) Exit 3. HOLD = START 4. While ((HOLD -> LINK ->LINK) not equal to
NULL) a) If(HOLD -> LINK ->DATA) equal to DATA i) TEMP = HOLD -> LINK ii) HOLD -> LINK = TEMP ->
LINK iii) Set free node TEMP -
which is deleted iv) Exit b) HOLD = HOLD -> NEXT
![Page 14: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/14.jpg)
Algorithm to Delete a Node
5. If(HOLD -> LINK ->DATA) equal to DATA i) TEMP = HOLD -> LINK ii) Set free node TEMP - which is deleted iii) HOLD -> LINK = NULL iv) Exit 6. Display DATA not found 7. Exit
![Page 15: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/15.jpg)
Algorithm to Delete a Node
link data
A B C D
x
link link link data data data
Node to be deleted start
![Page 16: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/16.jpg)
Algorithm for Searching a Node
Suppose START is the address of the first node in the linked list and DATA is the informSation to be searched.
1. Input the DATA to be searched.
2. Initialize TEMP = START and Pos =1
3. Repeat the step 4,5 and 6 until (TEMP is equal to NULL)
4. If (TEMP -> DATA is equal to DATA) (a) Display “The DATA found at POS “ (b) Exit 5. TEMP = TEMP -> LINK 6. POS = POS + 1 7. If (TEMP is equal to NULL) (a) Display “ The DATA is not found in the list” 8. Exit.
![Page 17: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/17.jpg)
Algorithm for Displaying all Nodes
Suppose List is the address of the first node in the linked list.
1. If ( START is equal to NULL) ( a ) Display “The List is Empty” ( b ) Exit
2. Initialize TEMP = START
3. Repeat the Step 4 and 5 until (TEMP == NULL)
4. Display TEMP -> DATA
5. TEMP = TEMP -> LINK
6. Exit
![Page 18: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/18.jpg)
Singly Linked Lists and Arrays
Singly linked list Array Elements are stored in linear order, accessible with links. Do not have a fixed size. Cannot access the previous element directly.
Elements are stored in linear order, accessible with an index. Have a fixed size. Can access the previous element easily.
![Page 19: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/19.jpg)
Doubly Linked List
A doubly linked list is often more convenient!
Nodes store: element link to the previous node link to the next node
Special trailer and header nodes
prev next
elem
trailer header nodes/positions
elements
node
![Page 20: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/20.jpg)
Insertion
We visualize operation insertAfter(p, X), which returns position q
A B X C
A B C
p
A B C
p
X
q
p q
![Page 21: U2.linked list](https://reader034.fdocuments.in/reader034/viewer/2022052523/55509967b4c90590208b47af/html5/thumbnails/21.jpg)
Deletion
We visualize remove(p), where p == last()
A B C D
p
A B C
D
p
A B C