|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个程序是创建一个保存学生编号和成绩的链表,然后实现在链表里插入一个学生的信息和成绩。单次插入学生信息的函数我写完了,就是那个add()函数,现在我想用这个函数来实现多次插入信息。我截的这一段程序是输入要插入的学生信息然后调用add函数,完整程序在后面。我的思路是,当addstu这个节点完成插入后,将它释放然后重新赋值,再次插入,但是写的这段代码有问题,问题在哪呢,又该怎么实现多次插入节点?求大佬解答。
- while(1)
- {
- addstu=(struct student *)malloc(sizeof(struct student));
- addstu->num=0;
- addstu->score=0;
- addstu->next=NULL;
- printf("请输入你想加入的学生的编号和成绩(输入0结束添加):");
- scanf("%d,%f",&addstu->num,&addstu->score);
- printf("\n");
- add(stu,addstu);
- free(addstu);
- if(0==addstu->num)
- {
- break;
- }
- }
复制代码
完整程序:
- #include <stdio.h>
- #include <malloc.h>
- struct student
- {
- int num;
- float score;
- struct student *next;
- };
- int n;
- int main(void)
- {
- struct student *creat();
- void print(struct student *r);
- void del(struct student *q,int num1);
- void add(struct student *a,struct student *a0);
- int m;
- struct student *stu;
- struct student *addstu;
- stu=creat();
- printf("\nThere are %d record!\n\n",n);
- print(stu);
- printf("请输入你想删除的学生的编号:");
- scanf("%d",&m);
- while(m==0)
- {
- printf("不能为0请重新输入:");
- scanf("%d",&m);
- }
- printf("\n");
- del(stu,m);
- while(1)
- {
- addstu=(struct student *)malloc(sizeof(struct student));
- addstu->num=0;
- addstu->score=0;
- addstu->next=NULL;
- printf("请输入你想加入的学生的编号和成绩(输入0结束添加):");
- scanf("%d,%f",&addstu->num,&addstu->score);
- printf("\n");
- add(stu,addstu);
- free(addstu);
- if(0==addstu->num)
- {
- break;
- }
- }
- printf("\n\n");
- }
- struct student *creat()
- {
- struct student *p1,*p2,*head;
- n=0;
- head=NULL;
- p2=(struct student *)malloc(sizeof(struct student));
- while(1)
- {
- p1=(struct student *)malloc(sizeof(struct student));
- printf("Please enter this student number:");
- scanf("%d",&p1->num);
- printf("Please enter this student score:");
- scanf("%f",&p1->score);
- if(0==p1->num)
- {
- break;
- }
- else
- {
- n++;
- if(1==n)
- {
- head=p1;
- p2=p1;
- }
- else
- {
- p2->next=p1;
- p2=p1;
- }
- }
- }
- p2->next=NULL;
- return head;
- }
- void print(struct student *r)
- {
-
- while(r)
- {
- printf("第 %d 位学生的成绩是 %.2f\n",r->num,r->score);
- r=r->next;
- }
- printf("\n");
- }
- void del(struct student *q,int num1)
- {
- void print(struct student *r);
- struct student *q1,*q2,*q3;
- q1=q3=q;
- if(q==NULL)
- {
- printf("空表不能操作!");
- }
- else
- {
- while(1)
- {
- int i=0;
- while(1)
- {
- if(num1==q3->num)
- {
- i=1;
- break;
- }
- if(q3->next==NULL)
- {
- break;
- }
- q3=q3->next;
- }
- if(0==i)
- {
- printf("没有该学生信息!\n请重新输入:");
- scanf("%d",&num1);
- q3=q;
- }
- if(1==i)
- break;
- }
- printf("\n");
- if(num1==q1->num)
- {
- q=q->next;
- }
- else
- {
- while(1)
- {
- if(num1!=q1->num)
- {
- q2=q1;
- q1=q1->next;
- }
- else
- {
- q2->next=q1->next;
- free(q1);
- break;
- }
- }
- }
- printf("\nThere are %d record!\n\n",n-1);
- print(q);
- printf("%d号学生成绩已删除\n\n",num1);
- }
- }
- void add(struct student *a,struct student *a0)
- {
- struct student *a1,*a2,*a3;
- a1=a;
- if(a0->num<a->num)//第一种情况,插入节点编号在首节点前
- {
- a0->next=a;
- a=a0;
- }
- else
- {
- while(1)
- {
- if(a1->next==NULL)
- {
- a3=a1;
- break;
- }
- a1=a1->next;
- }
- a1=a;
- if(a0->num>a3->num)
- {
- a3->next=a0;
- a0->next=NULL;
- }
- else
- {
- while(1)
- {
- if(a0->num<a1->num)
- {
- a2->next=a0;
- a0->next=a1;
- break;
- }
- a2=a1;
- a1=a1->next;
- }
- }
- }
- printf("\nThere are %d record!\n\n",n);
- print(a);//函数嵌套使用
- printf("%d号学生成绩添加成功\n\n",a0->num);
- }
复制代码
代码太长,没细看。至少有两个主要问题。
1、被链接到链表的节点为什么要 free ? 内存分配后,只要还要使用,就不能 free,free后就不能再去访问。
2、add()这个函数里没有判断表头是否为空,都表头为空时,访问出错。
还有就是 malloc 分配的内存在程序结束前没有 free。
create 函数里,输入学号0 的时候,不生成表头,但是分配的内存又没有释放。
|
评分
-
查看全部评分
|