SlideShare a Scribd company logo
1 of 58
ASSIGNMENT
OF
DATA & FILE STRUCTURES
[ TOPIC: ONE-WAY LINK LIST ]
SUBMITTED TO : SUBMITTED BY:
DR. VISHAL GOYAL 140454170
140454194
140454218
140454222
140454272
( M.C.A. 1ST - B)
1
• Introduction To Linked list
• Introduction To One Way Linked list
• Operations on One Way Linked List
Contents
2
Linked List
• A link list can be defined as the linear collection of
elements where each element is stored in a node.
• The linear order b/w elements is given by means of
pointers instead of sequential memory locations.
Introduction to Linked List
3
• Singular or one-way linked list.
•. Doubly or two-way linked list.
•. Circular linked list.
• Header linked list.
Types of Linked List
4
It is also known as singular linked list.
Each node has two parts:-
• The first part is known as info part which holds the element.
• Second part is known as next part which holds the address of
next node.
One Way Linked List
5
One Way Linked List
Info Address of next node
6
Operations of 1-Way Linked List
• Traversing a linked list.
• Searching an element in linked list
• Inserting an element in linked list
• Deleting an element in linked list
• Copying a linked list.
• Merging two linked lists
• Splitting linked list into further parts.
7
Traversing A Linked List
Traversing a linked list refers to visiting each node of the list
in order to process the elements stored in the nodes.
Example:- list of students with roll no.
401 402 Null414
401 402 414
8
Begin
Algorithm : Traversal of linked list
Step1.If Begin=Null then
Print ”linked list is empty”
Exit
[End If]
Step2. Set Pointer =Begin
Step3.Repeat While Pointer ≠ Null
a. print : Pointer → Info
b. Assign Pointer =Pointer → Next
[End loop]
Step4.Exit
9
• In searching we traverse the list from begin and compare the
elements stored in each node with the desired element to be
searched
• If match is found then the address of the node is returned
otherwise we proceed to next node
• If element not found till null then search unsuccessful
Searching In A Linked List
10
Searching In A Link List
11
Begin 104 102 103 Null
Pointer
102
Item found in link list
Item not matched
Algorithm :Searching a Linked List
Step1: If Begin = Null Then
Print:”Linked List is Empty”
Exit
[End If]
Step 2: Set Pointer = Begin
Step 3: Repeat While Pointer ≠ Null
If Pointer → info = Data Then
Print : “Element is found at address”:Pointer
Exit
Else
Set Pointer = Pointer → Next
[End If]
[End Loop]
Step 4: Print : “Element not found in linked list”
Step 5 :Exit
12
13
Memory Allocation/Deallocation
• Before we move on to insertion and deletion in a linked
list part let’s discuss about memory allocation and
deallocation.
• To insert an element into the linked list , the first we need
is to get a free node.
• In case of deletion of a node it is desired to return to
memory taken by deleted node for its reusability in future.
• First we get a free node from the free storage list
• Then element to be inserted is placed into the info part of the
node and pointers are set to add the new node at desired location
of list.
Insertion In Linked List
14
Insertion In Linked List (Continued)
Where we can insert element in linked list?
• At the beginning of linked list
• At end
• At a particular position in list
• In the sorted linked list
15
Insertion At Beginning
Begin 12 Null
Null414
400
Begin 401 88
16
New
Algorithm : Insertion At Beginning
Step 1: If Free = Null Then
Print : “Overflow: No free available for insertion”
Exit
[End if]
Step 2: Allocate Space to node New
(set New = Free And Free = Free → Next)
Step 3: Set New → Info = Data
Step 4: Set New → Next = Begin And Begin = New
Step 5: Exit
17
• If list is empty, store null value in next part of new node and insert
item in the info part
• If list is not empty, traverse list till end node.
• Store address of new node into next part of the last node of the
linked list and the next part set to null.
Insertion At End
Begin 401 402 Null414
405 Null
Pointer
18
New
Algorithm : Insertion At End
Step 1: If Free = Null Then
Print :”Overflow: No free space available for insertion”
Exit
[End If]
Step 2: Allocate space to node New
Set New = Free And Free = Free → Next
Step 3: Set New → Info = Data , New → Next = Null
Step 4: If Begin = Null Then
Begin = New
Exit
[End If]
Step 5: Set Pointer = Begin
Step 6: Repeat While Pointer → Next ≠ Null
Set Pointer = Pointer → Next
[End Loop]
Step 7: Set Pointer → Next = New
Step 8: Exit 19
Insertion At A Particular Position
• Locate the position of the node after which we want to insert
the new node.
• 2 cases are there if location found and if not found
• Traverse till we not reach on desired loc.
• If we reach on desired loc. Then loc. Found insert element if
we reach on end but not find a loc. Yet then loc. Not found.
20
Insertion At Any Location
Begin 401 402 Null414
Item
New → Next=Pointer → Next
Pointer → Next=New
21
Pointer
New
Algorithm : Insertion At Any Location
22
Step 1: If Free = Null Then
Print : “Overflow: No free space available for insertion”
Exit.
[End If]
Step 2: Set Pointer = Begin
Step 3: Repeat While Pointer ≠Null And Pointer → Info ≠ Data
Set Pointer = Pointer → Next
[End Loop]
Step 4: If Pointer = Null Then
Print: “The node containing element Data is not
present, so insertion is not possible.”
Else
Allocate space to node New
Set New=Free, Free=Free → Next, New → Info=Item
Set New → Next=Pointer → Next
Set Pointer → Next=New
[End If]
Step 5: Exit
23
Insertion In Sorted Linked List
• Find the position of the node after which new node has to be
inserted
• List can be sorted in ascending order and descending order
• In ascending order first we compare the element with the
first element if inserted element is small then it will inserted
at first position else comparing goes on in desc. Order it is
opposite.
24
Insertion In A Sorted Linked List
Begin 401 405 408 Null
403
Pointer
25
New
Algorithm : Insertion At Any Location In sorted Link List
26
Step 1: If Begin = Null Then
Allocate Space to node New
Set New = Free and Free = Free → Next
Set New → Info = Item
Set New → Next = Begin and Begin = New
[End If]
Step 2: If Item < Begin → Info Then
Allocate Space to node New
Set New = Free And Free = Free → Next
Set New → Info = Item
Set New → Next = Begin and Begin = New
Exit
[End If]
Step 3: Set Pointer = Begin and Pointer2 = Begin → Next
Step 4: Repeat While Pointer2 ≠ Null and Item > pointer2 → Info
Set Pointer1 = Pointer2 and Pointer2 = Pointer2 → Next
[End loop]
Step 5: If Free = Null Then
Print : “No space for insertion , Allocation of space to node
New is not possible”
Exit
[End If]
Step 6: Allocate space to node New
Set New = Free and Free = Free → Next
Step 7: Set New → Info = Item
Step 8: If Pointer2 = Null Then
Set Pointer1 → Next = New and New → Next = Null
Else
Set New → Next = Pointer → Next
Set Pointer1 → Next = New
[End If]
Step 9: Exit 27
Deletion From Linked List
Deletion can be done in 3 ways:
• Deleting a node at the begin of link list
• Deleting a node at end.
• Del. A particular node in the linked list.
28
DELETION OF NODE FROM BEGIN OF LIST
• Deletion of a node at the begin of the list is a very simple operation
which can be done by changing the list pointer variable begin.
• Now begin will point to next node in the list.
• The space occupied by the deleted node is returned to the free storage
list.
29
Deleting A Node At Begin.
Free
414
Null
Begin 401 402 Null
Algorithm : Deletion At Beginning of the linked list.
Step 1: If Begin = Null then
Print: “linked list is already empty”
Exit
[End if]
Step 2: set Item = Begin → Info and Pos = Begin
Step 3: Begin = Begin → Next
Step 4:Pos → Next = Free and Free = Pos
Step 5:Exit
30
• For deleting the lost node from the given plinked list, it is
necessary to traverse the entire linked list for finding the
address of the preceding node of the last node i.e., address
of second last node.
• After finding the address of the second last node we will
store the address stored in the next part of the last node
into the next part of the second last node i.e. null will be
stored in the next part of the 2nd last node.
31
Deleting A Node At End
Deleting A Node At End
Free Null
Begin 402 Null414
Pointer1 → Next=Pointer2 → Next
Pointer2 → Next=Free
Free=Pointer2
401
32
Pointer1 Pointer2
Pointer1 = Begin
Pointer2 = Begin→Next
Pointer1 = Pointer2
Pointer2 = Pointer2 → Next
NULL
Algorithm : Deletion At End
Step 1:If Begin = Null Then
Print : “Linked List Empty”
Exit
[End If]
Step 2: If Begin → Next = Null Then
Set Data = Begin → info
Deallocate memory held by Begin
(Begin → Next = Free and Free = Begin)
Set Begin = Null
Exit
[End If]
Step 3: Set Pointer1 = Begin and Pointer2 = Begin → Next
Step 4: Repeat While Pointer2 → Next ≠ Null
Set Pointer1 = Pointer2 and Pointer2 = Pointer2 → Next
[End loop] 33
Step 5: Set Pointer1 → Next = Pointer2 →Next
Step 6: Set Data = Pointer2 → Info
Step 7: Deallocate memory held by Pointer2
(Pointer2 →Next = Free and Free = Pointer2)
Step 8:Exit
34
• For deleting a particular node from the linked list, the first
task is to find the address of the preceding node of nth
node to be deleted.
• To complete the task traverse the linked list from begin and
compare the info. Stored in node with item.
• Two pointers pointer1 pointer2 will be used while
traversing the list for locating the address of the node to be
deleted and address of it’s preceding node.
35
Delete A Particular Node From Link List
Deleting A Particular Node In Link List
Begin
25 17 8
Free
Null
Pointer1 → Next=Pointer2 → Next
Pointer2 → Next=Free
Free=Pointer2
4
Null
Pointer1 Pointer2
36
Algorithm: Deletion At Any Location
Step 1 :If Begin = Null Then
Print : “Linked List is Empty”
Exit
[End If]
Step 2: If Begin → Info = Item Then
Set Pos = Begin
Set Begin = Begin → Next
Pos Next = Free and Free = Pos
Exit
[End If]
Step 3: Set Pointer1 = Begin and Pointer2 = Begin → Next
Step 4: Repeat While Pointer2 ≠ Null and Pointer2 → Info ≠ Item
SET Pointer1 = Pointer2 and Pointer2 → Next
[End loop]
37
Step 5: If Pointer2 = Null Then
Print : “Node containing element item not found”
Exit
Else
Set Pointer1 → Next = Pointer2 → Next
[End If]
Step 6: Deallocate memory held by Pointer2
(Set Pointer2 → Next = Free and Free = Pointer2)
Step 7: Exit
38
Copy A Link List Into Other Link List
• Consider the linked list with its start pointer as begin1.For
copying this given linked list into another list, use a new
pointer variable begin2 for the list in which source list will be
copied.
• Initially we will store null in the list variable begin2.
• Now we will traverse the entire source list from begin to the
end by copying the contents to the new target.
39
Copy A Link List Into Other Link List
17 8
Begin1 25 17 Null388
Begin2 25 Null38
A Link List Is Copied
40
Pointer
Algorithm: Copying One Link List Into Another Link List
41
Step 1: If Begin1 = Null Then
Print: “Source List is Empty”
Exit
[End If]
Step 2: Set Begin2 = Null
Step 3: If Free = Null Then
Print: “Free space not available”
Exit
Else
Allocate memory to the node New
Set New = Free And Free = Free → Next
[End If]
Step 4: Set New → Info=Begin1 → Info And New → Next =Null
Step 5: Set Begin2 = New
Step 6: Set Pointer1=Begin1 → Next And Pointer2=Begin2
Step 7: Repeat While Pointer1 ≠ Null And Free ≠ Null
a. Allocate memory to node New
(New=Free And Free=Free → Next)
b. Set New → Info=Pointer1 → Info And New → Next=Null
c. Set Pointer2 → Next=New
d. Set Pointer1=Pointer1 → Next And Pointer2=New
[End Loop]
Step 8: If Pointer1 ≠ Null Then
Print: “List copied successfully”
Else
Print: “Not enough space to perform copy operation”
[End If]
Step 9: Exit
42
Merging Two Linked List
• There are number of applications where there is need to merge
two or more linked lists into a single linked list.
• Merging operation refers to putting the elements of two or more
lists into one list.
• The list can be sorted or unsorted.
Begin2 10 17 Null30
Begin1 25 27 Null42
43
After Merging
Begin
Merged Complete List
Begin2 10 Null30
Begin1 Null42
Pointer1
Pointer2
17
2725
10 17 25 27 30 Null42
44
Algorithm : Merging Two Sorted Linked List
Step 1: If Begin1=Null or Begin2=Null then
Print “one of the given linked list is empty”
Exit
[end if]
Step 2: If Free =Null then
Print: “no free space available”
Exit
Else
//Allocate memory to node New
Set New =Free and Free=Free → Next
[End If]
Step 3: Set Begin=Null
Step 4: If Begin1 → Info ≥ Begin2 → Info then
Set New → Info=Begin2 → Info and New → Next=Null
Set Pointer1=Begin1 and Pointer2=Begin2 → Next
45
Else
Set New → Info=Begin1 → Info and New → Next=Null
Set Pointer1=Begin1 → Next and Pointer2=Begin2
[End If]
Step 5: Set Begin=New and Pointer =New
Step 6: Repeat steps 7and 8 while Pointer1≠Null and Pointer2 ≠ Null
Step 7: If Free=Null then
Print “No free space available”
Exit
Else
Set New =Free and Free=Free → Next
[End If]
Step 8: If Pointer1 → Info ≥ Pointer2 → Info then
Set New → Info=Pointer2 → Info
Set New → Next=Null
Set Pointer → Next=New
Set Pointer=New and Pointer2=Pointer2 → Next 46
[End If]
[End Loop]
Step 9: If Pointer1=Null and Free ≠ Null then
Repeat while Pointer2 ≠ Null
a. Set New=Free and Free=Free → Next
b. Set New → Info=Pointer2 → Info and
New → Next =Null
c. Set Pointer → Next=New
d. Set Pointer =New and Pointer2=Pointer2 → Next
[End Loop]
Else
Repeat while Pointer1 ≠ Null
a. Set New=Free and Free=Free → Next
b. Set New → Info=Pointer1 → Info and
New → Next=Null
c.Set Pointer → Next=New
d. Set Pointer=New and Pointer1=Pointer1 → Next47
[End Loop]
[End If]
Step 10: If Pointer1 = Null And Pointer2=Null Then
Print: “The given link lists merged successfully”
Else
Print “Not Enough space”
[End If]
Step 11: Exit
48
Splitting Two Lists
• Suppose we have a linked list which we want to split into
lists.
• First we check total no. Of nodes then (N/2)th and (N/2+1)th
Node.
• After finding these addresses we will store null in the next
part of the (n/2)th node and address of (n/2+1)th node will be
stored in the new list pointer variable begin2.
• Now our list divide into 2 parts n/2 and n-n/2 with list
begin1 and begin2.
49
Splitted List1 And List2 .
17 2710 Null28
Begin
Begin2
27 28 Null32
Begin1
10 17 Null25
Pointer
50
3225
Algorithm: Split A Link List Into Two Link Lists.
51
Step 1: If Begin=Null
Print: “Splitting cannot be performed on empty list”
Exit
[End If]
Step 2: Set pointer = Begin And Count = 0
Step 3: Repeat Steps 4 and 5 While Pointer ≠ Null
Step 4: Set Count = Count + 1
Step 5: Set Pointer = pointer → Next
[End Loop]
Step 6: Set Mid = Integer(count/2)
Step 7: Set Begin2 = Null And Pointer = Begin And i =1
Step 8: Repeat Step 9 While i ˂ Mid
Step 9: Set Pointer = Pointer → Next
Set i = i +1
[End Loop]
Step 10: Set Begin2 = Pointer → Next And
Pointer → Next = Null
Step 11: Exit
52
Reversing A One Way Linked List
• To reverse a linked list, we need to use three pointers
variables.
• One pointer variable is used to store the address of current
node.
• Second pointer variable will be used to store the address of
next node.
• The third pointer variable will be used to store the address of
next to next of current node.
53
Reversing A One Way Linked List
Begin2
105 Null
Begin1
5 Null10
54
Reverse Link List With More Than Two Nodes
17 2725Null10 4228
Begin
17 272510 Null4228
Begin
55
Algorithm: Reverse The One Way Link List
56
Step 1: If Begin = Null Then
Print: “No node is present in link list”
Exit
[End If]
Step 2: If Begin Next = Null Then
Print: “link list is having only one node”
Exit
[End If]
Step 3: If Begin → Next ≠ Null Then
Set Pointer1 = Begin
Set Pointer2 = Begin → Next
Set Pointer3 = Pointer2 → Next
[End If]
Step 4: If Pointer3 = Null Then
Set Pointer2 → Next = Pointer1
Set Pointer1 → Next = Null
Set Begin = Pointer2
Exit
[End If]
Step 5: Set Pointer1 → Next=Null
Step 6: Repeat steps 7 to 10 while Pointer3 → Next ≠Null
Step 7: Set Pointer2 → Next=Pointer1
Step 8: Set Pointer1=Pointer2
Step 9: Set Pointer2=Pointer3
Step 10: Set Pointer3=Pointer3 → Next
[End Loop]
Step 11: Set Pointer2 → Next=Pointer1
Step 12: Set Pointer3 → Next=Pointer2
Step 13: Set Begin=Pointer3
Step 14: Exit 57
THANKS
END
58

