单链表插入不到最后(插在最前或者中间都可以)
#include <stdio.h>#define N 5
typedef struct node
{
int number;
float wage;
struct node *link;
}stu;
stu *creat(int n)
{
stu *h,*p,*s;
int i;
if((h=(stu*)malloc(sizeof(stu)))==NULL)
{
printf("error:\n");
return 0;
}
h->number='\0';
h->wage='\0';
h->link=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s=(stu*)malloc(sizeof(stu)))==NULL)
{
printf("error:\n");
return 0;
}
p->link=s;
printf("input number,wage:\n");
scanf("%d%f",&s->number,&s->wage);
s->link=NULL;
p=s;
}
return (h);
}
list(stu *h)
{
stu *q;
for(q=h->link;q!=NULL;q=q->link)
{
printf("%d\t%f\n",q->number,q->wage);
}
}
stu *insert(stu *h)
{
stu *p,*q,*s;
int number;
float wage;
p=h->link;;
printf("input insert number:\n");
scanf("%d%f",&number,&wage);
if((s=(stu*)malloc(sizeof(stu)))==NULL)
{
printf("error:\n");
return 0;
}
if(p->number>number)
{
h->link=s;
s->link=p;
s->number=number;
s->wage=wage;
}
else
{
while((p->number<number)&&(p!=NULL))
{
q=p;
p=p->link;
}
if(p!=NULL)
{
q->link=s;
s->link=p;
}
else
{
q->link=s;
s->link=NULL;
}
s->number=number;
s->wage=wage;
}
return h;
}
main()
{
stu *h;
h=creat(N);
list(h);
insert(h);
list(h);
}