C语言 链表出问题了 急!!!!!! 大家帮帮忙

阿尔文实得分 2012-02-12 05:02:40
源代码
#include "stdio.h"
#include "stdlib.h"
struct stu
{
int index;
float score;
struct stu *next;
};


void CreateList(struct stu *head)
{
struct stu *temp=head; //将头指针赋给一个临时变量
int i=1;
printf("请输入5位学生的成绩:\n");
while(i<=5)
{
struct stu *newNode=(struct stu*)malloc(sizeof(struct stu)); //为新结点分配空间
newNode->next=NULL; //新节点next域赋值为NULL
newNode->index=i++; //为新结点赋值
scanf("%f",&newNode->score);
temp->next=newNode; //将新结点连到链表末尾
temp=newNode; //修改临时指针指向新结点
}
}
void main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
CreateList(head);


}
执行输出后 不能截图 大家看看这代码哪里出错了 我是VC6.0 编译的
出现错误
...全文
162 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿尔文实得分 2012-02-13
  • 打赏
  • 举报
回复
谢谢大家 问题已经解决了 非常感谢 好人一生平安
newfarmerchi 2012-02-12
  • 打赏
  • 举报
回复


#include "stdio.h"
#include "stdlib.h"
struct stu
{
int index;
float score;
struct stu *next;
};


void CreateList(struct stu **head)//--------->传二级指针
{
struct stu *temp=(*head); //------->here将头指针赋给一个临时变量
int i=1;
printf("请输入5位学生的成绩:\n");
while(i<=5)
{
struct stu *newNode=(struct stu*)malloc(sizeof(struct stu)); //为新结点分配空间
newNode->next=NULL; //新节点next域赋值为NULL
newNode->index=i++; //为新结点赋值
scanf("%f",&newNode->score);
temp->next=newNode; //将新结点连到链表末尾
temp=newNode; //修改临时指针指向新结点
}
}
void main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
CreateList(&head);//----->here
struct stu *t = head->next;
while (t)
{
printf("%d %.2f\n", t->index, t->score);
t=t->next;

}
}
//以下是一个结果
请输入5位学生的成绩:
66.7
55.8
99.8
34.5
67.98
1 66.70
2 55.80
3 99.80
4 34.50
5 67.98
Press any key to continue







rick_wym 2012-02-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

struct stu
{
int index;
float score;
struct stu *next;
};


void CreateList(struct stu *head)
{
struct stu *temp=head; //将头指针赋给一个临时变量

int i = 1 ;

printf("请输入5位学生的成绩:\n");

temp->index = i; //给首先输入第一个成绩

float score_temp;

scanf("%f",&score_temp);

temp->score = score_temp;

temp->next = NULL;

while(i < 5)
{


struct stu *newNode=(struct stu*)malloc(sizeof(struct stu)); //为新结点分配空间

newNode->next=NULL; //新节点next域赋值为NULL

newNode->index= i + 1 ; //为新结点赋值

scanf("%f",&(newNode->score));

temp->next=newNode; //将新结点连到链表末尾

temp=newNode; //修改临时指针指向新结点

i++;
}
}

void main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
//head->next=NULL;
CreateList(head);
}
rick_wym 2012-02-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

struct stu
{
int index;
float score;
struct stu *next;
};


void CreateList(struct stu *head)
{
struct stu *temp=head; //将头指针赋给一个临时变量

int i = 1 ;

printf("请输入5位学生的成绩:\n");

temp->index = i; //给首先输入第一个成绩

float score_temp;

scanf("%f",&score_temp);

temp->score = score_temp;

temp->next = NULL;

while(i < 5)
{


struct stu *newNode=(struct stu*)malloc(sizeof(struct stu)); //为新结点分配空间

newNode->next=NULL; //新节点next域赋值为NULL

newNode->index= i + 1 ; //为新结点赋值

scanf("%f",&(newNode->score));
temp->next=newNode; //将新结点连到链表末尾

temp=newNode; //修改临时指针指向新结点

i++;
}
}

void main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
//head->next=NULL;
CreateList(head);
}

scanf("%f",&temp->score);改为scanf("%f",&(temp->score));
涉及取址操作符&
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
还是出错呀 怎么办
面包大师 2012-02-12
  • 打赏
  • 举报
回复
就改成我这样子啊。。。
scanf("%f",&(newNode->score));
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
改为head? 可是还是出错呀
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
怎么改
面包大师 2012-02-12
  • 打赏
  • 举报
回复
scanf("%f",&(newNode->score));//把输入改下就好了
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
如何让上一个节点与下一个节点连接起来呢 请大侠指教
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
发现了断点怎么办
面包大师 2012-02-12
  • 打赏
  • 举报
回复
红点是断点。。。
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
有一个红点指的是错误的地方吗 可是 它指的是括号 main 第一个括号
taoyh2002 2012-02-12
  • 打赏
  • 举报
回复
程序本身没看出来问题,你还是调一下看看
F9 设断点, F5运行, F10 单步跟踪
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
不是 调试的时候没出现问题 VC 也显示没错误 一输入成绩时 就出现了那个错误的对话框
wifi狂人 2012-02-12
  • 打赏
  • 举报
回复
在linux下编译和运行都没发现有什么问题呀。我估计应该是VC的问题。
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
你能改一下源代码吗 这样说 我不好理解
taoyh2002 2012-02-12
  • 打赏
  • 举报
回复
单步跟踪一下,看看哪条语句出的错
阿尔文实得分 2012-02-12
  • 打赏
  • 举报
回复
不懂你说的什么???
Wolf0403 2012-02-12
  • 打赏
  • 举报
回复
你给 CreateList 传进去的 head 节点没有被赋值。

把 printf / scanf 和修改 stu 结构的代码分开方便调试。
写一个函数 void UpdateNode (struct stu *p, int index, float score, struct stu *next) 修改第一个传入的结构体,然后先验证所有 scanf 进来的值再更新到 struct 里面去。
加载更多回复(2)

64,686

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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