i've written a code for insertion in a linked list for all the 3 cases that is ...at the end,in the beginning & in between.....when i run this code then it runs successfully for insertion at end and in the beginning ,it is also showing no errors while compiling the program but when i run this code for insertion in between then it prints like this:
enter the size of list:5
enter list:
10
20
30
40
50
Enter 1.to insert at end
Enter 2.to insert in the beginning
Enter 3.to insert in the between
//so, when i enter here choice number 3.to insert in between
3.
enter the location after which the number has to be inserted:3
enter number to be inserted:100
then it doesn't print anything and comes back to the program...that is the screen where the source program was written.why?it's not printing anything.plz make necesaary changes in my code to make it properly run....plz help me out with my code...
#include<stdio.h>
#include<conio.h>
struct link
{
int data;
struct link*ptr;
};
typedef struct link node;
void main()
{
int size,i,choice;
char ch;
node*start,*temp,*p;
clrscr();
do
{
printf("enter the size of list:");
scanf("%d",&size);
printf("enter list\n");
start=NULL;
for(i=0;i<size;i++)
{
if(start==NULL)
{
start=(node*)malloc(sizeof(node));
scanf("%d",&start->data);
start->ptr=NULL;
}
else
{
for(p=start;p->ptr!=NULL;p=p->ptr);
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->ptr=NULL;
p->ptr=temp;
}
}
printf("Enter 1.To insert at the end\n");
printf("Enter 2.To insert at the beginning\n");
printf("Enter 3.To insert in between\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
end(start,p);
break;
case 2:
beg(start,p);
break;
case 3:
between(start,p,size);
break;
default:
printf("wrong number entered\n");
}
printf("Enter y to continue\n");
scanf("%c",&ch);
}
while(ch=='Y'||ch=='y');
getch();
}
end(node*start,node*p)
{
node*temp;
for(p=start;p->ptr!=NULL;p=p->ptr)
temp=(node*)malloc(sizeof(node));
printf("enter the number to be inserted\n");
scanf("%d",&temp->data);
temp->ptr=NULL;
p->ptr=temp;
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
return(0);
}
beg(node*start,node*p)
{
node*temp;
temp=(node*)malloc(sizeof(node));
printf("enter the number to be inserted\n");
scanf("%d",&temp->data);
temp->ptr=start;
start=temp;
printf("new linked list is:\n");
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
return(0);
}
between(node*start,node*p,int size)
{
node*temp;
int loc,count=0;
printf("enter the location after which the number has to be inserted\n");
scanf("%d",&loc);
if(loc<=size)
{
temp=(node*)malloc(sizeof(node));
printf("enter the number to be inserted\n");
scanf("%d",&temp->data);
for(p=start;p!=NULL;p=p->ptr)
{
count++;
if(count==loc)
{
temp->ptr=p->ptr;
p->ptr=temp;
printf("new linked list is:");
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
return(0);
}
else
{
printf("location is not found\n");
return(0);
}
}
}
return(0);
}