More Related Content

What's hot

Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinaryPrashant Rai
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists MrDavinderSingh
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
Ds06 linked list- insert a node at end
Ds06   linked list- insert a node at endDs06   linked list- insert a node at end
Ds06 linked list- insert a node at endjyoti_lakhani
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5Teksify
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)shah alom
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked listsAfriyieCharles
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked listecomputernotes
 

What's hot (18)

Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Lect 11-12 Zaheer Abbas
Lect 11-12 Zaheer AbbasLect 11-12 Zaheer Abbas
Lect 11-12 Zaheer Abbas
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Link list
Link listLink list
Link list
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
02. the linked lists (1)
02. the linked lists (1)02. the linked lists (1)
02. the linked lists (1)
 
Ds06 linked list- insert a node at end
Ds06   linked list- insert a node at endDs06   linked list- insert a node at end
Ds06 linked list- insert a node at end
 
Linked List
Linked ListLinked List
Linked List
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 

Similar to Ppt of operations on one way link list

Similar to Ppt of operations on one way link list (20)

5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssssDSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
 
algorithms_in_linkedlist (1).pdf
algorithms_in_linkedlist (1).pdfalgorithms_in_linkedlist (1).pdf
algorithms_in_linkedlist (1).pdf
 
algorithms_in_linkedlist.pptx
algorithms_in_linkedlist.pptxalgorithms_in_linkedlist.pptx
algorithms_in_linkedlist.pptx
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptx
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Linked list
Linked listLinked list
Linked list
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
linked list
linked listlinked list
linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 

