鱼C论坛

 找回密码
 立即注册
查看: 1760|回复: 2

小白求助 关于链表逆序的问题

[复制链接]
发表于 2020-3-24 18:44:01 | 显示全部楼层 |阅读模式

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

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

x
我是照视频打的代码 前面的运行都成功 最后的逆序运行失败

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"

  5. typedef struct Node
  6. {
  7.         int data;
  8.         struct Node* next;
  9. }SLIST;

  10. //创建链表
  11. SLIST* List_Create();
  12. //遍历链表
  13. int List_Print(SLIST* pHead);
  14. //往x前面插入y
  15. int List_Insert(SLIST* pHead, int x, int y);
  16. //删除结点
  17. int List_Delete(SLIST* pHead, int x);
  18. //销毁链表
  19. int List_Destory(SLIST* pHead);
  20. //链表逆序
  21. int List_Inverse(SLIST* pHead);

  22. void main()
  23. {
  24.         SLIST* pHead = NULL;
  25.         pHead = List_Create();

  26.         List_Print(pHead);

  27.         List_Insert(pHead, 4, 199);

  28.         List_Print(pHead);

  29.         List_Delete(pHead, 6);

  30.         List_Print(pHead);

  31.         List_Destory(pHead);

  32.         List_Inverse(pHead);

  33.         List_Print(pHead);

  34.         system("pause");
  35. }

  36. SLIST* List_Create()
  37. {
  38.         SLIST* pHead = NULL;
  39.         SLIST* pCur = NULL;

  40.         int num = 0;
  41.         while (num != -1)
  42.         {
  43.                 printf("请输入num的值:");
  44.                 scanf("%d", &num);
  45.                 if (num == -1)
  46.                 {
  47.                         break;
  48.                 }

  49.                 SLIST* pM = (SLIST*)malloc(sizeof(SLIST));
  50.                 if (pM == NULL)
  51.                 {
  52.                         return NULL;
  53.                 }
  54.                 pM->data = num;
  55.                 pM->next = NULL;
  56.                 if (pHead == NULL)
  57.                 {
  58.                         pHead = pCur = pM;
  59.                 }
  60.                 else
  61.                 {
  62.                         pCur->next = pM;
  63.                         pCur = pM;
  64.                 }
  65.         }
  66.         printf("num等于-1,存值结束\n");
  67.         return pHead;
  68. }

  69. int List_Print(SLIST* pHead)
  70. {
  71.         SLIST* pCur = NULL;
  72.         for (pCur = pHead; pCur; pCur = pCur->next)
  73.         {
  74.                 printf("%d ", pCur->data);
  75.         }
  76.         printf("\n");
  77.         return 0;
  78. }

  79. int List_Insert(SLIST* pHead, int x, int y)//链表是单向的,链表的位置保存在前驱结点中
  80. {
  81.         SLIST* pFre = pHead;
  82.         SLIST* pCur = pHead->next;
  83.         SLIST* pM = (SLIST*)malloc(sizeof(SLIST));
  84.         if (pM == NULL)
  85.         {
  86.                 return -1;
  87.         }
  88.         pM->data = y;
  89.         pM->next = NULL;
  90.         while (pCur)
  91.         {
  92.                 if (pCur->data == x)
  93.                 {
  94.                         break;
  95.                 }
  96.                 pFre = pCur;
  97.                 pCur = pCur->next;
  98.         }
  99.         pM->next = pFre->next;
  100.         pFre->next = pM;
  101.                
  102.         return 0;
  103. }

  104. int List_Delete(SLIST* pHead, int x)
  105. {
  106.         SLIST* pCur = pHead->next;
  107.         SLIST* pFre = pHead;
  108.         while (pCur)
  109.         {
  110.                 if (pCur->data == x)
  111.                 {
  112.                         break;
  113.                 }
  114.                 pFre = pCur;
  115.                 pCur = pCur->next;
  116.         }
  117.         if (pCur != NULL)
  118.         {
  119.                 pFre->next = pCur->next;
  120.                 free(pCur);
  121.         }
  122.         else
  123.         {
  124.                 printf("不存在该元素\n");
  125.                 return -1;
  126.         }
  127.         return 0;
  128. }

  129. int List_Destory(SLIST* pHead)
  130. {
  131.         SLIST* tmp = pHead;
  132.         while (tmp)
  133.         {
  134.                 tmp = pHead->next;
  135.                 free(pHead);
  136.                 pHead = tmp;
  137.         }
  138. }

  139. //逆序的时候 需要缓存下一个结点的位置  这里是逆序
  140. int List_Inverse(SLIST* pHead)
  141. {
  142.         if (pHead == NULL || pHead->next == NULL ||pHead->next->next == NULL)
  143.         {
  144.                 return -1;
  145.         }
  146.         SLIST* tmp = NULL;
  147.         SLIST* pCur = pHead->next;
  148.         SLIST* pFre = pHead;
  149.         while (pCur)
  150.         {
  151.                 tmp = pCur->next;
  152.                 pCur->next = pFre;
  153.                 pFre = pCur;
  154.                 pCur = tmp;
  155.         }
  156.         pHead->next->next = NULL;
  157.         pHead->next = pFre;
  158.         return 0;
  159. }
复制代码


逆序失败

逆序失败
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-25 08:58:33 | 显示全部楼层
  1. if (pHead == NULL || pHead->next == NULL ||pHead->next->next == NULL)
复制代码

我觉得这句话有问题,这里应该是判断单链表为空链表或者单链表只有一个元素。但是pHead->next->next是有问题的。pHead->next指向的是下一个元素而不是下一个节点。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 15:35:27 | 显示全部楼层
哦哦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-2 22:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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