鱼C论坛

 找回密码
 立即注册
查看: 4086|回复: 3

打印链表

[复制链接]
发表于 2013-1-16 15:06:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #define LEN sizeof(struct student )
  3. int main(void)
  4. {
  5.        
  6.         struct student
  7.         {
  8.                 int num;
  9.                 float score;
  10.                 struct student *next;
  11.         };
  12.        
  13.         struct student stu1,stu2;
  14.         struct student *pstu1,*pstu2,*pstu3;
  15.         pstu1=&stu1;
  16.         pstu2=&stu2;
  17.        
  18.         stu1.num=001;
  19.         stu1.score  =88.8;
  20.         pstu1->next = &stu2;
  21.        
  22.         //stu2.next=NULL;// 如果这里不设定为 NULL , 那么stu.next 是指向哪里的呢  ?

  23.         stu2.num =003;
  24.         stu2.score =99.9;
  25. do       
  26. {
  27.         printf("%d %f",pstu1->num,pstu1->score);
  28.         pstu1=pstu1->next;
  29. }        while(pstu1->next!=NULL);


  30. return 0;
  31. }

  32. /*
  33. 运行结果

  34. *********************************************************
  35. 1 88.800003请按任意键继续. . .

  36. *********************************************************
  37. */

  38. /*

  39. 如果将上方 stu2.next=NULL;  注释掉的话,
  40. 会输出
  41. *********************************************************
  42. 1 88.8000033 99.900002请按任意键继续. . .

  43. *********************************************************
  44. 但是编译器会报错

  45. 请教如何打印此链表?

  46. */
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-16 15:12:19 | 显示全部楼层
既然lz都不知道,那计算机会知道么?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-16 16:20:40 | 显示全部楼层
这是你写的那个循环结束条件出错了,应该是pstu1!=NULL才对。你可以画一下你写的那个链表,如果按你写的那样,当输出第一个学生的信息后,pstu1指向第二个学生了,这是pstu1的next已经是是NULL了,所以就不会输出第二的学生的信息。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-16 18:18:33 | 显示全部楼层
你如果pstu2->next 没有定义的话,就指向一个垃圾值。所以要指向NULL
  1. do        
  2. {
  3.         printf("%d %f",pstu1->num,pstu1->score);
  4.         pstu1=pstu1->next;  //首先你不能直接用pstu1来直接操作,这操作后,pstu1所指向的值就发生改变了,不利用下次的使用。你之前定义了一个指针pstu3没有用,可以用着它。
  5. }        while(pstu1->next!=NULL);
复制代码
// 你可以画出节点进行分析,然后你就知道你那里错了。

  1. pstu3=pstu1;
  2. do        
  3. {
  4.         printf("%d %f\n",pstu3->num,pstu3->score);
  5.         pstu3=pstu3->next;
  6. }        while(pstu3!=NULL);
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-9 04:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表