This document discusses inserting a node into a linked list at the end. It explains that to insert a node at the end, you first check if the list is empty and if so make the new node the start. If the list is not empty, you iterate through the list with a pointer ptr until ptr->next is NULL, at which point you set ptr->next to the new node and new_node->next to NULL. Diagrams are included to visualize the process. The algorithm runs in O(n) time where n is the number of nodes in the list.
1. 11/28/2020 (c) Dr. Jyoti Lakhani 1
Linked List
Linear Data Structure
Store more than one value in it (Same as array)
11 12 13
start First Node Last Node
pointer
Node Node Node
NULL
2. 8/14/2020 2
Done so far...
Create a Node
Insert Node at Beginning of Linked List
Insert Node at End of Linked List
Insert at a given Location
Insert after or before a given Node
Deletion from Beginning
Deletion from End
Deletion from a given location
Deletion from before or after a given node
Search an item in Linked List
Traverse Linked List
Display Linked List
Count nodes in a Linked List
3. 8/14/2020 (c) Dr. Jyoti Lakhani 3
Insert a Node in Linked List at the End
Algorithm : Insert_End(new_node)
Input : A node to be inserted in the Linked List
Output: Linked List with new node at the beginning
Steps:
1. [Case 1: List is empty]
If start == NULL then
start= new_node
2. [Case 2: List if Non- Empty]
start
NULL
11
new_node
NULL
4. Algorithm : Insert_End(new_node)
Input : A node to be inserted in the Linked List
Output: Linked List with new node at the beginning
Steps:
1. [Case 1: List is empty]
If start == NULL then
start= new_node
2. [Case 2: List if Non- Empty]
//Reach at the at last Node
ptr = start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next = NULL;
8/14/2020 (c) Dr. Jyoti Lakhani 4
Insert a Node in Linked List at the End
5. 8/14/2020 5
11 12 13
start First Node Last Node
NULL
10
new_node
Insert a Node in Linked List at the End
6. 8/14/2020 6
11 12 13
start First Node Last Node
10
new_node
Insert a Node in Linked List at the End
7. 8/14/2020 7
11 12 13
start First Node Last Node
NULL
10
new_node
Insert a Node in Linked List at the End
ptr
ptr = start
while(ptr->next!=NULL)
ptr=ptr->next;
8. 8/14/2020 8
11 12 13
start First Node Last Node
Insert a Node in Linked List at the End
ptr
while(ptr->next!=NULL)
ptr=ptr->next;
NULL
10
new_node
9. 8/14/2020 9
11 12 13
start First Node Last Node
NULL
10
new_node
Insert a Node in Linked List at the End
ptr
while(ptr->next!=NULL)
ptr=ptr->next;
10. 8/14/2020 10
11 12 13
start First Node Last Node
10
new_node
Insert a Node in Linked List at the End
ptr
while(ptr->next!=NULL)
ptr=ptr->next;