为了账号安全,请及时绑定邮箱和手机立即绑定

动态链表怎么只能输入一次,错哪了?要实现多次输入输出,怎么改?

动态链表怎么只能输入一次,错哪了?要实现多次输入输出,怎么改?

C
溯源1 2017-01-15 11:49:24
#include "stdio.h"#include<malloc.h>#include<string.h>#define LEN sizeof(struct student)struct student{ int num; int score; struct student*next;};struct student*create(){ char ch;    struct student *head;    struct student*p1,*p2;    int a=0;    printf("是否输入新数据:\n");    while(toupper(ch=getche())=='Y');    {    p1=(struct student*)malloc(LEN);    printf("\n请输入:");    scanf("%d%d",&p1->num,&p1->score);    printf("是否输入新数据:\n");    a++;    if(a==1) head=p1;    else p2->next=p1;    p2=p1;    p1->next=NULL;    }    return(head);}void print(struct student *head){ struct student *p; p=head; while(p!=NULL) { printf("\n学号:%d,成绩:%d",p->num,p->score); p=p->next; }}void main( ){ struct student *stu; stu=create(); print(stu);}
查看完整描述

2 回答

已采纳
?
zy112

TA贡献8条经验 获得超10个赞

  if(a==1) head=p1;
  else p2->next=p1;
    p2=p1;
    p1->next=NULL;

你的代码这里有问题,到底想干啥呢??我建议你写if else 的时候最好加上括号,这样不会混,而且你的返回值是返回 head, 但是else里面根本没有 head的事,同时,main里面,你要循环输出的时候,只输出一个吗?如果有多个的话应该怎么办呢?是不是应该判断一下。这是主要的问题,你可以自己先改改看 ,实在改不了的话,就回复我一下,不会的我在和你说说,或者改了给你看看也行。。

查看完整回答
1 反对 回复 2017-01-15
  • 溯源1
    溯源1
    改成这个,前辈看下,要实现多次输入输出,怎么改? #include "stdio.h" #include<malloc.h> #include<string.h> #define LEN sizeof(struct student) struct student{ int num; int score; struct student*next; }; struct student*create() { char ch; struct student *head; struct student*p1,*p2; int a=0; printf("是否输入新数据:\n"); while(toupper(ch=getche())=='Y'); { p1=(struct student*)malloc(LEN); printf("\n请输入:"); scanf("%d%d",&p1->num,&p1->score); printf("是否输入新数据:\n"); a++; if(a==1) head=p1; else p2->next=p1; p2=p1; p1->next=NULL; } return(head); } void print(struct student *head) { struct student *p; p=head; while(p!=NULL) { printf("\n学号:%d,成绩:%d",p->num,p->score); p=p->next; } } void main( ){ struct student *stu; stu=create(); print(stu); }
  • zy112
    zy112
    你这个写的没大问题,就是while 输入 Y 的时候,后面应该没有分号,while(toupper(ch=getche())=='Y'); 然后我试了一下,可以多次输入的。
?
lay3

TA贡献2条经验 获得超0个赞

你这样写代码是会下岗的
查看完整回答
反对 回复 2017-01-16
  • 2 回答
  • 0 关注
  • 1603 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信