Example-
Linked list is:3->4->1->9
Insert: 8 at 3rd position
Linked list become: 3->4->8->1->9
program-
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *first;
void create()
{
struct node *ptr,*cpt;
char ch;
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter Number\n");
scanf("%d",&cpt->data);
first=cpt;
do
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter Number\n");
scanf("%d",&ptr->data);
cpt->next=ptr;
cpt=ptr;
printf("Continue(y/n)?n");
ch=getch();
}while(ch=='y');
cpt->next=first;
}
void traverse()
{
struct node *ptr=first;
printf("Linklist is :\n");
while(ptr->next!=first)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
void insertion()
{
int pos,i=0;
struct node *ptr,*cpt=first;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter Number to insert and positionn");
scanf("%d%d",&ptr->data,&pos);
for(i=0;i< pos-2;i++)
{
cpt=cpt->next;
}
ptr->next=cpt->next;
cpt->next=ptr;
}
void main()
{
clrscr();
create();
traverse();
insertion();
traverse();
getch();
}
Enter Number
2
Enter Number
4
Continue(y/n)?
Enter Number
1
Continue(y/n) ?
Enter Number
9
Continue(y/n) ?
Enter Number
0
Continue(y/n) ?
Linklist is :
2 4 1 9 0
Enter Number to insert and position
10 3
Linklist is :
2 4 10 1 9 0