SlideShare une entreprise Scribd logo
1  sur  19
Mrs. K. Kasthuri., M.Sc., M.Phil., M.Tech.,
Assistant Professor of IT,
V.V.Vanniaperumal College for Women,
Virudhunagar.
LINKED LIST
DEFINITI0N
A linked list is a data structure that stores a
sequence of elements. Each element in the list is called a
node, and each node has a reference to the next node in
the list. The first node in the list is called the head, and the
last node in the list is called the tail.
Linked List
uses
These nodes hold both the data and a reference to
the next node in the list. Linked lists are often used because
of their efficient insertion and deletion. They can be used to
implement stacks, queues, and other abstract data types.
A singly linked list is a type of linked list that is
unidirectional, that is, it can be traversed in only one
direction from head to the last node (tail). Each element
in a linked list is called a node. A single node contains
data and a pointer to the next node which helps in
maintaining the structure of the list.
class Node {
public:
int data;
Node* next;
}
The singly linked list is used to
implement stack and queue. The undo or
redo options, the back buttons, etc., that we
discussed above are implemented using a
singly linked list. During the implementation
of a hash function, there arises a problem of
collision, to deal with this problem, a singly
linked list is used.
USES
 Traversing the list
 Inserting a node into the first
Deleting a node from the list
Merging the linked list
Copying the list
Searching the list
Operations on Singly Linked List::
The following operations are performed on a Single
LinkedList
Traversing:Displaying the contents of a linked list is very simple.
Insertion:The insertion operation can be performed in three ways. They are as
follows...Inserting At the Beginning of the list
Inserting At End of the list
Inserting At Specific location in the list
Deletion:The deletion operation can be performed in three ways. They are as
follows…Deleting from the Beginning of the list
Deleting from the End of the list
Deleting a Specific Node
Search: It is a process of determining and retrieving a specific node either from
the front, the end or anywhere in the list.
Merging:The merge point is where both lists point to the same node, i.e. they
reference the same memory location
Traverse a Linked List
Displaying the contents of a linked list is very simple. We
keep moving the temp node to the next one and display its
contents.When temp is NULL, we know that we have reached the
end of the linked list so we get out of the while loop.
void list::traverse_list(){
node*new1,node*head;
new1=new Node;
new1->data = item;
new1->next = NULL;}
1. Insert at the Beginning
•Allocate memory for new node
•Store data
•Change next of new node to point to head
•Change head to point to recently created node
2. Insert at the End
•Allocate memory for new node
•Store data
•Traverse to last node
•Change next of last node to recently created node
void list::insert_end (){
node *new1, node *head;{
new1=new node;new1->data = item;new1->next= NULL;
while(ptr ->next != null){ptr = ptr->next; }
Ptr->next = new1;}
void list::inserte_begining(){
node*new1,node*head;
new1=new Node;
new1->data = item;
new1->next = NULL;}
3.Insert at the Middle
•Allocate memory and store data for new node
•Traverse to node just before the required position of new node
•Change next pointers to include new node in between
Void list::insert_end(){
node *new1,node*head, int position{
new1->data =item; ptr=head;
while(ptr->data!=position){ ptr = ptr->next; }
new1->next = ptr->next; ptr->next = new1; }
DELETE FROM SINGLE LINKEDLIST
2. Delete from End
•Traverse to second last element
•Change its next pointer to null
void list::delete_begining(){
node*head;{ item=head->data; head = head->next;}}
1. Delete from Beginning
• Point head to the second node
void list::deleteLast(){
Node*head,*ptr,*prev;{
ptr = head;
while(ptr->next->next != null){ prev=ptr;
ptr = ptr->next; } prev->next = NULL; }
3. Delete from Middle
•Traverse to element before the element to be deleted
•Change next pointers to exclude the node from the chain
void list::delete_middle()
node*head,*ptr; int position{
ptr = head;
while(ptr->data != popsition){
ptr = ptr ->next; }
ptr->next= ptr->next->next; }
Search an Element on a
Linked List
 You can search an element on a linked list using a loop using the following steps. We
are finding item on a linked list.
 Make head as the current node.
 Run a loop until the current node is NULL because the last element points to NULL.
 In each iteration, check if the key of the node is equal to item. If it the key matches the
item, return true otherwise return false.
Void list::search(){
node*head,*ptr, int position,count=0;{
ptr= head;
while(ptr != null && ptr->data != position){
count++;ptr = ptr->next; } }
Sorting Elements of a Linked List
We will use a simple sorting algorithm, Bubble Sort, to sort the elements of a linked list in
asscending order below.
1. Make the head as the current node and create another node index for later use.
2. If head is null, return.
3. Else, run a loop till the last node (i.e. NULL).
4. In each iteration, follow the following step 5-6.
5. Store the next node of current in index.
6. Check if the data of the current node is greater than the next node. If it is greater,
swap current and index.
Void list :: sorting(){
node*ptr ,*ptr1;{ Ptr=head ;
While(ptr!=NULL){
for(ptr1=ptr->next;ptr1!=NULL;ptr1=ptr1->next){
if(ptr->data>ptr->data){
T=ptr->data;ptr->next=ptr1->data=t;}}
void list :: merge(){
node*ptr1,*ptr2,*ptr3;{
Ptr3=head1;ptr1=head1;
While(ptr1->next!=NULL){
Ptr3=ptr1;ptr1=ptr->next;ptr3=ptr3->next;}
Ptr2=head2;
Ptr3->next=head2;}}
MERGING
We need to call the mergeSort() function. Inside the mergeSort() function, we need to
perform certain steps:
First, handle the base case i.e. if the head pointer of the linked list is pointing to null
then we cannot perform anything, so return.
Else, we will divide the linked list into smaller halves.
Now, we will sort the smaller halves of the linked list.
Finally, we will merge this sorted linked list and update the head pointer pointing to the
head of the merged linked list.
ptr1
ptr2
Ptr3
Linked list and its operations - Traversal

Contenu connexe

Similaire à Linked list and its operations - Traversal

Similaire à Linked list and its operations - Traversal (20)

Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked list
Linked list Linked list
Linked list
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
Team 10
Team 10Team 10
Team 10
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Link list assi
Link list assiLink list assi
Link list assi
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 

Plus de kasthurimukila

Input - Output Organization and I/O Interface
Input - Output Organization and I/O InterfaceInput - Output Organization and I/O Interface
Input - Output Organization and I/O Interfacekasthurimukila
 
Blockchain Technology ,Architecture and its Structure
Blockchain Technology ,Architecture and its StructureBlockchain Technology ,Architecture and its Structure
Blockchain Technology ,Architecture and its Structurekasthurimukila
 
Circular Linked List.pptx
Circular Linked List.pptxCircular Linked List.pptx
Circular Linked List.pptxkasthurimukila
 
Introduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of DatabaseIntroduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of Databasekasthurimukila
 

Plus de kasthurimukila (12)

Input - Output Organization and I/O Interface
Input - Output Organization and I/O InterfaceInput - Output Organization and I/O Interface
Input - Output Organization and I/O Interface
 
Blockchain Technology ,Architecture and its Structure
Blockchain Technology ,Architecture and its StructureBlockchain Technology ,Architecture and its Structure
Blockchain Technology ,Architecture and its Structure
 
Circular Linked List.pptx
Circular Linked List.pptxCircular Linked List.pptx
Circular Linked List.pptx
 
Files_in_C.ppt
Files_in_C.pptFiles_in_C.ppt
Files_in_C.ppt
 
data analytics.pptx
data analytics.pptxdata analytics.pptx
data analytics.pptx
 
WML Script.pptx
WML Script.pptxWML Script.pptx
WML Script.pptx
 
Big Data.pptx
Big Data.pptxBig Data.pptx
Big Data.pptx
 
Scatter Plot.pptx
Scatter Plot.pptxScatter Plot.pptx
Scatter Plot.pptx
 
Introduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of DatabaseIntroduction to Database, Purpose of Data, Data models, Components of Database
Introduction to Database, Purpose of Data, Data models, Components of Database
 
Number system
Number systemNumber system
Number system
 
Java
Java Java
Java
 
Dbms
DbmsDbms
Dbms
 

Dernier

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
“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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Dernier (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
“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...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Linked list and its operations - Traversal

  • 1. Mrs. K. Kasthuri., M.Sc., M.Phil., M.Tech., Assistant Professor of IT, V.V.Vanniaperumal College for Women, Virudhunagar.
  • 2. LINKED LIST DEFINITI0N A linked list is a data structure that stores a sequence of elements. Each element in the list is called a node, and each node has a reference to the next node in the list. The first node in the list is called the head, and the last node in the list is called the tail.
  • 3. Linked List uses These nodes hold both the data and a reference to the next node in the list. Linked lists are often used because of their efficient insertion and deletion. They can be used to implement stacks, queues, and other abstract data types.
  • 4.
  • 5.
  • 6.
  • 7. A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node (tail). Each element in a linked list is called a node. A single node contains data and a pointer to the next node which helps in maintaining the structure of the list. class Node { public: int data; Node* next; }
  • 8. The singly linked list is used to implement stack and queue. The undo or redo options, the back buttons, etc., that we discussed above are implemented using a singly linked list. During the implementation of a hash function, there arises a problem of collision, to deal with this problem, a singly linked list is used. USES
  • 9.  Traversing the list  Inserting a node into the first Deleting a node from the list Merging the linked list Copying the list Searching the list
  • 10. Operations on Singly Linked List:: The following operations are performed on a Single LinkedList Traversing:Displaying the contents of a linked list is very simple. Insertion:The insertion operation can be performed in three ways. They are as follows...Inserting At the Beginning of the list Inserting At End of the list Inserting At Specific location in the list Deletion:The deletion operation can be performed in three ways. They are as follows…Deleting from the Beginning of the list Deleting from the End of the list Deleting a Specific Node Search: It is a process of determining and retrieving a specific node either from the front, the end or anywhere in the list. Merging:The merge point is where both lists point to the same node, i.e. they reference the same memory location
  • 11. Traverse a Linked List Displaying the contents of a linked list is very simple. We keep moving the temp node to the next one and display its contents.When temp is NULL, we know that we have reached the end of the linked list so we get out of the while loop. void list::traverse_list(){ node*new1,node*head; new1=new Node; new1->data = item; new1->next = NULL;}
  • 12. 1. Insert at the Beginning •Allocate memory for new node •Store data •Change next of new node to point to head •Change head to point to recently created node 2. Insert at the End •Allocate memory for new node •Store data •Traverse to last node •Change next of last node to recently created node void list::insert_end (){ node *new1, node *head;{ new1=new node;new1->data = item;new1->next= NULL; while(ptr ->next != null){ptr = ptr->next; } Ptr->next = new1;} void list::inserte_begining(){ node*new1,node*head; new1=new Node; new1->data = item; new1->next = NULL;}
  • 13. 3.Insert at the Middle •Allocate memory and store data for new node •Traverse to node just before the required position of new node •Change next pointers to include new node in between Void list::insert_end(){ node *new1,node*head, int position{ new1->data =item; ptr=head; while(ptr->data!=position){ ptr = ptr->next; } new1->next = ptr->next; ptr->next = new1; } DELETE FROM SINGLE LINKEDLIST
  • 14. 2. Delete from End •Traverse to second last element •Change its next pointer to null void list::delete_begining(){ node*head;{ item=head->data; head = head->next;}} 1. Delete from Beginning • Point head to the second node void list::deleteLast(){ Node*head,*ptr,*prev;{ ptr = head; while(ptr->next->next != null){ prev=ptr; ptr = ptr->next; } prev->next = NULL; }
  • 15. 3. Delete from Middle •Traverse to element before the element to be deleted •Change next pointers to exclude the node from the chain void list::delete_middle() node*head,*ptr; int position{ ptr = head; while(ptr->data != popsition){ ptr = ptr ->next; } ptr->next= ptr->next->next; }
  • 16. Search an Element on a Linked List  You can search an element on a linked list using a loop using the following steps. We are finding item on a linked list.  Make head as the current node.  Run a loop until the current node is NULL because the last element points to NULL.  In each iteration, check if the key of the node is equal to item. If it the key matches the item, return true otherwise return false. Void list::search(){ node*head,*ptr, int position,count=0;{ ptr= head; while(ptr != null && ptr->data != position){ count++;ptr = ptr->next; } }
  • 17. Sorting Elements of a Linked List We will use a simple sorting algorithm, Bubble Sort, to sort the elements of a linked list in asscending order below. 1. Make the head as the current node and create another node index for later use. 2. If head is null, return. 3. Else, run a loop till the last node (i.e. NULL). 4. In each iteration, follow the following step 5-6. 5. Store the next node of current in index. 6. Check if the data of the current node is greater than the next node. If it is greater, swap current and index. Void list :: sorting(){ node*ptr ,*ptr1;{ Ptr=head ; While(ptr!=NULL){ for(ptr1=ptr->next;ptr1!=NULL;ptr1=ptr1->next){ if(ptr->data>ptr->data){ T=ptr->data;ptr->next=ptr1->data=t;}}
  • 18. void list :: merge(){ node*ptr1,*ptr2,*ptr3;{ Ptr3=head1;ptr1=head1; While(ptr1->next!=NULL){ Ptr3=ptr1;ptr1=ptr->next;ptr3=ptr3->next;} Ptr2=head2; Ptr3->next=head2;}} MERGING We need to call the mergeSort() function. Inside the mergeSort() function, we need to perform certain steps: First, handle the base case i.e. if the head pointer of the linked list is pointing to null then we cannot perform anything, so return. Else, we will divide the linked list into smaller halves. Now, we will sort the smaller halves of the linked list. Finally, we will merge this sorted linked list and update the head pointer pointing to the head of the merged linked list. ptr1 ptr2 Ptr3