Linked List Program to insert a node at the end

Linked List Program to insert  a node at the end:-

Four Step to insert node at the end:-

1. allocate node

2. put in the data

3. make new node next=NULL

4. if linked list is empty make new node as head otherwise traverse list till the last node

4. last node next to new node

#include<stdio.h>
#include<stdlib.h>

struct node{

int data;
struct node *next;  //structure for a single node...A node have two parts 1->data(int,float,double..etc), 2->pointer to next node
};


int InsertAtEnd(struct node *head,int new_data)
{
struct node *new_node;

//Memory allocation for new node
new_node=(struct node *)malloc(sizeof(struct node));

new_node->data=new_data;
new_node->next=NULL;

//if Linked List Empty..Make new_node head
if(head==NULL)
{
head=new_node;
}

while(head->next!=NULL)
{
head=head->next;
}
head->next=new_node;

}

void LinkListTraversal(struct node *n)
{
while(n!=0)
{
printf("%d\t",n->data);
n=n->next;
}
printf("\n");
}

main()

{

//initially assign 'NULL' to all node..otherwise garbage value will be in node

struct node *head=0;
struct node *first=0;
struct node *second=0;
struct node *third=0;


//Create space for node->Allocate Memory to All node using malloc or calloc.
// malloc return a void pointer which casting in structure data type.

head=(struct node*)malloc(sizeof(struct node));
first=(struct node*)malloc(sizeof(struct node));
second=(struct node*)malloc(sizeof(struct node));
third=(struct node*)malloc(sizeof(struct node));


//assign value to all node & point to next node

//head node

head->data=2;        //head`s data part store value 2                 ----------
                                               //                                                |  2   |    |-->
head->next=first;    //'next of head' point to node first             ----------
                                             

//first node

first->data=3;        //first`s data part store value 2                        ----------       ----------
                                               //                                                     |  2   |     |-->|  3   |     |-->
first->next=second;    //'next of first' point to node second          ----------       ----------



//second


second->data=4;        //second`s data part store value 2                 ----------      ----------     ----------
                                               //                                                       |  2   |     |-->|  3  |     |-->|  4 |   |
second->next=third;    //'next of second' point to node second       ----------      ----------     ----------


//third node

third->data=5;        //second`s data part store value 2    ----------      ----------   ----------     --------
                                               //                                       |  2   |    |-->|  3   |     |-->|  4   |    |-->|  5 |    |-->0
third->next=0;    //'next of third' point to NULL            ----------      ----------    ----------     ---------

printf("Link List Before insertion:\n");

LinkListTraversal(head);

InsertAtEnd(first,9);

printf("Link List After insertion:\n");

LinkListTraversal(head);

}

Related Posts