SlideShare a Scribd company logo
DATA STRUCTURES
Dr. P. Subathra
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 3
18.08.2020
(04:00 PM – 05:00 PM)
UNIT 1
SINGLY LINKED LIST
INSERT
AFTER
AN ELEMENT
INSERT AFTER AN ELEMENT
Algorithm :
• STEP 1 : Dynamically generate a new node.
• STEP 2 : Get the value in the node’s data field.
• STEP 3 :
i. Check if the list is empty, if so display a message and
return.
ii. Traverse the list until the specified node is reached
or End of List is reached.
a. If element is found, insert the new element after the
current node
b. Else if end of list is reached, display a message of
ELEMENT NOT FOUND and return
• Initial List
• Insert 8 after 6
INSERT AFTER AN ELEMENT
• Initial List
• Insert 8 after 6
• Final List
INSERT AFTER AN ELEMENT
ALGORITHM
STEP 1 : Dynamically generate a
new node.
STEP 2 : Get the value in the
node’s data field.
CODE & ILLUSTRATION
INSERT AFTER AN ELEMENT
ALGORITHM
STEP 3:
i. Check if the list is empty,
if so, display a message and
return
CODE & ILLUSTRATION
INSERT AFTER AN ELEMENT
if (head==NULL)
{
prinft(“Empty List”);
return(); //optional
}
NULL
head
ALGORITHM
STEP 3 :
ii. Else traverse the list until the
specified node is reached or
End of List is reached.
a. If element is found, insert the
new element after the current node
b. Else if end of list is reached,
display a message of ELEMENT NOT
FOUND and return
CODE & ILLUSTRATION
INSERT AFTER AN ELEMENT
else
{
node * current = head;
while (current  data ! = searchElement
|| currentnext!=NULL)
{
current = current  next;
}
}
ALGORITHM & CODESTEP 3 :
ii. Else traverse the list until the specified node
is reached or End of List is reached.
a. If element is found, insert the
new element after the current node
b. Else if end of list is reached,
display a message of ELEMENT NOT
FOUND and return
ILLUSTRATION
INSERT AFTER AN ELEMENT
else
{
node * current = head; while (current  data ! = searchElement
|| currentnext!=NULL) { current = current  next; }
if ( current data == element)
{
tempnext=current->next;
currentnext=temp;
}
else
printf(“Element not found”);
}
• Initial List
• Insert 8 after 6
• Final List
INSERT AFTER AN ELEMENT
INSERT
BEFORE
AN ELEMENT
INSERT BEFORE AN ELEMENT
Algorithm :
• STEP 1 : Dynamically create a new node
• STEP 2 : Get the value in the data field of the new node.
• STEP 3:
i. Check if the list is empty, if so display a message and return.
ii. Check if the node is available as the first node
a. Perform Insert First operation
iii. Traverse the list until the specified node is reached or End of
List is reached. While traversing, keep track of the previous
node also.
a. If element is found, insert the new element after the previous node
b. Else if end of list is reached, display a message of ELEMENT NOT
FOUND and return
ALGORITHM
STEP 1 : Dynamically generate a
new node.
STEP 2 : Get the value in the
node’s data field.
CODE & ILLUSTRATION
INSERT BEFORE AN ELEMENT
ALGORITHM
STEP 3:
i. Check if the list is empty,
if so, display a message and
return
CODE & ILLUSTRATION
INSERT BEFORE AN ELEMENT
if (head==NULL)
{
prinft(“Empty List”);
return(); //
}
NULL
head
ALGORITHM & CODE
STEP 3:
ii. Else check if the node is
available as the first node
a. Perform Insert First operation
(eg) Insert after 7
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else if (head-> data==element)
{
temp->next=head;
head=temp;
}
NULL
NULL
NULL
ALGORITHM & CODE
STEP 3:
iii. Else Traverse the list until the specified node is
reached or End of List is reached. While
traversing, keep track of the previous node
also.
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = element
|| current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
}
ALGORITHM & CODE
STEP 3: iii.
a. If element is found, insert the new
element after the previous node
b. Else if end of list is reached, display a
message of ELEMENT NOT FOUND
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = element
|| current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
if(current->data==element)
{
temp->next=prev->next;
prev->next = temp;
}
else
printf(“Element not found”);
}
ALGORITHM & CODE
STEP 3: iii.
a. If element is found, insert the new
element after the previous node
b. Else if end of list is reached, display a
message of ELEMENT NOT FOUND
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = element
|| current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
if(current->data==element)
{
temp->next=prev->next;
prev->next = temp;
}
else
printf(“Element not found”);
}
INSERT BEFORE AN ELEMENT
INSERT AT
A POSITION
INSERT AT A POSITION
Algorithm :
STEP 1 : Dynamically create a new node.
STEP 2 : Read the new value into the element field of the new node.
STEP 3:
Let k= POSITION
i. Check if the list is empty, if so display a message and return.
ii. Check if k==1 (First Position)
a. Perform Insert First operation
iii. Traverse the list until the specified (k -1)th node is reached or End
of List is reached
a. If (k -1) is available in the list, then, insert the new element after the (k -1)th
node
Copy the (k-1)th nodes next field value into the next field of the new node.
Make the (k-1)th node to point to this new node.
b. Else if end of list is reached, display a message of ELEMENT NOT FOUND and
return
ALGORITHM
STEP 1 : Dynamically generate a
new node.
STEP 2 : Get the value in the
node’s data field.
CODE & ILLUSTRATION
INSERT AT A POSITION
ALGORITHM
STEP 3:
i. Check if the list is empty,
if so, display a message and
return
CODE & ILLUSTRATION
if (head==NULL)
{
prinft(“Empty List”);
return(); //
}
NULL
head
INSERT AT A POSITION
ALGORITHM & CODE
STEP 3:
ii. Check if k==1 (First Position)
a. Perform Insert First operation
ILLUSTRATION
else if (k== 1)
{
temp->next=head;
head=temp;
}
NULL
NULL
NULL
INSERT AT A POSITION
ALGORITHM & CODE
STEP 3:
iii.
Traverse the list until the specified (k -1)th
node is reached or End of List is reached
(eg) Let Position = 3
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
count =1; k = position;
current = head;
while (count <=(k-1) ||
current->next!=NULL)
{
current= current-> next;
count++;
}
}
ALGORITHM & CODE
STEP 3:
iii.
Traverse the list until the specified (k -1)th
node is reached or End of List is reached
(eg) Let Position = 3
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
count =1; k = position;
current = head;
while (count <=(k-1) ||
current->next!=NULL)
{
current= current-> next;
count++;
}
temp->next = current-> next;
current-> next = temp
}
ALGORITHM & CODE
STEP 3:
iii.
Traverse the list until the specified (k -1)th
node is reached or End of List is reached
(eg) Let Position = 3
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
count =1; k = position;
current = head;
while (count <=(k-1) ||
current->next!=NULL)
{
current= current-> next;
count++;
}
}
NULL
ALGORITHM & CODE
STEP 3:
iii. Traverse the list until the specified (k -1)th
node is reached or End of List is reached
(eg) Let Position = 3
ILLUSTRATION
INSERT BEFORE AN ELEMENT
else
{
count =1; k = position;
current = head;
while (count <=(k-1) ||
current->next!=NULL)
{
current= current-> next;
count++;
}
temp->next = current-> next;
current-> next = temp
}
NULL
NULL
NULL
NULL
NULL
END
OF
INSERTION
OPERATIONS

