运行这个程序,电脑CPU使用空前高(不是标题党)

E_stream 2019-08-18 05:27:31
代码就是这样的,是个列表的建立和输出。和书上几乎无异(我改了一点,no warning,no error) #include<stdio.h> #include<malloc.h> int n=1; // 用来统计链表中节点个数。 struct Student { int num; float score; struct Student * next; }; int main() { struct Student * head; struct Student * creat(); // 输入函数的声明 void print(struct Student * head); // 输出函数 head=creat(); print(head); return 0; } struct Student * creat() { struct Student *head; struct Student *p1,*p2; head=NULL; p1=p2=(struct Student *)malloc(sizeof(struct Student)); scanf("%d%f",&p1->num,&p1->score); // 输入第一个节点的数据 while(p1->num!=0) { if(n==1) head=p1; else {p2->next=p1;p2=p1;} // 接着再开辟一个新的节点 p1=(struct Student *)malloc(sizeof(struct Student)); scanf("%d%f",&p1->num,&p1->score); n++; } p2->next=NULL; return (head); } void print(struct Student * head) { struct Student * p; int i; printf("the %d records are :\n",n); printf("num score\n"); for(i=0,p=head;i<n;i++) {printf("%d %f\n",p->num,p->score); p=p->next;} } 但程序却运行不正确,且CPU使用达百分之九十左右。 why
...全文
70 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2019-09-02
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
E_stream 2019-08-19
  • 打赏
  • 举报
回复
引用 7 楼 自信男孩 的回复:
#include<stdio.h>
#include<malloc.h>

//int n=1; // 用来统计链表中节点个数。
int n=0; // 用来统计链表中节点个数。
struct Student
{
int num;
float score;
struct Student * next;
};

int main()
{
struct Student * head;

struct Student * creat(); // 输入函数的声明
void print(struct Student * head); // 输出函数

head=creat();
print(head);

return 0;
}

struct Student * creat()
{
struct Student *head;
struct Student *p1,*p2;

head=NULL;
p1=p2=(struct Student *)malloc(sizeof(struct Student));

scanf("%d%f",&p1->num,&p1->score);
// 输入第一个节点的数据

while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else {
p2->next=p1;
p2=p1;
}

// 接着再开辟一个新的节点
p1 = (struct Student *)malloc(sizeof(struct Student));
scanf("%d%f",&p1->num,&p1->score);
}
free(p1); //最后一个几点需要释放掉,因为并没有链入链表中
p2->next=NULL;

printf("n = %d\n", n);
return (head);
}

void print(struct Student * head)
{
struct Student * p;
int i;
printf("the %d records are :\n",n);
printf("num score\n");
for(i=0,p=head; i < n; i++) {
printf("%d %f\n",p->num,p->score);
p=p->next;
}
}


程序有问题,输出的时候,按照楼主的逻辑,最后一个节点也算上,但是最后一个节点并没有链入链表中,而是直接返回了。所以
n的值比链表实际长度大1.
看一下上面的逻辑~
辛苦了,老哥 free(p1) // 这个学到了 那个n,我也知道为啥了,确实有毛病
自信男孩 2019-08-19
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>

//int n=1; // 用来统计链表中节点个数。
int n=0; // 用来统计链表中节点个数。
struct Student
{
int num;
float score;
struct Student * next;
};

int main()
{
struct Student * head;

struct Student * creat(); // 输入函数的声明
void print(struct Student * head); // 输出函数

head=creat();
print(head);

return 0;
}

struct Student * creat()
{
struct Student *head;
struct Student *p1,*p2;

head=NULL;
p1=p2=(struct Student *)malloc(sizeof(struct Student));

scanf("%d%f",&p1->num,&p1->score);
// 输入第一个节点的数据

while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else {
p2->next=p1;
p2=p1;
}

// 接着再开辟一个新的节点
p1 = (struct Student *)malloc(sizeof(struct Student));
scanf("%d%f",&p1->num,&p1->score);
}
free(p1); //最后一个几点需要释放掉,因为并没有链入链表中
p2->next=NULL;

printf("n = %d\n", n);
return (head);
}

void print(struct Student * head)
{
struct Student * p;
int i;
printf("the %d records are :\n",n);
printf("num score\n");
for(i=0,p=head; i < n; i++) {
printf("%d %f\n",p->num,p->score);
p=p->next;
}
}


程序有问题,输出的时候,按照楼主的逻辑,最后一个节点也算上,但是最后一个节点并没有链入链表中,而是直接返回了。所以
n的值比链表实际长度大1.
看一下上面的逻辑~
E_stream 2019-08-19
  • 打赏
  • 举报
回复
引用 4 楼 赵4老师 的回复:
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
哦吼,出来了,加深了解了,谢谢老师,以后会注意的
E_stream 2019-08-18
  • 打赏
  • 举报
回复
引用 4 楼 赵4老师 的回复:
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
嗯,好的(心急了)
赵4老师 2019-08-18
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
E_stream 2019-08-18
  • 打赏
  • 举报
回复
运行不成功的具体表现是: 书上输入(1001,99 1002,98 0,0) 即输出两节点数据 而我不一样...同上输入后,按回车键没反应,瞎试了试发现(无法输入数字,而输入字符后按回车字符消失)并且额..电脑喘的厉害....
E_stream 2019-08-18
  • 打赏
  • 举报
回复
引用 1 楼 (.....) 的回复:
积极向上正能量.....
我电脑喘了好一阵子
E_stream 2019-08-18
  • 打赏
  • 举报
回复
积极向上正能量.....

69,373

社区成员

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

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