/* Inserting a node into a singly Linked List*/
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *link;
};
typedef struct list node;
// create the list
void createlist(node *n)
{
char ch;
printf("\n\nEnter the item (number only) to be inserted\n");
scanf("%d",&n->info);
printf("\nDo you want to continue(Y/N)\n");
ch= getche();
getch();
if(ch=='y'|| ch=='Y')
{
n->link=(node *)malloc(sizeof(node));
createlist(n->link);
}
else
n->link=NULL;
}
void displaylist(node *n)
{
node *n1;
n1=n;
if(n1!=NULL)
{
printf("%d\n",n->info);
displaylist(n1->link);
}
}
void insnode(node *n,int desti)
{
int cnt=1;
node *n1, *temp;
n1=n;
while(n1!=NULL)
{
cnt++;
if(desti==1)
{
temp->link=(node *)malloc(sizeof(node));
printf("\nEnter the item to be inserted\n");
scanf("%d",&temp->info);
temp->link=n1;
n=temp;
[B]n1->link;[/B]
printf("\nNode Inserted Successfully\n");
printf("\nThe new Linked List elements are\n");
break;
}
else if(desti==cnt)
{
temp->link=(node *)malloc(sizeof(node));
printf("\nEnter the item to be inserted\n");
scanf("%d",&temp->info);
temp->link=n1->link;
n1->link=temp;
printf("\nNode Inserted Successfully\n");
printf("\nThe new Linked List elements are\n");
break;
}
n1=n1->link;
}
displaylist(n);
}
void main()
{
node *start;
int loc;
clrscr();
start->link=(node *)malloc(sizeof(node));
printf("\nCreate a linked list :");
createlist(start);
printf("\n\nCreated linked list elements are:\n");
displaylist(start);
printf("\nEnter the position at which you want to add new node\n");
scanf("%d",&loc);
insnode(start,loc);
getch();
}