SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
Creating a Linked list :
Algorithm :
Create first node and assign the address of the first node to the
pointer ‘start’.
Step : 1 start =getnode( );
Use another pointer to store the address of the first node, called
CURRPTR
Step : 2 currptr=Start;
Accept the item and store it in the INFO field of the node
Step : 3 info[currptr ]= item;
Accept the Choice from the keyboard whether to create another node
or not
Step : 4 if ( choice = ‘Y’)
goto step 5
else
goto step 6
If choice is ‘Y’ to create another node
Step : 5 i) Newnode = getnode( );
Newnode which contains the address of the newly created node
ii) link[currptr]=Newnode;
Making a link between the original last node and present newnode
iii) currptr=Newnode;
Address of the Newnode is assigned to the original newnode
iv) info[currptr]=item;
Accepting the item and storing it in the info field of currptr
go to step 4
If choice is ‘N’ to make the LINK field of the last node to NULL
Step : 6 link [currptr] = NULL and
Exit
Step : 7 return
• Traversing a linked list :
Algorithm:
Step 1 : Is linked list is empty
if ( start ==NULL)
display (“ linked list is empty “);
exit( )
Step 2 : Assign start value to CURRPTR
currptr=start;
Step 3 : Repeat while(currptr!=NULL)
Process info[currptr]
Step 4 : currptr=link[currptr] /* moving the currptr from one
node to the next node */
Step 5 : Exit
• Algorithm for Displaying a list :
• first check list is empty or not
• Step 1 : if ( start == NULL)
– Write (“ list is Empty”)
– return;
Store the Address of first node in another pointer CURRPTR
• Step 2 : set currptr = start;
• Step 3 : Repeat step4 and step5 while ( currptr !=NULL)
• Display items one-by-one from first node until Currptr becomes
NULL
• Step 4 : display ( info[currptr]);
After the display of this element ,Currptr should point to the next node
• Step 5 : currptr=link[currptr];
• Step 6 : Exit
• Insert operation at the beginning :
• Algorithm :
• Creating a node that is to be inserted
• step 1 : Newnode =getnode( );
• Enter the item into the Info field of the New node
• step 2 : info [Newnode]= item;
• Assign the start value to the LINK part of the inserted node(New
node).
• It makes a link from ‘New node’ to the previous first node.
• Now start and New node points to the original first node of the list.
• It makes link between start and Newnode.
• step 3 : link [Newnode]=start;
• step 4 : start = Newnode;
Exit;
• Algorithm : insert operation at end
Step 1 : if list is empty
if ( start == NULL)
insert at the beginning
exit( )
end if
Step 2 : currptr = start;
Traverse the list to obtain the last node address
Step 3 : while (link [currptr]! = NULL)
currptr = link [ currptr]
end while
Create a newnode and enter the accepted ITEM into its INFO fields
Step 4 : Newnode =getnode( );
Newnode -> info =item;
Make a link between Currptr(last node) and New node
Step 5 : link [currptr] = Newnode;
Step 6 : link [Newnode] = NULL
Step 7 : Exit
• Algorithm for Inserting at certain position :
Step 1: Set CURRPTR to Start
Currptr=Start
Step 2: If the item is to be inserted at 1st position
if (pos==1)
Algorithm for inserting at beginning
Else go to step 3
Step 3: If the item to inserted at some position other than 1st position
for ( i=0; i < pos-2;i++)
{
Currptr= LINK[Currptr]
}
Step 4: Create a Newnode
Newnode=getnode();
Step 5: Enter the element in the INFO field of Newnode
INFO[Newnode]=ITEM;
Step 6: Make a connection between the Newnode and the next node after
CURRPTR
LINK[Newnode]=LINK[Currptr]
Step 7: Make a link between Currptr and newnode
LINK[Currptr]=Newnode;
Step 8 : Exit
• Delete a node from the beginning:
• Algorithm:
Step 1 : check whether the list is empty
if ( start==NULL)
• write “ linked list is empty”
• else goto step 2
Step 2 : Set Currptr to start
Currptr=start;
Step 3 : Assign the link field of the first node to ‘start’ to delete the
original first node
start = link[start];
Step 4 : Free the deleted item
free(Currptr);
Step 5 : Exit
• Delete a node At the end:
• Algorithm:
Step 1 : check whether the list is empty
if ( start==NULL)
• write “ linked list is empty”
• else goto step 2
Step 2 : check if the list has only one element
if ( Link[start]==NULL) then
start=NULL;
else goto step 4
Step 3 : free the deleted node
free(start);
Step 4 : Assign start value to Currptr
Currptr=start;
Step 5 : Assign NULL to the another pointer PREVPTR
PREVPTR = NULL
Step 6 : Traverse the list until the Currptr points to last node
and PREVPTR points to the last before node
While(Link[Currptr]!=NULL)
{
PREVPTR=Currptr;
Currptr = Currptr -> link;
Step 7 : To make the LINK part of the last node, after
deletion to NULL
PREVPTR->LINK=NULL
Step 8 : Exit
• Delete a node at the given position:
• Algorithm:
→ Step 1: Check for the position whether the deletion is at first position
if ( pos==1)
Algorithm for deleting a node at beginning
else goto Step 2
→ Step 2 : Set Currptr to Start
Currptr=start
→ Step 3: set PREVPTR to NULL
PREVPTR = NULL
→Step 4 : if the item to be deleted is not at first position
for ( i=1; i<pos; i++ )
{
PREVPTR=Currptr;
Currptr = Currptr->link
}
• → Step 5 : LINK of PREVPTR is assigned with Link of
Currptr i.e. … make a link between the PREVPTR and the
next node of Currptr
PREVPTR → Link = Currptr→link
Step 6 : Exit
• Searching in a single linked list
• Algorithm :
• Step 1: set Currptr=start LOC = NULL
• Step 2: Repeat step 3 while (Currptr !=NULL)
• Step 3: if (item == Info [Currptr] )
– Then LOC = Currptr
– And display “ Search Successful “
– Exit
– else
– Currptr = Link [ Currptr ]
[ so Currptr, Now points to the next node ]
• Step 4: if LOC = NULL
Display “ Search unsuccessful “
• Step 5 : Exit
• Garbage Collection:
Garbage collection is the automatic reclamation of computer storage
The GC function is to find data objects that are no longer in use and make their
space available by the running program.
So Why Garbage Collection:
A software routine operating on data structure should not have to depend
what other routines may be operating on the same structure.
If the process does not free used memory the unused space is accumulated
until the process terminates.
Garbage collection is considered cheaper than explicit deal location
A good garbage collector shows a program down by factor of 10 percent.
Although it seems a lot , it is only a small price to pay for :
Convenience
Development time
Reliability

Más contenido relacionado

Similar a algorithms_in_linkedlist (1).pdf (20)

Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Data Structure
Data StructureData Structure
Data Structure
 
Singly link list
Singly link listSingly link list
Singly link list
 
Linked list
Linked listLinked list
Linked list
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Lect 11-12 Zaheer Abbas
Lect 11-12 Zaheer AbbasLect 11-12 Zaheer Abbas
Lect 11-12 Zaheer Abbas
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
Linked list
Linked listLinked list
Linked list
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
queue
queuequeue
queue
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
1. 3 singly linked list insertion 2
1. 3 singly linked list   insertion 21. 3 singly linked list   insertion 2
1. 3 singly linked list insertion 2
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Linked list
Linked listLinked list
Linked list
 

Más de Koteswari Kasireddy

Estimation & Hypothesis Testing - General.pdf
Estimation & Hypothesis Testing - General.pdfEstimation & Hypothesis Testing - General.pdf
Estimation & Hypothesis Testing - General.pdfKoteswari Kasireddy
 
Chapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdfChapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdfKoteswari Kasireddy
 
Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfKoteswari Kasireddy
 
Relational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptxRelational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptxKoteswari Kasireddy
 
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Unit 4 chapter - 8 Transaction processing Concepts (1).pptxUnit 4 chapter - 8 Transaction processing Concepts (1).pptx
Unit 4 chapter - 8 Transaction processing Concepts (1).pptxKoteswari Kasireddy
 
Database System Concepts AND architecture [Autosaved].pptx
Database System Concepts AND architecture [Autosaved].pptxDatabase System Concepts AND architecture [Autosaved].pptx
Database System Concepts AND architecture [Autosaved].pptxKoteswari Kasireddy
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptxKoteswari Kasireddy
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxparts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxKoteswari Kasireddy
 

Más de Koteswari Kasireddy (20)

DA Syllabus outline (2).pptx
DA Syllabus outline (2).pptxDA Syllabus outline (2).pptx
DA Syllabus outline (2).pptx
 
Estimation & Hypothesis Testing - General.pdf
Estimation & Hypothesis Testing - General.pdfEstimation & Hypothesis Testing - General.pdf
Estimation & Hypothesis Testing - General.pdf
 
Chapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdfChapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdf
 
Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
 
unit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdfunit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdf
 
DBMS_UNIT_1.pdf
DBMS_UNIT_1.pdfDBMS_UNIT_1.pdf
DBMS_UNIT_1.pdf
 
business analytics
business analyticsbusiness analytics
business analytics
 
Relational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptxRelational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptx
 
CHAPTER -12 it.pptx
CHAPTER -12 it.pptxCHAPTER -12 it.pptx
CHAPTER -12 it.pptx
 
WEB_DATABASE_chapter_4.pptx
WEB_DATABASE_chapter_4.pptxWEB_DATABASE_chapter_4.pptx
WEB_DATABASE_chapter_4.pptx
 
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Unit 4 chapter - 8 Transaction processing Concepts (1).pptxUnit 4 chapter - 8 Transaction processing Concepts (1).pptx
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
 
Database System Concepts AND architecture [Autosaved].pptx
Database System Concepts AND architecture [Autosaved].pptxDatabase System Concepts AND architecture [Autosaved].pptx
Database System Concepts AND architecture [Autosaved].pptx
 
Evolution Of WEB_students.pptx
Evolution Of WEB_students.pptxEvolution Of WEB_students.pptx
Evolution Of WEB_students.pptx
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxparts_of_python_programming_language.pptx
parts_of_python_programming_language.pptx
 
linked_list.pptx
linked_list.pptxlinked_list.pptx
linked_list.pptx
 
matrices_and_loops.pptx
matrices_and_loops.pptxmatrices_and_loops.pptx
matrices_and_loops.pptx
 

Último

Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxMetabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxDr. Santhosh Kumar. N
 
3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptx3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptxmary850239
 
UNIT I Design Thinking and Explore.pptx
UNIT I  Design Thinking and Explore.pptxUNIT I  Design Thinking and Explore.pptx
UNIT I Design Thinking and Explore.pptxGOWSIKRAJA PALANISAMY
 
Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024bsellato
 
3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptx3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptxmary850239
 
Metabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptxMetabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptxDr. Santhosh Kumar. N
 
AI Uses and Misuses: Academic and Workplace Applications
AI Uses and Misuses: Academic and Workplace ApplicationsAI Uses and Misuses: Academic and Workplace Applications
AI Uses and Misuses: Academic and Workplace ApplicationsStella Lee
 
Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...AKSHAYMAGAR17
 
Plant Tissue culture., Plasticity, Totipotency, pptx
Plant Tissue culture., Plasticity, Totipotency, pptxPlant Tissue culture., Plasticity, Totipotency, pptx
Plant Tissue culture., Plasticity, Totipotency, pptxHimansu10
 
Dhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian PoeticsDhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian PoeticsDhatriParmar
 
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in PharmacyASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in PharmacySumit Tiwari
 
How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17
How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17
How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17Celine George
 
3.14.24 Gender Discrimination and Gender Inequity.pptx
3.14.24 Gender Discrimination and Gender Inequity.pptx3.14.24 Gender Discrimination and Gender Inequity.pptx
3.14.24 Gender Discrimination and Gender Inequity.pptxmary850239
 
Material Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.pptMaterial Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.pptBanaras Hindu University
 
ICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfVanessa Camilleri
 
2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...
2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...
2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...Marlene Maheu
 
BBA 205 BE UNIT 2 economic systems prof dr kanchan.pptx
BBA 205 BE UNIT 2 economic systems prof dr kanchan.pptxBBA 205 BE UNIT 2 economic systems prof dr kanchan.pptx
BBA 205 BE UNIT 2 economic systems prof dr kanchan.pptxProf. Kanchan Kumari
 
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdfArti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdfwill854175
 

Último (20)

Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxMetabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
 
3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptx3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptx
 
UNIT I Design Thinking and Explore.pptx
UNIT I  Design Thinking and Explore.pptxUNIT I  Design Thinking and Explore.pptx
UNIT I Design Thinking and Explore.pptx
 
Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024
 
3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptx3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptx
 
Metabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptxMetabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptx
 
AI Uses and Misuses: Academic and Workplace Applications
AI Uses and Misuses: Academic and Workplace ApplicationsAI Uses and Misuses: Academic and Workplace Applications
AI Uses and Misuses: Academic and Workplace Applications
 
Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
 
Plant Tissue culture., Plasticity, Totipotency, pptx
Plant Tissue culture., Plasticity, Totipotency, pptxPlant Tissue culture., Plasticity, Totipotency, pptx
Plant Tissue culture., Plasticity, Totipotency, pptx
 
Dhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian PoeticsDhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian Poetics
 
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in PharmacyASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
 
How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17
How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17
How to Customise Quotation's Appearance Using PDF Quote Builder in Odoo 17
 
3.14.24 Gender Discrimination and Gender Inequity.pptx
3.14.24 Gender Discrimination and Gender Inequity.pptx3.14.24 Gender Discrimination and Gender Inequity.pptx
3.14.24 Gender Discrimination and Gender Inequity.pptx
 
Material Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.pptMaterial Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.ppt
 
Problems on Mean,Mode,Median Standard Deviation
Problems on Mean,Mode,Median Standard DeviationProblems on Mean,Mode,Median Standard Deviation
Problems on Mean,Mode,Median Standard Deviation
 
ICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdf
 
2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...
2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...
2024 March 11, Telehealth Billing- Current Telehealth CPT Codes & Telehealth ...
 
BBA 205 BE UNIT 2 economic systems prof dr kanchan.pptx
BBA 205 BE UNIT 2 economic systems prof dr kanchan.pptxBBA 205 BE UNIT 2 economic systems prof dr kanchan.pptx
BBA 205 BE UNIT 2 economic systems prof dr kanchan.pptx
 
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdfArti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
 

algorithms_in_linkedlist (1).pdf

  • 1. Creating a Linked list : Algorithm : Create first node and assign the address of the first node to the pointer ‘start’. Step : 1 start =getnode( ); Use another pointer to store the address of the first node, called CURRPTR Step : 2 currptr=Start; Accept the item and store it in the INFO field of the node Step : 3 info[currptr ]= item; Accept the Choice from the keyboard whether to create another node or not Step : 4 if ( choice = ‘Y’) goto step 5 else goto step 6 If choice is ‘Y’ to create another node
  • 2. Step : 5 i) Newnode = getnode( ); Newnode which contains the address of the newly created node ii) link[currptr]=Newnode; Making a link between the original last node and present newnode iii) currptr=Newnode; Address of the Newnode is assigned to the original newnode iv) info[currptr]=item; Accepting the item and storing it in the info field of currptr go to step 4 If choice is ‘N’ to make the LINK field of the last node to NULL Step : 6 link [currptr] = NULL and Exit Step : 7 return
  • 3. • Traversing a linked list : Algorithm: Step 1 : Is linked list is empty if ( start ==NULL) display (“ linked list is empty “); exit( ) Step 2 : Assign start value to CURRPTR currptr=start; Step 3 : Repeat while(currptr!=NULL) Process info[currptr] Step 4 : currptr=link[currptr] /* moving the currptr from one node to the next node */ Step 5 : Exit
  • 4. • Algorithm for Displaying a list : • first check list is empty or not • Step 1 : if ( start == NULL) – Write (“ list is Empty”) – return; Store the Address of first node in another pointer CURRPTR • Step 2 : set currptr = start; • Step 3 : Repeat step4 and step5 while ( currptr !=NULL) • Display items one-by-one from first node until Currptr becomes NULL • Step 4 : display ( info[currptr]); After the display of this element ,Currptr should point to the next node • Step 5 : currptr=link[currptr]; • Step 6 : Exit
  • 5. • Insert operation at the beginning : • Algorithm : • Creating a node that is to be inserted • step 1 : Newnode =getnode( ); • Enter the item into the Info field of the New node • step 2 : info [Newnode]= item; • Assign the start value to the LINK part of the inserted node(New node). • It makes a link from ‘New node’ to the previous first node. • Now start and New node points to the original first node of the list. • It makes link between start and Newnode. • step 3 : link [Newnode]=start; • step 4 : start = Newnode; Exit;
  • 6. • Algorithm : insert operation at end Step 1 : if list is empty if ( start == NULL) insert at the beginning exit( ) end if Step 2 : currptr = start; Traverse the list to obtain the last node address Step 3 : while (link [currptr]! = NULL) currptr = link [ currptr] end while Create a newnode and enter the accepted ITEM into its INFO fields Step 4 : Newnode =getnode( ); Newnode -> info =item; Make a link between Currptr(last node) and New node Step 5 : link [currptr] = Newnode; Step 6 : link [Newnode] = NULL Step 7 : Exit
  • 7. • Algorithm for Inserting at certain position : Step 1: Set CURRPTR to Start Currptr=Start Step 2: If the item is to be inserted at 1st position if (pos==1) Algorithm for inserting at beginning Else go to step 3 Step 3: If the item to inserted at some position other than 1st position for ( i=0; i < pos-2;i++) { Currptr= LINK[Currptr] } Step 4: Create a Newnode Newnode=getnode();
  • 8. Step 5: Enter the element in the INFO field of Newnode INFO[Newnode]=ITEM; Step 6: Make a connection between the Newnode and the next node after CURRPTR LINK[Newnode]=LINK[Currptr] Step 7: Make a link between Currptr and newnode LINK[Currptr]=Newnode; Step 8 : Exit
  • 9. • Delete a node from the beginning: • Algorithm: Step 1 : check whether the list is empty if ( start==NULL) • write “ linked list is empty” • else goto step 2 Step 2 : Set Currptr to start Currptr=start; Step 3 : Assign the link field of the first node to ‘start’ to delete the original first node start = link[start]; Step 4 : Free the deleted item free(Currptr); Step 5 : Exit
  • 10. • Delete a node At the end: • Algorithm: Step 1 : check whether the list is empty if ( start==NULL) • write “ linked list is empty” • else goto step 2 Step 2 : check if the list has only one element if ( Link[start]==NULL) then start=NULL; else goto step 4 Step 3 : free the deleted node free(start); Step 4 : Assign start value to Currptr Currptr=start; Step 5 : Assign NULL to the another pointer PREVPTR PREVPTR = NULL
  • 11. Step 6 : Traverse the list until the Currptr points to last node and PREVPTR points to the last before node While(Link[Currptr]!=NULL) { PREVPTR=Currptr; Currptr = Currptr -> link; Step 7 : To make the LINK part of the last node, after deletion to NULL PREVPTR->LINK=NULL Step 8 : Exit
  • 12. • Delete a node at the given position: • Algorithm: → Step 1: Check for the position whether the deletion is at first position if ( pos==1) Algorithm for deleting a node at beginning else goto Step 2 → Step 2 : Set Currptr to Start Currptr=start → Step 3: set PREVPTR to NULL PREVPTR = NULL →Step 4 : if the item to be deleted is not at first position for ( i=1; i<pos; i++ ) { PREVPTR=Currptr; Currptr = Currptr->link }
  • 13. • → Step 5 : LINK of PREVPTR is assigned with Link of Currptr i.e. … make a link between the PREVPTR and the next node of Currptr PREVPTR → Link = Currptr→link Step 6 : Exit
  • 14. • Searching in a single linked list • Algorithm : • Step 1: set Currptr=start LOC = NULL • Step 2: Repeat step 3 while (Currptr !=NULL) • Step 3: if (item == Info [Currptr] ) – Then LOC = Currptr – And display “ Search Successful “ – Exit – else – Currptr = Link [ Currptr ] [ so Currptr, Now points to the next node ] • Step 4: if LOC = NULL Display “ Search unsuccessful “ • Step 5 : Exit
  • 15. • Garbage Collection: Garbage collection is the automatic reclamation of computer storage The GC function is to find data objects that are no longer in use and make their space available by the running program. So Why Garbage Collection: A software routine operating on data structure should not have to depend what other routines may be operating on the same structure. If the process does not free used memory the unused space is accumulated until the process terminates. Garbage collection is considered cheaper than explicit deal location A good garbage collector shows a program down by factor of 10 percent. Although it seems a lot , it is only a small price to pay for : Convenience Development time Reliability