More Related Content

What's hot (20)

PPTX
stacks and queues
EktaVaswani2
 
PDF
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
PPTX
The Stack And Recursion
Ashim Lamichhane
 
PPTX
Stacks in c++
Vineeta Garg
 
PPT
Stacks, Queues, Deques
A-Tech and Software Development
 
PDF
stacks and queues
DurgaDeviCbit
 
PPTX
Stack project
Amr Aboelgood
 
PPTX
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
PPTX
Data structure by Digvijay
Digvijay Singh Karakoti
 
PDF
Stack
Zaid Shabbir
 
PPT
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
PPTX
Stack in Sata Structure
Muhazzab Chouhadry
 
PPTX
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
PPT
Queue Data Structure
Sriram Raj
 
PPT
Stack a Data Structure
ForwardBlog Enewzletter
 
PPT
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
PPTX
Stack - Data Structure
Bhavesh Sanghvi
 
PPTX
My lecture stack_queue_operation
Senthil Kumar
 
PPT
Stacks overview with its applications
Saqib Saeed
 
PPT
Stack & queue
Siddique Ibrahim
 
stacks and queues
EktaVaswani2
 
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
The Stack And Recursion
Ashim Lamichhane
 
Stacks in c++
Vineeta Garg
 
Stacks, Queues, Deques
A-Tech and Software Development
 
