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


#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);

}


溯源1
浏览 1606回答 2
2回答

zy112

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

lay3

你这样写代码是会下岗的
打开App,查看更多内容
随时随地看视频慕课网APP