求教一个链表问题!
我写了一个学生管理系统的链表程序函数几乎是按照书上写的
但是就是有问题
貌似每次函数调用时 head都等于NULL
我百思不得其解
我已经改了三天了,始终没有找出问题,特来此寻求大神帮助!
希望各位能帮我指出错误,并且帮我改正一下!
感激不尽!
下面是我的代码:
#include<stdio.h>
#include<stdlib.h>
void menu();
struct student
{
long num;
char name[20];
float score;
};
struct intnode
{
struct student stu;
struct intnode *next;
};
void creat(struct intnode **head)
{
int n;
struct intnode *p;
printf("输入学生学号\n");
scanf("%d",&n);
while(n!=0)
{
p=(struct intnode *)malloc(sizeof(struct intnode));
p->stu.num=n;
printf("输入学生姓名\n");
scanf("%s",p->stu.name);
printf("输入学生成绩\n");
scanf("%f",&p->stu.score);
p->next=*head;
*head=p;
printf("输入学生学号 以0结束\n");
scanf("%d",&n);
}
p=*head;
while(p!=NULL)
{
n++;
p=p->next;
}
printf("n=%d",n);
printf("\n按回车键返回主菜单……\n");
getchar();
getchar();
menu();
}
void sort(struct intnode *head,int n)
{
int i,j;
struct student temp;
struct intnode *p1,*p2,*p;
for(p1=head,i=0;i<n-1;i++,p1=p1->next)
for(p2=p1->next,j=i+1;j<n;p2=p2->next,j++)
if(p1->stu.score<p2->stu.score)
{
temp=p1->stu;
p1->stu=p2->stu;
p2->stu=temp;
}
p=head;
while(p!=NULL)
{
printf("%l\t%s\t%f\n",p->stu.num,p->stu.name,p->stu.score);
p=p->next;
}
}
struct intnode * insert(intnode **head,float x)
{
struct intnode *other,*last,*current;
other=(struct intnode *)malloc(sizeof(struct intnode));
other->stu.score=x;
current=*head;
while(x<other->stu.score&¤t->next!=NULL)
{
last=current;
current=current->next;
}
if(x>=current->stu.score)
if(current==*head)
{
other->next=*head;
*head=other;
}
else
{
other->next=current;
last->next=other;
}
else
{
other->next=NULL;
current->next=other;
}
return(other);
}
int delet(struct intnode **head,int x)
{
struct intnode *p,*last;
p=*head;
while(x<p->stu.num&&p->next!=NULL)
{
last=p;
p=p->next;
}
if(p->stu.num==x)
{
if(p==*head)
*head=p->next;
else
last->next=p->next;
free(p);
return 1;
}
else return 0;
}
void main()
{
menu();
}
void menu()
{
system("cls");
int k,n,t;
float x;
struct intnode *head;
head=NULL;
printf("**************************************\n");
printf(" 学生成绩系统 \n");
printf("**************************************\n\n");
printf("1、建立学生记录链表 并遍历学生链表\n");
printf("2、将学生链表从高到低排序\n");
printf("3、向链表中插入结点\n");
printf("4、按指定学号删除结点\n\n");
printf("请输入数字 选择功能\n");
scanf("%d",&k);
switch(k){
case 1: printf("建立学生记录链表\n");
creat(&head);break;
case 2: printf("将学生链表从高到低排序\n");
sort(head,n);break;
case 3: printf("向链表中插入结点\n");
printf("输入学生成绩\n");
scanf("%f",&x);
insert(&head,x);break;
case 4: printf("按指定学号删除结点\n");
printf("输入删除学生学号\n");
scanf("%d",&t);
delet(&head,t);break;
case 5: printf("系统正在关闭\n");
system("pause");
default: printf("输入错误,请重新输入1-7的数字");
printf("\n按回车键返回主菜单……\n");
getchar();
getchar();
menu();
}
printf("\n按回车键返回主菜单……\n");
getchar();
getchar();
menu();
}