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

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 – DATASTRUCTURES ONLINE CLASSES – CLASS NO. 3 18.08.2020 (04:00 PM – 05:00 PM)
  • 3.
  • 4.
  • 5.
    INSERT AFTER ANELEMENT 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. Checkif 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 & CODESTEP3 : 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
  • 13.
  • 14.
    INSERT BEFORE ANELEMENT 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. Checkif 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 STEP3: 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 STEP3: 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 STEP3: 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 STEP3: 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.
  • 22.
  • 23.
    INSERT AT APOSITION 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. Checkif 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 STEP3: 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 STEP3: 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 STEP3: 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 STEP3: 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 STEP3: 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
  • 31.