stacks and queues
DurgaDeviCbit
 
Stack project
Amr Aboelgood
 
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
Data structure by Digvijay
Digvijay Singh Karakoti
 
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Stack in Sata Structure
Muhazzab Chouhadry
 
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Queue Data Structure
Sriram Raj
 
Stack a Data Structure
ForwardBlog Enewzletter
 
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Stack - Data Structure
Bhavesh Sanghvi
 
My lecture stack_queue_operation
Senthil Kumar
 
Stacks overview with its applications
Saqib Saeed
 
Stack & queue
Siddique Ibrahim
 

Similar to 1. 3 singly linked list insertion 2 (20)

PPTX
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPTX
Linked lists a
Khuram Shahzad
 
PPT
Linked list introduction and different operation
HODCSE170941
 
PDF
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
PPTX
VCE Unit 02 (1).pptx
skilljiolms
 
PPTX
Dounly linked list
NirmalPandey23
 
PPTX
GDSC MPSTME Shirpur DSA Day 1.pptx
amanbhogal7
 
DOC
Final ds record
Ganisius Ganish
 
PPT
Operations on linked list
Sumathi Kv
 
PPT
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
PPTX
Presentation1.pptx
Koteswari Kasireddy
 
PPTX
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
PPTX
5.Linked list
Mandeep Singh
 
PPTX
data structure3.pptx
SajalFayyaz
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
PPT
lecture four of data structures :Linked List-ds.ppt
donemoremaregere376
 
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Unit ii(dsc++)
Durga Devi
 
Linked lists a
Khuram Shahzad
 
Linked list introduction and different operation
HODCSE170941
 
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
VCE Unit 02 (1).pptx
skilljiolms
 
Dounly linked list
NirmalPandey23
 
GDSC MPSTME Shirpur DSA Day 1.pptx
amanbhogal7
 
Final ds record
Ganisius Ganish
 
Operations on linked list
Sumathi Kv
 
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
Presentation1.pptx
Koteswari Kasireddy
 
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
5.Linked list
Mandeep Singh
 
data structure3.pptx
SajalFayyaz
 
single linked list
Sathasivam Rangasamy
 
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
lecture four of data structures :Linked List-ds.ppt
donemoremaregere376
 
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
Ad

More from P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai (20)

PPTX
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPTX
1. C Basics for Data Structures Bridge Course
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
The stable marriage problem iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Maximum matching in bipartite graphs iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Knapsack dynamic programming formula top down (1)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Knapsack dynamic programming formula bottom up
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Multiplication of integers &amp; strassens matrix multiplication subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Multiplication of large integers problem subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
The stable marriage problem iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Maximum matching in bipartite graphs iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Knapsack dynamic programming formula top down (1)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Multiplication of integers &amp; strassens matrix multiplication subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Multiplication of large integers problem subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Ad

Recently uploaded (20)

PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Introduction to Basic Renewable Energy.pptx
examcoordinatormesu
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Introduction to Basic Renewable Energy.pptx
examcoordinatormesu
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Design Thinking basics for Engineers.pdf
CMR University
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Big Data and Data Science hype .pptx
SUNEEL37
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 

