鱼C论坛

 找回密码
 立即注册
查看: 799|回复: 1

[已解决]单链表,为什么无法打印节点,看子函数PrintTable即可

[复制链接]
发表于 2022-3-17 15:00:02 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 猪猪虾 于 2022-3-17 15:05 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>

  5. /******************************************************
  6. 要求:
  7.         定义一个结构体Node,结构体由两部分组成,第一个是它本身的值,第二个是下一个元素的位置
  8.         数组有3个节点,3个节点的关系如下
  9.                 0                       1                       2
  10.         node1(5,2)                                node2(7,-1)                                node3(8,1)

  11.         node1是第一个节点,指向的下一个节点的位置是2,也即是node3,node3指向的节点是位置1,也即是node2

  12. ********************************************************/

  13. #define Size 10
  14. void getInput(struct Node* node);
  15. void HeadInsert(struct Node** Ptable);
  16. void PrintTable(struct Node* Ptable);
  17. void initialTable(struct Node* Table[]);
  18. void ReleaseTable(struct Node** Ptable);


  19. struct Node
  20. {
  21.         int value;
  22.         int nextNodeIndex;
  23.         struct Node* next;
  24. };

  25. void getInput(struct Node* node)
  26. {
  27.         printf("enter node'value:");
  28.         scanf_s("%d", &node->value);  //这是scanf的基本用法,要传入输入变量的地址,node->value是结构体对应的成员
  29.         printf("enter node'value:");  //当输入的是是字符串的时候,字符串变量名本身就是首地址
  30.         scanf_s("%d", &node->nextNodeIndex);
  31. }

  32. void HeadInsert(struct Node** Ptable)
  33. {
  34.         //头插法:永远将数据插入链表的头部,称为头插法
  35.         struct Node* temp;

  36.         struct Node* node = new Node;
  37.         if (node == NULL)
  38.         {
  39.                 printf("failed to distribute memory....\n");
  40.                 exit(1);
  41.         }

  42.         getInput(node);


  43.         if (*Ptable != NULL)  //判断传进来的单链表是不是空的,即判断第第一个节点的地址是不是空的
  44.         {
  45.                 temp = *Ptable;
  46.                 *Ptable = node;
  47.                 node->next = temp;
  48.         }
  49. }

  50. void PrintTable(struct Node* Ptable)
  51. {
  52.         struct Node* node;
  53.         int count = 1;
  54.         node = Ptable;

  55.         while (node != NULL)
  56.         {
  57.                 printf("node'value: %d\n", node->value);
  58.                 printf("enter node'value: %d\n", node->nextNodeIndex);
  59.                 printf("\n");
  60.                 node = node->next;
  61.                 count++;
  62.         }
  63. }

  64. void initialTable(struct Node* Table[])
  65. {
  66.         for (int i = 0; i < Size; i++)
  67.         {
  68.                 Table[i] = NULL;
  69.         }
  70. }

  71. void ReleaseTable(struct Node** Ptable)
  72. {
  73.         struct Node* temp;
  74.         for (int i = 0; i < Size; i++)
  75.         {
  76.                 if (*Ptable != NULL) //*Ptable这里是解引用,对应第一个节点的地址
  77.                 {
  78.                         temp = *Ptable;
  79.                         *Ptable = (*Ptable)->next;
  80.                         free(temp);
  81.                 }
  82.         }
  83. }

  84. int main()
  85. {
  86.         struct Node* Table[Size];
  87.         struct Book* ptr = NULL;
  88.         initialTable(Table);

  89.         HeadInsert(Table);

  90.         PrintTable(*Table);

  91.         //Table 的定义是   struct Node* Table[Size];
  92.         //这是一个指针数组,实际上与  struct Node** 是相同的数据类型
  93.         //是一个指针数组,存放的是 struct Node* 类型的指针
  94.         //        Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node* 的地址
  95.         ReleaseTable(Table); //* Table[]是指针数组,存放的是指针,Table指代的第一个元素的地址
  96.         return 0;
  97. }
复制代码
最佳答案
2022-3-17 15:53:46
本帖最后由 jhq999 于 2022-3-17 15:58 编辑
  1. void HeadInsert(struct Node** Ptable)///////主函数传进来的* Ptable=NULL;
  2. {
  3.         //头插法:永远将数据插入链表的头部,称为头插法
  4.         struct Node* temp;

  5.         struct Node* node = new Node;
  6.         if (node == NULL)
  7.         {
  8.                 printf("failed to distribute memory....\n");
  9.                 exit(1);
  10.         }

  11.         getInput(node);


  12.         if (*Ptable != NULL)  //判断传进来的单链表是不是空的,即判断第第一个节点的地址是不是空的
  13.         {
  14.                 temp = *Ptable;
  15.                 *Ptable = node;
  16.                 node->next = temp;
  17.         }
  18. }
  19. int main()
  20. {
  21.         struct Node* Table[Size];
  22.         struct Book* ptr = NULL;
  23.         initialTable(Table);//Node指针数组Table所有的指针初始化成NULL

  24.         HeadInsert(Table);//*Table=NULL传进去的也是NULL

  25.         PrintTable(*Table);

  26.         //Table 的定义是   struct Node* Table[Size];
  27.         //这是一个指针数组,实际上与  struct Node** 是相同的数据类型
  28.         //是一个指针数组,存放的是 struct Node* 类型的指针
  29.         //        Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node* 的地址
  30.         ReleaseTable(Table); //* Table[]是指针数组,存放的是指针,Table指代的第一个元素的地址
  31.         return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-17 15:53:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-3-17 15:58 编辑
  1. void HeadInsert(struct Node** Ptable)///////主函数传进来的* Ptable=NULL;
  2. {
  3.         //头插法:永远将数据插入链表的头部,称为头插法
  4.         struct Node* temp;

  5.         struct Node* node = new Node;
  6.         if (node == NULL)
  7.         {
  8.                 printf("failed to distribute memory....\n");
  9.                 exit(1);
  10.         }

  11.         getInput(node);


  12.         if (*Ptable != NULL)  //判断传进来的单链表是不是空的,即判断第第一个节点的地址是不是空的
  13.         {
  14.                 temp = *Ptable;
  15.                 *Ptable = node;
  16.                 node->next = temp;
  17.         }
  18. }
  19. int main()
  20. {
  21.         struct Node* Table[Size];
  22.         struct Book* ptr = NULL;
  23.         initialTable(Table);//Node指针数组Table所有的指针初始化成NULL

  24.         HeadInsert(Table);//*Table=NULL传进去的也是NULL

  25.         PrintTable(*Table);

  26.         //Table 的定义是   struct Node* Table[Size];
  27.         //这是一个指针数组,实际上与  struct Node** 是相同的数据类型
  28.         //是一个指针数组,存放的是 struct Node* 类型的指针
  29.         //        Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node* 的地址
  30.         ReleaseTable(Table); //* Table[]是指针数组,存放的是指针,Table指代的第一个元素的地址
  31.         return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-4-24 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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