单链表插入修改
int data;struct LNode *next;
}Linklist;
Linklist *creatlist()
{
Linklist *s,*L;
int i,n,a[21];
L=(Linklist *)malloc(sizeof(Linklist));
printf("Len:\n");
scanf("%d",&n);
L->next=NULL;
for(i=0;i<=n-1;i++)
{printf("Number:\n");
scanf("%d",&a[i]);
s=(Linklist *)malloc(sizeof(Linklist));
s->data=a[i];
s->next=L->next;
L->next=s;
}
return(L);
}
int Listinsert(Linklist *L,int i,int e)
{
int j=0;
Linklist *p=L,*s;
while(j<i-1&&p!=NULL)
{j++;
p=p->next;
}
if(p==NULL) return(0);
s=(Linklist *)malloc(sizeof(Linklist));
s->data=e;
s->next=p->next;
p->next=s;
return(1);
}
Listdelete(Linklist *L,int i)
{
int j=0;
Linklist *p=L,*q;
while(j<i-1&&p!=NULL)
{ j++;
p=p->next;
}
if(p==NULL)
return 0;
else
{ q=p->next;
if(q==NULL) return 0;
p->next=q->next;
free(q);
return 1;
}
}
void displist(Linklist *L)
{
Linklist *p=L->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
main()
{ Linklist *M;
int a,b;
M=creatlist();
displist(M);
printf("Please insert to insert position and data:");
scanf("%d %d",&a,&b);
Listinsert(M,a,b);
displist(M);
Listdelete(M,a);
displist(M);
printf("%d ",a[21]);
}