1. 3 singly linked list insertion 2

83 views 31 slides Aug 18, 2020
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

singly linked list insertion 2


Slide Content

DATA STRUCTURES Dr. P. Subathra Professor Dept. of Information Technology KAMARAJ College of Engineering & Technology

CS8 391 – 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 : Check if the list is empty, if so display a message and return. Traverse the list until the specified node is reached or End of List is reached . If element is found, insert the new element after the current node 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) { p rinft (“Empty List”); return(); //optional } NULL head

ALGORITHM STEP 3 : 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 & CODE STEP 3 : 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 p rintf (“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 : Check if the list is empty, if so display a message and return. Check if the node is available as the first node Perform Insert First operation Traverse the list until the specified node is reached or End of List is reached . While traversing, keep track of the previous node also .   If element is found, insert the new element after the previous node 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) { p rinft (“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 e lse 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 p rintf (“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 p rintf (“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 Check if the list is empty, if so display a message and return. Check if k==1 (First Position) Perform Insert First operation Traverse the list until the specified (k -1) th node is reached or End of List is reached   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. 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) { p rinft (“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 e lse 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