这个链表是什么问题?

Jophoenix 2010-05-27 07:56:10
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct STUDENT)
//定义结构体
struct STUDENT
{
char name[20];
char gender;
int number;
int age,math,english,clanguage,total;
struct STUDENT *next;
};
typedef struct STUDENT STU;
//各种函数
void display_menu()
{
printf("**********************************************\n");
printf("* 请选择功能 *\n");
printf("* *\n");
printf("* 1.输入学生信息 *\n");
printf("* 2.输出学生信息 *\n");
printf("* *\n");
printf("**********************************************\n");
}
//创建链表函数
STU *creatlist(void)
{
STU *head=NULL;
STU *p1,*p2;
int n=0;
while (1)
{
printf("输入0退出\n");
n++;
p1=p2=(STU*)malloc(LEN);
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
printf("\输入第%d学生信息:\n",n);
printf("请输入学号\n");
scanf("%d",&p1->number);
if(p1->number==0)break;
fflush(stdin);
printf("请输入姓名\n");
gets(p1->name);
fflush(stdin);
printf("请输出年龄\n");
scanf("%d",&p1->age);
fflush(stdin);
printf("请选择性别 F/M\n");
do
{
p1->gender=getchar();
while(getchar()!='\n');
if(p1->gender!='M'&& p1->gender!='F')
printf("输入错误,请重新输入\n");
}while(p1->gender!='M'&& p1->gender!='F');
printf("请输入高数成绩\n");
scanf("%d",&p1->math);
fflush(stdin);
printf("请输入英语成绩\n");
scanf("%d",&p1->english);
fflush(stdin);
printf("请输入C语言成绩\n");
scanf("%d",&p1->clanguage);
fflush(stdin);
}
p2->next=NULL;
free(p1);
return head;
}
//输出链表函数
void outputlist(STU *head)
{
STU *p;
p=head;
if(p==NULL)printf("无数据\n");
while(p->next!=NULL)
{
p->total=p->math+p->english+p->clanguage;
printf("------------------------------------------------------------\n");
printf("| 学号 | 姓名 | 性别 | 年龄 | C语言 | 英语 | 高数 | 总分 |\n");
printf("------------------------------------------------------------\n");
printf("|%d | %s | %c |%d | %d | %d | %d | %d |\n",p->number,p->name,p->gender,p->age,p->math,p->english,p->clanguage,p->total);
p=p->next;
}
}
//函数声明
void display_menu();
STU *creatlist(void);
void outputlist(STU *head);
//主函数
int main()
{
int choose,number;
STU *head=NULL,*p0; //让head初始值为0

do
{
display_menu();
scanf("%d",&choose);
switch(choose)
{
case 1:
{
head=creatlist();
system("cls");
system("pause");
}break;
case 2:
{
p0=head;
outputlist(p0);
system("pause");
}break;
}
}while(choose!=0);
}

不知道错哪了。。。
...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
周靖峰 2010-05-28
  • 打赏
  • 举报
回复
你的程序中STU *creatlist(void);和void outputlist(STU *head);这两个函数都有问题,我已经帮你改好了,错误用注释标出了,你自己再看一下吧。

#include<stdio.h>
#include<malloc.h>
#include<windows.h>

#define LEN sizeof(struct STUDENT)
//定义结构体
struct STUDENT
{
char name[20];
char gender;
int number;
int age,math,english,clanguage,total;
struct STUDENT *next;
};

typedef struct STUDENT STU;
//各种函数
void display_menu()
{
printf("**********************************************\n");
printf("* 请选择功能 *\n");
printf("* *\n");
printf("* 1.输入学生信息 *\n");
printf("* 2.输出学生信息 *\n");
printf("* *\n");
printf("**********************************************\n");
}

//创建链表函数
STU *creatlist(void)
{
STU *head=NULL;
STU *p1,*p2;
int n=0;
while (1)
{
printf("输入0退出\n");
n++;
p1=(STU*)malloc(LEN); //p2不用分配
if(n==1) head=p1;
else p2->next=p1;
printf("\n输入第%d学生信息:\n",n);
printf("请输入学号\n");
scanf("%d",&p1->number);
if(p1->number==0)break;
fflush(stdin);
printf("请输入姓名\n");
gets(p1->name);
fflush(stdin);
printf("请输出年龄\n");
scanf("%d",&p1->age);
fflush(stdin);
printf("请选择性别 F/M\n");
do
{
p1->gender=getchar();
while(getchar()!='\n');
if(p1->gender!='M'&& p1->gender!='F')
printf("输入错误,请重新输入\n");
}while(p1->gender!='M'&& p1->gender!='F');
printf("请输入高数成绩\n");
scanf("%d",&p1->math);
fflush(stdin);
printf("请输入英语成绩\n");
scanf("%d",&p1->english);
fflush(stdin);
printf("请输入C语言成绩\n");
scanf("%d",&p1->clanguage);
fflush(stdin);
p2=p1; //这句放最后
}
p2->next=NULL;
free(p1);
return head;
}

//输出链表函数
void outputlist(STU *head)
{
STU *p;
p=head;
if(p==NULL)printf("无数据\n");
while(p!=NULL) //这里原来为p->next!=NULL
{
p->total=p->math+p->english+p->clanguage;
printf("------------------------------------------------------------\n");
printf("| 学号 | 姓名 | 性别 | 年龄 | C语言 | 英语 | 高数 | 总分 |\n");
printf("------------------------------------------------------------\n");
printf("|%d | %s | %c |%d | %d | %d | %d | %d |\n",p->number,p->name,p->gender,p->age,p->math,p->english,p->clanguage,p->total);
p=p->next;
}
}

//函数声明
void display_menu();
STU *creatlist(void);
void outputlist(STU *head);

//主函数
int main()
{
int choose,number;
STU *head=NULL,*p0; //让head初始值为0

do
{
display_menu();
scanf("%d",&choose);
switch(choose)
{
case 1:
{
head=creatlist();
system("cls");
system("pause");
}break;
case 2:
{
p0=head;
outputlist(p0);
system("pause");
}break;
}
}while(choose!=0);
}
周靖峰 2010-05-28
  • 打赏
  • 举报
回复

p1=p2=(STU*)malloc(LEN);
if(n==1) head=p1;
else p2->next=p1;
p2=p1;

楼主,我有一个疑问,当n!=1时,p1=p2,p2->next=p1,岂不是p1->next=p1?
楼主你想要干什么呢?
joyer688 2010-05-28
  • 打赏
  • 举报
回复
顶---
sonicrang 2010-05-28
  • 打赏
  • 举报
回复
lZ的创建链表有问题,不是输出的问题
LinkList CreateList()
{

int i, n, j;
cout<<"Input data number:";
cin>>n;

LinkList q, p, head;
head = new LNode;
head->next = NULL;
q = head;

for (i = 0; i < n; i++)
{
p = new LNode;
cout<<"Input Information:"<<endl<<"Sno";
cin>>p->sno;
cout<<"name:";
cin>>p->name;

for (j=0; j<3; j++)
{
cout<<"Score ["<<j<<"]";
cin>>p->score[j];
}

p->next = NULL;
q->next = p;
q = p;
}

return (head);
}


给你一段c++的
Jophoenix 2010-05-27
  • 打赏
  • 举报
回复
还是不懂哪错了诶。
stein42 2010-05-27
  • 打赏
  • 举报
回复
STU *creatlist(void);

这个在返回前怎么就用free()了,光有返回值怎么向现有链表添加数据啊。

void outputlist(STU *head);

这个貌似最后一个记录没打印吧。

建议楼主先学习下链表的基本操作。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