1. 3 singly linked list insertion 2

  • 1. DATA STRUCTURES Dr. P. Subathra Professor Dept. of Information Technology KAMARAJ College of Engineering & Technology
  • 2. CS8391 – DATA STRUCTURES ONLINE CLASSES – CLASS NO. 3 18.08.2020 (04:00 PM – 05:00 PM)
  • 5. INSERT AFTER AN ELEMENT Algorithm : • STEP 1 : Dynamically generate a new node. • STEP 2 : Get the value in the node’s data field. • STEP 3 : i. Check if the list is empty, if so display a message and return. ii. Traverse the list until the specified node is reached or End of List is reached. a. If element is found, insert the new element after the current node b. Else if end of list is reached, display a message of ELEMENT NOT FOUND and return
  • 6. • Initial List • Insert 8 after 6 INSERT AFTER AN ELEMENT
  • 7. • Initial List • Insert 8 after 6 • Final List INSERT AFTER AN ELEMENT
  • 8. ALGORITHM STEP 1 : Dynamically generate a new node. STEP 2 : Get the value in the node’s data field. CODE & ILLUSTRATION INSERT AFTER AN ELEMENT
  • 9. ALGORITHM STEP 3: i. Check if the list is empty, if so, display a message and return CODE & ILLUSTRATION INSERT AFTER AN ELEMENT if (head==NULL) { prinft(“Empty List”); return(); //optional } NULL head
  • 10. ALGORITHM STEP 3 : ii. Else traverse the list until the specified node is reached or End of List is reached. a. If element is found, insert the new element after the current node b. Else if end of list is reached, display a message of ELEMENT NOT FOUND and return CODE & ILLUSTRATION INSERT AFTER AN ELEMENT else { node * current = head; while (current  data ! = searchElement || currentnext!=NULL) { current = current  next; } }
  • 11. ALGORITHM & CODESTEP 3 : ii. Else traverse the list until the specified node is reached or End of List is reached. a. If element is found, insert the new element after the current node b. Else if end of list is reached, display a message of ELEMENT NOT FOUND and return ILLUSTRATION INSERT AFTER AN ELEMENT else { node * current = head; while (current  data ! = searchElement || currentnext!=NULL) { current = current  next; } if ( current data == element) { tempnext=current->next; currentnext=temp; } else printf(“Element not found”); }
  • 12. • Initial List • Insert 8 after 6 • Final List INSERT AFTER AN ELEMENT
  • 14. INSERT BEFORE AN ELEMENT Algorithm : • STEP 1 : Dynamically create a new node • STEP 2 : Get the value in the data field of the new node. • STEP 3: i. Check if the list is empty, if so display a message and return. ii. Check if the node is available as the first node a. Perform Insert First operation iii. Traverse the list until the specified node is reached or End of List is reached. While traversing, keep track of the previous node also. a. If element is found, insert the new element after the previous node b. Else if end of list is reached, display a message of ELEMENT NOT FOUND and return
  • 15. ALGORITHM STEP 1 : Dynamically generate a new node. STEP 2 : Get the value in the node’s data field. CODE & ILLUSTRATION INSERT BEFORE AN ELEMENT
  • 16. ALGORITHM STEP 3: i. Check if the list is empty, if so, display a message and return CODE & ILLUSTRATION INSERT BEFORE AN ELEMENT if (head==NULL) { prinft(“Empty List”); return(); // } NULL head
  • 17. ALGORITHM & CODE STEP 3: ii. Else check if the node is available as the first node a. Perform Insert First operation (eg) Insert after 7 ILLUSTRATION INSERT BEFORE AN ELEMENT else if (head-> data==element) { temp->next=head; head=temp; } NULL NULL NULL
  • 18. ALGORITHM & CODE STEP 3: iii. Else Traverse the list until the specified node is reached or End of List is reached. While traversing, keep track of the previous node also. ILLUSTRATION INSERT BEFORE AN ELEMENT else { node * previous=head; node * current = head-> next; while (current->data! = element || current->next!=NULL) { current= current-> next; previous = previous->next; } }
  • 19. ALGORITHM & CODE STEP 3: iii. a. If element is found, insert the new element after the previous node b. Else if end of list is reached, display a message of ELEMENT NOT FOUND ILLUSTRATION INSERT BEFORE AN ELEMENT else { node * previous=head; node * current = head-> next; while (current->data! = element || current->next!=NULL) { current= current-> next; previous = previous->next; } if(current->data==element) { temp->next=prev->next; prev->next = temp; } else printf(“Element not found”); }
  • 20. ALGORITHM & CODE STEP 3: iii. a. If element is found, insert the new element after the previous node b. Else if end of list is reached, display a message of ELEMENT NOT FOUND ILLUSTRATION INSERT BEFORE AN ELEMENT else { node * previous=head; node * current = head-> next; while (current->data! = element || current->next!=NULL) { current= current-> next; previous = previous->next; } if(current->data==element) { temp->next=prev->next; prev->next = temp; } else printf(“Element not found”); }
  • 21. INSERT BEFORE AN ELEMENT
  • 23. INSERT AT A POSITION Algorithm : STEP 1 : Dynamically create a new node. STEP 2 : Read the new value into the element field of the new node. STEP 3: Let k= POSITION i. Check if the list is empty, if so display a message and return. ii. Check if k==1 (First Position) a. Perform Insert First operation iii. Traverse the list until the specified (k -1)th node is reached or End of List is reached a. If (k -1) is available in the list, then, insert the new element after the (k -1)th node Copy the (k-1)th nodes next field value into the next field of the new node. Make the (k-1)th node to point to this new node. b. Else if end of list is reached, display a message of ELEMENT NOT FOUND and return
  • 24. ALGORITHM STEP 1 : Dynamically generate a new node. STEP 2 : Get the value in the node’s data field. CODE & ILLUSTRATION INSERT AT A POSITION
  • 25. ALGORITHM STEP 3: i. Check if the list is empty, if so, display a message and return CODE & ILLUSTRATION if (head==NULL) { prinft(“Empty List”); return(); // } NULL head INSERT AT A POSITION
  • 26. ALGORITHM & CODE STEP 3: ii. Check if k==1 (First Position) a. Perform Insert First operation ILLUSTRATION else if (k== 1) { temp->next=head; head=temp; } NULL NULL NULL INSERT AT A POSITION
  • 27. ALGORITHM & CODE STEP 3: iii. Traverse the list until the specified (k -1)th node is reached or End of List is reached (eg) Let Position = 3 ILLUSTRATION INSERT BEFORE AN ELEMENT else { count =1; k = position; current = head; while (count <=(k-1) || current->next!=NULL) { current= current-> next; count++; } }
  • 28. ALGORITHM & CODE STEP 3: iii. Traverse the list until the specified (k -1)th node is reached or End of List is reached (eg) Let Position = 3 ILLUSTRATION INSERT BEFORE AN ELEMENT else { count =1; k = position; current = head; while (count <=(k-1) || current->next!=NULL) { current= current-> next; count++; } temp->next = current-> next; current-> next = temp }
  • 29. ALGORITHM & CODE STEP 3: iii. Traverse the list until the specified (k -1)th node is reached or End of List is reached (eg) Let Position = 3 ILLUSTRATION INSERT BEFORE AN ELEMENT else { count =1; k = position; current = head; while (count <=(k-1) || current->next!=NULL) { current= current-> next; count++; } } NULL
  • 30. ALGORITHM & CODE STEP 3: iii. Traverse the list until the specified (k -1)th node is reached or End of List is reached (eg) Let Position = 3 ILLUSTRATION INSERT BEFORE AN ELEMENT else { count =1; k = position; current = head; while (count <=(k-1) || current->next!=NULL) { current= current-> next; count++; } temp->next = current-> next; current-> next = temp } NULL NULL NULL NULL NULL