Example-
Linked list : 2 3 4 5
After Insertion of 1 : 2 3 4 5 1
Insertion Function:
void insertion(int n)
{
struct node *cpt,*ptr;
cpt=(struct node*)malloc(sizeof(struct node));
cpt->data=n;
ptr=first;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=cpt;
cpt->link=NULL;
}
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node{
struct node *link;
int data;
};
struct node *first=NULL;
void traverse()
{
struct node *ptr=first;
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->link;
}
}
void insertion(int n)
{
struct node *cpt,*ptr;
cpt=(struct node*)malloc(sizeof(struct node));
cpt->data=n;
ptr=first;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=cpt;
cpt->link=NULL;
}
void main()
{
struct node *cpt,*ptr;
int n;
char ch;
clrscr();
first=(struct node*)malloc(sizeof(struct node));
printf("Enter first element in Linklist\n");
scanf("%d",&first->data);
ptr=first;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter another datan");
scanf("%d",&cpt->data);
ptr->link=cpt;
ptr=ptr->link;
printf("Continue(y/n) ?");
ch=getch();
}while(ch=='y');
ptr->link=NULL;
printf("Linklist isn");
traverse();
printf("Enter number to add in linklist at End\nn");
scanf("%d",&n);
insertion(n);
printf("Linklist After Insertion\n");
traverse();
getch();
}
Enter first element in Linked list
2
Enter another data
4
Continue(y/n)?
Enter another data
1
Continue(y/n) ?
Enter another data
9
Continue(y/n) ?
Enter another data
0
Continue(y/n) ?
Linklist is :
2 4 1 9 0
Enter number to add in linklist at End
5
Linklist After Insertion
2 4 1 9 0 5