SlideShare a Scribd company logo
1 of 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

More Related Content

Similar to Linked list and its operations - Traversal

Similar to 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
 

More from 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
 

More from 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
 

Recently uploaded

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
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
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
“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
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
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
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
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
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
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
 
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
 

Recently uploaded (20)

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
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
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
“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...
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
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 🔝✔️✔️
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
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
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
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...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

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