链表的创建,添加!
运行一下这个程序,我不知道那里错了,一直是这样的结果。
#include "stdio.h"
#include "malloc.h"
#define NULL 0
#define KAICH sizeof(struct student)
int n;
struct student
{int num;
float score;
struct student *next;
};
struct student *creat(void)
{int f1;
float f2;
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(KAICH);
scanf("%d,%3.1f",&p1->num,&p2->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(KAICH);
scanf("%d,%3.1f",&p1->num,&p2->score);
}
p2->next=NULL;
return(head);
}
struct student *dele(struct student *head,int number)
{struct student *p1,*p2;
if(head==NULL)
printf("the lianbiao is null!\n");
else
p2=head;
while(p2->num!=number&&p2->next!=NULL)
{p1=p2;p2=p2->next;}
if(p2->num==number)
{if(p2==head)head=p2->next;
else p1->next=p2->next;
printf("the node is delete %d!\n",number);
n=n-1;
}
else
free(p2);
printf("the number is not find!\n");
return(head);
}
struct student *insert(struct student *head,struct student *inp)
{struct student *p1,*p2,*p3;
p1=head;
p3=inp;
if(head==NULL)
{head=p3;
p3->next=NULL;}
else
{while((p1->num<p3->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(p1->num>p3->num)
{if(head==p1)head=p3;
else p2->next=p3;
p3->next=p1;
}
else
p1->next=p3;
p3->next=NULL;
}
n=n+1;
return(head);
}
void print(struct student *head)
{struct student *p;
p=head;
printf("\nyour lianbiao are %d\n",n);
while(head!=NULL&&p->num!=NULL)
{printf("%d\t%3.1f\n",p->num,p->score);p=p->next;}
}
main()
{struct student *head,*pum;
int delnum;
printf("input what you want and over when num is NULL!\n");
head=creat();
print(head);
printf("\ninput you delete number\n");
scanf("%d",&delnum);
while(delnum!=0)
{
head=dele(head,delnum);
print(head);
printf("you delete number\n");
scanf("%d",&delnum);
}
printf("input you want insert number\n");
pum=(struct student*)malloc(KAICH);
scanf("%d,%3.1f",&pum->num,&pum->score);
while(pum->num!=0)
{head=insert(head,pum);
print(head);
printf("go on insert\n");
pum=(struct student*)malloc(KAICH);
scanf("%d,%3.1f",&pum->num,&pum->score);
}
}