Recently uploaded

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Ppt of operations on one way link list

  • 1. ASSIGNMENT OF DATA & FILE STRUCTURES [ TOPIC: ONE-WAY LINK LIST ] SUBMITTED TO : SUBMITTED BY: DR. VISHAL GOYAL 140454170 140454194 140454218 140454222 140454272 ( M.C.A. 1ST - B) 1
  • 2. • Introduction To Linked list • Introduction To One Way Linked list • Operations on One Way Linked List Contents 2
  • 3. Linked List • A link list can be defined as the linear collection of elements where each element is stored in a node. • The linear order b/w elements is given by means of pointers instead of sequential memory locations. Introduction to Linked List 3
  • 4. • Singular or one-way linked list. •. Doubly or two-way linked list. •. Circular linked list. • Header linked list. Types of Linked List 4
  • 5. It is also known as singular linked list. Each node has two parts:- • The first part is known as info part which holds the element. • Second part is known as next part which holds the address of next node. One Way Linked List 5
  • 6. One Way Linked List Info Address of next node 6
  • 7. Operations of 1-Way Linked List • Traversing a linked list. • Searching an element in linked list • Inserting an element in linked list • Deleting an element in linked list • Copying a linked list. • Merging two linked lists • Splitting linked list into further parts. 7
  • 8. Traversing A Linked List Traversing a linked list refers to visiting each node of the list in order to process the elements stored in the nodes. Example:- list of students with roll no. 401 402 Null414 401 402 414 8 Begin
  • 9. Algorithm : Traversal of linked list Step1.If Begin=Null then Print ”linked list is empty” Exit [End If] Step2. Set Pointer =Begin Step3.Repeat While Pointer ≠ Null a. print : Pointer → Info b. Assign Pointer =Pointer → Next [End loop] Step4.Exit 9
  • 10. • In searching we traverse the list from begin and compare the elements stored in each node with the desired element to be searched • If match is found then the address of the node is returned otherwise we proceed to next node • If element not found till null then search unsuccessful Searching In A Linked List 10
  • 11. Searching In A Link List 11 Begin 104 102 103 Null Pointer 102 Item found in link list Item not matched
  • 12. Algorithm :Searching a Linked List Step1: If Begin = Null Then Print:”Linked List is Empty” Exit [End If] Step 2: Set Pointer = Begin Step 3: Repeat While Pointer ≠ Null If Pointer → info = Data Then Print : “Element is found at address”:Pointer Exit Else Set Pointer = Pointer → Next [End If] [End Loop] Step 4: Print : “Element not found in linked list” Step 5 :Exit 12
  • 13. 13 Memory Allocation/Deallocation • Before we move on to insertion and deletion in a linked list part let’s discuss about memory allocation and deallocation. • To insert an element into the linked list , the first we need is to get a free node. • In case of deletion of a node it is desired to return to memory taken by deleted node for its reusability in future.
  • 14. • First we get a free node from the free storage list • Then element to be inserted is placed into the info part of the node and pointers are set to add the new node at desired location of list. Insertion In Linked List 14
  • 15. Insertion In Linked List (Continued) Where we can insert element in linked list? • At the beginning of linked list • At end • At a particular position in list • In the sorted linked list 15
  • 16. Insertion At Beginning Begin 12 Null Null414 400 Begin 401 88 16 New
  • 17. Algorithm : Insertion At Beginning Step 1: If Free = Null Then Print : “Overflow: No free available for insertion” Exit [End if] Step 2: Allocate Space to node New (set New = Free And Free = Free → Next) Step 3: Set New → Info = Data Step 4: Set New → Next = Begin And Begin = New Step 5: Exit 17
  • 18. • If list is empty, store null value in next part of new node and insert item in the info part • If list is not empty, traverse list till end node. • Store address of new node into next part of the last node of the linked list and the next part set to null. Insertion At End Begin 401 402 Null414 405 Null Pointer 18 New
  • 19. Algorithm : Insertion At End Step 1: If Free = Null Then Print :”Overflow: No free space available for insertion” Exit [End If] Step 2: Allocate space to node New Set New = Free And Free = Free → Next Step 3: Set New → Info = Data , New → Next = Null Step 4: If Begin = Null Then Begin = New Exit [End If] Step 5: Set Pointer = Begin Step 6: Repeat While Pointer → Next ≠ Null Set Pointer = Pointer → Next [End Loop] Step 7: Set Pointer → Next = New Step 8: Exit 19
  • 20. Insertion At A Particular Position • Locate the position of the node after which we want to insert the new node. • 2 cases are there if location found and if not found • Traverse till we not reach on desired loc. • If we reach on desired loc. Then loc. Found insert element if we reach on end but not find a loc. Yet then loc. Not found. 20
  • 21. Insertion At Any Location Begin 401 402 Null414 Item New → Next=Pointer → Next Pointer → Next=New 21 Pointer New
  • 22. Algorithm : Insertion At Any Location 22 Step 1: If Free = Null Then Print : “Overflow: No free space available for insertion” Exit. [End If] Step 2: Set Pointer = Begin Step 3: Repeat While Pointer ≠Null And Pointer → Info ≠ Data Set Pointer = Pointer → Next [End Loop] Step 4: If Pointer = Null Then Print: “The node containing element Data is not present, so insertion is not possible.”
  • 23. Else Allocate space to node New Set New=Free, Free=Free → Next, New → Info=Item Set New → Next=Pointer → Next Set Pointer → Next=New [End If] Step 5: Exit 23
  • 24. Insertion In Sorted Linked List • Find the position of the node after which new node has to be inserted • List can be sorted in ascending order and descending order • In ascending order first we compare the element with the first element if inserted element is small then it will inserted at first position else comparing goes on in desc. Order it is opposite. 24
  • 25. Insertion In A Sorted Linked List Begin 401 405 408 Null 403 Pointer 25 New
  • 26. Algorithm : Insertion At Any Location In sorted Link List 26 Step 1: If Begin = Null Then Allocate Space to node New Set New = Free and Free = Free → Next Set New → Info = Item Set New → Next = Begin and Begin = New [End If] Step 2: If Item < Begin → Info Then Allocate Space to node New Set New = Free And Free = Free → Next Set New → Info = Item Set New → Next = Begin and Begin = New Exit [End If] Step 3: Set Pointer = Begin and Pointer2 = Begin → Next
  • 27. Step 4: Repeat While Pointer2 ≠ Null and Item > pointer2 → Info Set Pointer1 = Pointer2 and Pointer2 = Pointer2 → Next [End loop] Step 5: If Free = Null Then Print : “No space for insertion , Allocation of space to node New is not possible” Exit [End If] Step 6: Allocate space to node New Set New = Free and Free = Free → Next Step 7: Set New → Info = Item Step 8: If Pointer2 = Null Then Set Pointer1 → Next = New and New → Next = Null Else Set New → Next = Pointer → Next Set Pointer1 → Next = New [End If] Step 9: Exit 27
  • 28. Deletion From Linked List Deletion can be done in 3 ways: • Deleting a node at the begin of link list • Deleting a node at end. • Del. A particular node in the linked list. 28
  • 29. DELETION OF NODE FROM BEGIN OF LIST • Deletion of a node at the begin of the list is a very simple operation which can be done by changing the list pointer variable begin. • Now begin will point to next node in the list. • The space occupied by the deleted node is returned to the free storage list. 29 Deleting A Node At Begin. Free 414 Null Begin 401 402 Null
  • 30. Algorithm : Deletion At Beginning of the linked list. Step 1: If Begin = Null then Print: “linked list is already empty” Exit [End if] Step 2: set Item = Begin → Info and Pos = Begin Step 3: Begin = Begin → Next Step 4:Pos → Next = Free and Free = Pos Step 5:Exit 30
  • 31. • For deleting the lost node from the given plinked list, it is necessary to traverse the entire linked list for finding the address of the preceding node of the last node i.e., address of second last node. • After finding the address of the second last node we will store the address stored in the next part of the last node into the next part of the second last node i.e. null will be stored in the next part of the 2nd last node. 31 Deleting A Node At End
  • 32. Deleting A Node At End Free Null Begin 402 Null414 Pointer1 → Next=Pointer2 → Next Pointer2 → Next=Free Free=Pointer2 401 32 Pointer1 Pointer2 Pointer1 = Begin Pointer2 = Begin→Next Pointer1 = Pointer2 Pointer2 = Pointer2 → Next NULL
  • 33. Algorithm : Deletion At End Step 1:If Begin = Null Then Print : “Linked List Empty” Exit [End If] Step 2: If Begin → Next = Null Then Set Data = Begin → info Deallocate memory held by Begin (Begin → Next = Free and Free = Begin) Set Begin = Null Exit [End If] Step 3: Set Pointer1 = Begin and Pointer2 = Begin → Next Step 4: Repeat While Pointer2 → Next ≠ Null Set Pointer1 = Pointer2 and Pointer2 = Pointer2 → Next [End loop] 33
  • 34. Step 5: Set Pointer1 → Next = Pointer2 →Next Step 6: Set Data = Pointer2 → Info Step 7: Deallocate memory held by Pointer2 (Pointer2 →Next = Free and Free = Pointer2) Step 8:Exit 34
  • 35. • For deleting a particular node from the linked list, the first task is to find the address of the preceding node of nth node to be deleted. • To complete the task traverse the linked list from begin and compare the info. Stored in node with item. • Two pointers pointer1 pointer2 will be used while traversing the list for locating the address of the node to be deleted and address of it’s preceding node. 35 Delete A Particular Node From Link List
  • 36. Deleting A Particular Node In Link List Begin 25 17 8 Free Null Pointer1 → Next=Pointer2 → Next Pointer2 → Next=Free Free=Pointer2 4 Null Pointer1 Pointer2 36
  • 37. Algorithm: Deletion At Any Location Step 1 :If Begin = Null Then Print : “Linked List is Empty” Exit [End If] Step 2: If Begin → Info = Item Then Set Pos = Begin Set Begin = Begin → Next Pos Next = Free and Free = Pos Exit [End If] Step 3: Set Pointer1 = Begin and Pointer2 = Begin → Next Step 4: Repeat While Pointer2 ≠ Null and Pointer2 → Info ≠ Item SET Pointer1 = Pointer2 and Pointer2 → Next [End loop] 37
  • 38. Step 5: If Pointer2 = Null Then Print : “Node containing element item not found” Exit Else Set Pointer1 → Next = Pointer2 → Next [End If] Step 6: Deallocate memory held by Pointer2 (Set Pointer2 → Next = Free and Free = Pointer2) Step 7: Exit 38
  • 39. Copy A Link List Into Other Link List • Consider the linked list with its start pointer as begin1.For copying this given linked list into another list, use a new pointer variable begin2 for the list in which source list will be copied. • Initially we will store null in the list variable begin2. • Now we will traverse the entire source list from begin to the end by copying the contents to the new target. 39
  • 40. Copy A Link List Into Other Link List 17 8 Begin1 25 17 Null388 Begin2 25 Null38 A Link List Is Copied 40 Pointer
  • 41. Algorithm: Copying One Link List Into Another Link List 41 Step 1: If Begin1 = Null Then Print: “Source List is Empty” Exit [End If] Step 2: Set Begin2 = Null Step 3: If Free = Null Then Print: “Free space not available” Exit Else Allocate memory to the node New Set New = Free And Free = Free → Next [End If] Step 4: Set New → Info=Begin1 → Info And New → Next =Null Step 5: Set Begin2 = New
  • 42. Step 6: Set Pointer1=Begin1 → Next And Pointer2=Begin2 Step 7: Repeat While Pointer1 ≠ Null And Free ≠ Null a. Allocate memory to node New (New=Free And Free=Free → Next) b. Set New → Info=Pointer1 → Info And New → Next=Null c. Set Pointer2 → Next=New d. Set Pointer1=Pointer1 → Next And Pointer2=New [End Loop] Step 8: If Pointer1 ≠ Null Then Print: “List copied successfully” Else Print: “Not enough space to perform copy operation” [End If] Step 9: Exit 42
  • 43. Merging Two Linked List • There are number of applications where there is need to merge two or more linked lists into a single linked list. • Merging operation refers to putting the elements of two or more lists into one list. • The list can be sorted or unsorted. Begin2 10 17 Null30 Begin1 25 27 Null42 43
  • 44. After Merging Begin Merged Complete List Begin2 10 Null30 Begin1 Null42 Pointer1 Pointer2 17 2725 10 17 25 27 30 Null42 44
  • 45. Algorithm : Merging Two Sorted Linked List Step 1: If Begin1=Null or Begin2=Null then Print “one of the given linked list is empty” Exit [end if] Step 2: If Free =Null then Print: “no free space available” Exit Else //Allocate memory to node New Set New =Free and Free=Free → Next [End If] Step 3: Set Begin=Null Step 4: If Begin1 → Info ≥ Begin2 → Info then Set New → Info=Begin2 → Info and New → Next=Null Set Pointer1=Begin1 and Pointer2=Begin2 → Next 45
  • 46. Else Set New → Info=Begin1 → Info and New → Next=Null Set Pointer1=Begin1 → Next and Pointer2=Begin2 [End If] Step 5: Set Begin=New and Pointer =New Step 6: Repeat steps 7and 8 while Pointer1≠Null and Pointer2 ≠ Null Step 7: If Free=Null then Print “No free space available” Exit Else Set New =Free and Free=Free → Next [End If] Step 8: If Pointer1 → Info ≥ Pointer2 → Info then Set New → Info=Pointer2 → Info Set New → Next=Null Set Pointer → Next=New Set Pointer=New and Pointer2=Pointer2 → Next 46
  • 47. [End If] [End Loop] Step 9: If Pointer1=Null and Free ≠ Null then Repeat while Pointer2 ≠ Null a. Set New=Free and Free=Free → Next b. Set New → Info=Pointer2 → Info and New → Next =Null c. Set Pointer → Next=New d. Set Pointer =New and Pointer2=Pointer2 → Next [End Loop] Else Repeat while Pointer1 ≠ Null a. Set New=Free and Free=Free → Next b. Set New → Info=Pointer1 → Info and New → Next=Null c.Set Pointer → Next=New d. Set Pointer=New and Pointer1=Pointer1 → Next47
  • 48. [End Loop] [End If] Step 10: If Pointer1 = Null And Pointer2=Null Then Print: “The given link lists merged successfully” Else Print “Not Enough space” [End If] Step 11: Exit 48
  • 49. Splitting Two Lists • Suppose we have a linked list which we want to split into lists. • First we check total no. Of nodes then (N/2)th and (N/2+1)th Node. • After finding these addresses we will store null in the next part of the (n/2)th node and address of (n/2+1)th node will be stored in the new list pointer variable begin2. • Now our list divide into 2 parts n/2 and n-n/2 with list begin1 and begin2. 49
  • 50. Splitted List1 And List2 . 17 2710 Null28 Begin Begin2 27 28 Null32 Begin1 10 17 Null25 Pointer 50 3225
  • 51. Algorithm: Split A Link List Into Two Link Lists. 51 Step 1: If Begin=Null Print: “Splitting cannot be performed on empty list” Exit [End If] Step 2: Set pointer = Begin And Count = 0 Step 3: Repeat Steps 4 and 5 While Pointer ≠ Null Step 4: Set Count = Count + 1 Step 5: Set Pointer = pointer → Next [End Loop] Step 6: Set Mid = Integer(count/2) Step 7: Set Begin2 = Null And Pointer = Begin And i =1 Step 8: Repeat Step 9 While i ˂ Mid
  • 52. Step 9: Set Pointer = Pointer → Next Set i = i +1 [End Loop] Step 10: Set Begin2 = Pointer → Next And Pointer → Next = Null Step 11: Exit 52
  • 53. Reversing A One Way Linked List • To reverse a linked list, we need to use three pointers variables. • One pointer variable is used to store the address of current node. • Second pointer variable will be used to store the address of next node. • The third pointer variable will be used to store the address of next to next of current node. 53
  • 54. Reversing A One Way Linked List Begin2 105 Null Begin1 5 Null10 54
  • 55. Reverse Link List With More Than Two Nodes 17 2725Null10 4228 Begin 17 272510 Null4228 Begin 55
  • 56. Algorithm: Reverse The One Way Link List 56 Step 1: If Begin = Null Then Print: “No node is present in link list” Exit [End If] Step 2: If Begin Next = Null Then Print: “link list is having only one node” Exit [End If] Step 3: If Begin → Next ≠ Null Then Set Pointer1 = Begin Set Pointer2 = Begin → Next Set Pointer3 = Pointer2 → Next [End If]
  • 57. Step 4: If Pointer3 = Null Then Set Pointer2 → Next = Pointer1 Set Pointer1 → Next = Null Set Begin = Pointer2 Exit [End If] Step 5: Set Pointer1 → Next=Null Step 6: Repeat steps 7 to 10 while Pointer3 → Next ≠Null Step 7: Set Pointer2 → Next=Pointer1 Step 8: Set Pointer1=Pointer2 Step 9: Set Pointer2=Pointer3 Step 10: Set Pointer3=Pointer3 → Next [End Loop] Step 11: Set Pointer2 → Next=Pointer1 Step 12: Set Pointer3 → Next=Pointer2 Step 13: Set Begin=Pointer3 Step 14: Exit 57