Inserting an item at the end of the linked list

The effect of addLast(h,x) --- inserting item x at the end of the linked list h:

  • Example: the linked list before the insertion of the item 5:

  • The linked list after the insertion of 5 is:

  • Insert at end algorithm:

       (1) Find the last element in the linked list
       (2) Make a new Node with data and link to the last element
       (3) Return the new first node
    

Algorithm to find the last node in a linked list

  • The algorithm to find the last node in a list is based on list traversal:

  • The generic list traversal algorithm (goes through all nodes in list):

       struct Node *current = first;
    
       while ( current != NULL ) 
       {
           current = current->next;
       }
    
       // We will have visited all nodes in the list
    

Algorithm to find the last node in a linked list

  • To find the last node, we exit the (search) loop when current->next == NULL:

  • Algorithm to find the last node in a list:

       struct Node *current = first;
    
       while ( current != NULL && current->next != NULL ) 
       {
           current = current->next;
       }
    
       // current points to the last node
    

Algorithm to find the last node in a linked list

  • Because the list traversal always stops before current == NULL:

    Algorithm to find the last node in a list can be simplified:

       struct Node *current = first;
    
       while ( current->next != NULL ) 
       {
           current = current->next;
       }
    
       // current points to the last node
    

Implementation of addLast(h, x)

 

Let's implement the addLast(h, x) method:

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;

   while ( current->next != NULL )   // Find last list element
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new firstnode
}

Implementation of addLast(h, x)

 

Start the search at the first node in the linked list:

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;  // h = first node in list

   while ( current->next != NULL )   // Find last list element
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new firstnode
}

Implementation of addLast(h, x)

 

Find the last element in the linked list:   the next variable in this element is NULL

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;  // h = first node in list

   while ( current->next != NULL ) // Exit loop when last list elem found
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new firstnode
}

Implementation of addLast(h, x)

 

Make a new Node containing the item = x and next = NULL (because it will be the last element)

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;    newNode->next = NULL;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;  // h = first node in list

   while ( current->next != NULL ) // Exit loop when last list elem found
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new firstnode
}

Implementation of addLast(h, x)

 

Link the new Node to the last element in the list:

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;    newNode->next = NULL;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;  // h = first node in list

   while ( current->next != NULL ) // Exit loop when last list elem found
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new firstnode
}

Implementation of addLast(h, x)

 

Return the new first element:

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;    newNode->next = NULL;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;  // h = first node in list

   while ( current->next != NULL ) // Exit loop when last list elem found
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new first node
}

Implementation of addLast(h, x)

 

Check for edge case(s): an empty list

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;    newNode->next = NULL;

   if ( h == NULL )
   {
      return newNode;        // Return the new firstnode
   } 

   struct Node *current = h;       // Edge case: h = NULL

   while ( current->next != NULL ) // null->next will cause a crash !!
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new first node
}

Implementation of addLast(h, x)

 

We must handle the edge case separately:

struct Node *addLast(struct Node *h, int x)
{
   struct Node *newNode = malloc( sizeof(struct Node) );
   newNode->item = x;    newNode->next = NULL;

   if ( h == NULL )
   {
      return newNode;        // Return the new first node
   } 

   struct Node *current = h;       // h != NULL

   while ( current->next != NULL ) 
   {
      current = current->next;
   }
   current->next = newNode;  // Link new node after last node
   return h;                 // Return the new first node
}

Demo program

struct Node
{
   int          item;
   struct Node *next;
};

int main()
{
   struct Node *head = NULL;
   int k;

   printList(head);

   for (k = 1; k < 10; k++ )
   {
      head = addLast(head, k);
      printList(head);
   }
}

DEMO: demo/C/Linked-list/addLast.c