|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
建了一个链表插入函数,基本上按谭浩强书上写的,已假设已有链表中各结点的成员项num(学号)是按学号由小到大顺序排列,但是发现一个问题,当输入的学号小于已有链表的第一个学号时,插不进去,程序执行后不输出该项,看程序也看不出问题,请大家帮忙看看是什么原因
#include<stdlib.h>
#include<stdio.h>
#define Null 0
#define Len sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
struct student *insert(head)
struct student *head;
{
struct student *p1,*p2,*stu;
stu=(struct student *)malloc(Len);
scanf("%ld,%f",&stu->num,&stu->score);
p1=head;
if (head==Null) {head=stu;stu->next=Null;}
else {
while((stu->num>p1->num)&&(p1->next!=Null))
{p2=p1; p1=p1->next;}
if (stu->num<p1->num)
{
if (head==p1) {head=stu;stu->next=p1;}
else {p2->next=stu; stu->next=p1;}
}
else{p1->next=stu; stu->next=Null;}
}
return head;
} |
|