Insert node at Last Position : Singly Linked List

Insert node at Last / End Position in Singly Linked List

Inserting node at start in the SLL (Steps):

1. Create New Node
2. Fill Data into “Data Field
3. Make it’s “Pointer” or “Next Field” as NULL
4. Node is to be inserted at Last Position so we need to traverse SLL upto Last Node.
5. Make link between last node and newnode
```void insert_at_end()
{
struct node *new_node,*current;

new_node=(struct node *)malloc(sizeof(struct node));

if(new_node == NULL)
printf("nFailed to Allocate Memory");

printf("nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
temp = start;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = new_node;
}
}
```

Diagram :

Attention :

1. If starting node is not available then “Start = NULL” then following part is executed
```if(start==NULL)
{
start=new_node;
current=new_node;
}```
1. If we have previously created First or starting node then “else part” will be executed to insert node at start
2. Traverse Upto Last Node., So that temp can keep track of Last node
```else
{
temp = start;
while(temp->next!=NULL)
{
temp = temp->next;
}```
1. Make Link between Newly Created node and Last node ( temp )
`temp->next = new_node;`

To pass Node Variable to Function Write it as –

`void insert_at_end(struct node *temp)`