Insert Node at the End of Singly Linked List

Insert Node at the End of Singly Linked List” is very basic programming problem based on. of linked list data structures. Here, we are given a linked list and our task. is to add a new node is added at the end of the linked list. 

Example:

Current Linked List: 
1 -> 2 -> 3 -> 4 -> NULL
After Inserting Node with Value ‘5’, the linked list become:
1 -> 2 -> 3 -> 4 -> 5 -> NULL

The steps required to insert the new node at the beginning of the linked list is as follows:

  1. Create a new node with the desired value in data part and NULL value in pointer part.
  2. Check whether linked list is empty or not.
  3. If linked list is empty, then the new node will become the head of the linked list.
  4. Else if linked is not empty, then traverse the whole linked list and reach to the last node of the linked list, then point the pointer part of last node of the linked list to the new node.

If the current linked list is {1->2->3}, then after inserting node with value 4, the newly linked list will become {1->2->3->4}. 

C++ Program insert new node at the end of the linked list is as follows:

/* C++ Program to Insert New Node at the End of the Linked List */
#include<bits/stdc++.h>
using namespace std;
/* Structrue of the Node */
struct node
{
    int data;
    struct node *next;
};
/* Function to create New Node of the Linked List */
struct node *create(struct node **new_node, int ele)
{
    (*new_node) = (struct node*)malloc(sizeof(struct node));
    (*new_node)->data = ele;
    (*new_node)->next = NULL;
    return (*new_node);
}
/* Function to Insert New Node at the End of the Linked List */
void insert_end(struct node **head, int ele)
{
    /*Creating a new node.*/
    struct node *new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = ele;
    new_node->next = NULL;
    
    /* check whether the linked list is empty or not*/
    /* If empty, new node will become head of the linked list */
    if(*head == NULL)     
    {
        *head = new_node;
        return;
    }
    /*If not empty, we traverse the complete linked list 
    and points the last node of the linked list to the new node */
    struct node *temp = NULL;
    temp = (*head);
    while(temp->next != NULL)
    temp=temp->next;
    temp->next = new_node;
}
/* Function to Print the Linked List */
void print(struct node *head)
{
    struct node *p = head;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
int main()
{
    struct node *head;
    head = NULL;
    cout<<"Inserting 3 Nodes to the End of the Linked List!!";
    insert_end(&head,1);
    insert_end(&head,2);
    insert_end(&head,3);
    cout<<"\nThe Nodes of the Linked List are: \n";
    print(head);
}
OUTPUT:
Inserting 3 Nodes to the End of the Linked List!!
The Nodes of the Linked List are: 
1 2 3

Related Posts:

  1. Insert a New Node at the beginning of the Linked List.
  2. Insert a New Node at the Middle of the Linked List.
  3. Insert a New Node at the Sorted Linked List.
  4. Reverse a Linked List.
  5. Reverse a Linked List Using Stack.
  6. Printing Linked List in Reverse Order without actually reversing the Linked List.
  7. Swap Adjacent Elements of the Linked List.
  8. Count All Occurrences of a Particular Node in a Linked List.
  9. Bubble Sort on Linked List.
  10. Detect a Loop in a Linked List.
  11. Find the Length of the Loop present in the Linked List.
  12. Detect and Remove Loop from a Linked List.
  13. Segregate Even and Odd Nodes of the Linked List.
  14. Delete Complete Linked List.
  15. Delete Nth Node of the Linked List.
  16. Delete without head pointer of the Linked List.
  17. Delete All Occurrences of particular node of the Linked List.
  18. Delete Alternate Nodes of the Linked List.
  19. Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
  20. Remove Duplicate Nodes from an Unsorted Linked List

You may also like...