鱼C论坛

 找回密码
 立即注册
查看: 3619|回复: 14

[已解决]怎么将链表首尾连接?

[复制链接]
发表于 2022-5-23 15:15:30 | 显示全部楼层 |阅读模式
23鱼币
  1. struct SUN
  2. {
  3.         int num;
  4.         struct SUN* next;
  5. };
  6. void Pint(struct SUN* head)
  7. {
  8.         struct SUN* temp;
  9.         temp = head;
  10.        
  11.         while (temp != NULL)
  12.         {
  13.                 printf("%d", temp->num);
  14.                 temp = temp->next;
  15.         }
  16. }


  17. void add(struct SUN** head, int num)
  18. {
  19.         struct SUN* space, * temp;
  20.         space = (struct SUN*)malloc(sizeof(struct SUN));

  21.         space->num = num;

  22.         temp = *head;
  23.         *head = space;
  24.         space->next = temp;
  25.        
  26.         if (space->next != NULL)
  27.         {
  28.                 printf("空间满了\n");
  29.         }
  30.         else
  31.         {
  32.                 space->next = *head;
  33.         }
  34. }
  35. int main()
  36. {
  37.         int num;
  38.         struct SUN* head = NULL;//链头

  39.         printf("input num:");
  40.         scanf("%d", &num);

  41.         for (int i = 0; i < num; i++)
  42.         {
  43.                 add(&head, i);
  44.         }
  45.         Pint(head);//若循环num ~ 0 则成功连接
  46.        
  47.         return 0;
  48. }
复制代码
最佳答案
2022-5-23 15:15:31
本帖最后由 傻眼貓咪 于 2022-5-23 17:43 编辑

我的思路:

第一次赋值:
头指针赋值后,next 指向自己便可。

接下来的赋值:

赋值函数,先传入头指针
头指针 -> 指针 A -> ............ -> 尾指针 -> 头指针

设新指针 p,将值赋值于 p
将 p 指向头指针->next(如上,也就是指针 A)
将头指针->next 指向 p

示例:头指针 -> p -> 指针 A -> ............ -> 尾指针 -> 头指针

  1. #include <stdio.h>
  2. #include <malloc.h>

  3. typedef struct Node {
  4.     int data;
  5.     struct Node* next;
  6. }LinkedList;

  7. void insert(LinkedList** head, int data) {
  8.     if (!(*head)) {
  9.         (*head) = (LinkedList*)malloc(sizeof(LinkedList));
  10.         if (*head) {
  11.             (*head)->data = data;
  12.             (*head)->next = (*head);
  13.         }
  14.     }
  15.     else {
  16.         LinkedList* p = (LinkedList*)malloc(sizeof(LinkedList));
  17.         if (p) {
  18.             p->data = data;
  19.             p->next = (*head)->next;
  20.             (*head)->next = p;
  21.         }
  22.     }
  23. }

  24. void freeAll(LinkedList** head) {
  25.     LinkedList* p, * q = (*head);
  26.     if (*head) {
  27.         p = (*head);
  28.         (*head) = (*head)->next;
  29.         free(p);
  30.     }while (q != head);
  31.     free(head);
  32. }

  33. void print(LinkedList* head) {
  34.     LinkedList* p = head;
  35.     if (head) {
  36.         do {
  37.             head = head->next;
  38.             printf("%d ", head->data);
  39.         } while (p != head);
  40.     }
  41. }

  42. int main(void) {
  43.     int arr[] = { 7, 14, 21, 28, 35 };
  44.     LinkedList* head = NULL;

  45.     for (int i = 0; i < 5; ++i) {
  46.         insert(&head, arr[i]);
  47.     }

  48.     print(head);
  49.     freeAll(&head);

  50.     return 0;
  51. }
复制代码

最佳答案

查看完整内容

我的思路: 第一次赋值: 头指针赋值后,next 指向自己便可。 接下来的赋值: 赋值函数,先传入头指针 头指针 -> 指针 A -> ............ -> 尾指针 -> 头指针 设新指针 p,将值赋值于 p 将 p 指向头指针->next(如上,也就是指针 A) 将头指针->next 指向 p 示例:头指针 -> p -> 指针 A -> ............ -> 尾指针 -> 头指针
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-23 18:22:09 | 显示全部楼层

谢谢各位大佬帮我解决,我找到问题了,其实一开始就是想找个连接链头链尾的方法而已,代码也只是一部分不完整,我出问题在NULL,每次返回指向NULL指针的地址,将他跟链头连接起来,而忘记指向NULL的指针就是0,没有地址,所以没有连起来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:15:31 | 显示全部楼层    本楼为最佳答案   
本帖最后由 傻眼貓咪 于 2022-5-23 17:43 编辑

我的思路:

第一次赋值:
头指针赋值后,next 指向自己便可。

接下来的赋值:

赋值函数,先传入头指针
头指针 -> 指针 A -> ............ -> 尾指针 -> 头指针

设新指针 p,将值赋值于 p
将 p 指向头指针->next(如上,也就是指针 A)
将头指针->next 指向 p

示例:头指针 -> p -> 指针 A -> ............ -> 尾指针 -> 头指针

  1. #include <stdio.h>
  2. #include <malloc.h>

  3. typedef struct Node {
  4.     int data;
  5.     struct Node* next;
  6. }LinkedList;

  7. void insert(LinkedList** head, int data) {
  8.     if (!(*head)) {
  9.         (*head) = (LinkedList*)malloc(sizeof(LinkedList));
  10.         if (*head) {
  11.             (*head)->data = data;
  12.             (*head)->next = (*head);
  13.         }
  14.     }
  15.     else {
  16.         LinkedList* p = (LinkedList*)malloc(sizeof(LinkedList));
  17.         if (p) {
  18.             p->data = data;
  19.             p->next = (*head)->next;
  20.             (*head)->next = p;
  21.         }
  22.     }
  23. }

  24. void freeAll(LinkedList** head) {
  25.     LinkedList* p, * q = (*head);
  26.     if (*head) {
  27.         p = (*head);
  28.         (*head) = (*head)->next;
  29.         free(p);
  30.     }while (q != head);
  31.     free(head);
  32. }

  33. void print(LinkedList* head) {
  34.     LinkedList* p = head;
  35.     if (head) {
  36.         do {
  37.             head = head->next;
  38.             printf("%d ", head->data);
  39.         } while (p != head);
  40.     }
  41. }

  42. int main(void) {
  43.     int arr[] = { 7, 14, 21, 28, 35 };
  44.     LinkedList* head = NULL;

  45.     for (int i = 0; i < 5; ++i) {
  46.         insert(&head, arr[i]);
  47.     }

  48.     print(head);
  49.     freeAll(&head);

  50.     return 0;
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:24:32 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-23 15:27:16 | 显示全部楼层
豆嘉木 发表于 2022-5-23 15:24
要不看看这个:https://blog.csdn.net/qq_58190943/article/details/119079769

主要是我想知道我错哪了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:37:38 | 显示全部楼层
是想构建循环单链表吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:46:16 | 显示全部楼层
1187520285 发表于 2022-5-23 15:27
主要是我想知道我错哪了

调试别人的代码 是最难的,根本就看不懂你的思路
你是构建无头节点链表吗?
if (space->next != NULL)   你从来都没指定NULL  这个if 岂不是永远成立
        {
                printf("空间满了\n");
        }

PS D:\wp> ./ct6
input num:5
空间满了
空间满了
空间满了
空间满了

4
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-23 15:50:25 | 显示全部楼层
wp231957 发表于 2022-5-23 15:46
调试别人的代码 是最难的,根本就看不懂你的思路
你是构建无头节点链表吗?
if (space->next != NULL)  ...

这个if是为了找出尾节点,顺便打印下开辟了多少节点而已无其他意思!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:54:33 | 显示全部楼层
1187520285 发表于 2022-5-23 15:50
这个if是为了找出尾节点,顺便打印下开辟了多少节点而已无其他意思!

问题是你找得到吗?
如果是尾插法,你需要自己设置NULL
如果是头插法,你也需要自己设置NULL
而我就没看到你设置NULL  也没看明白你是有头链表还是无头链表  是头插法还是尾插法
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:57:30 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-23 16:08 编辑
  1. void add(struct SUN** head, int num)
  2. {
  3.         struct SUN* space, * temp;
  4.         space = (struct SUN*)malloc(sizeof(struct SUN));

  5.         space->num = num;
  6.         if(*head==NULL)*head=space,space->next=*head;////////
  7.         temp = *head;//////
  8.         while(temp->next!=*head)temp=temp->next;///////////////////楼上说的无头节点的头插法
  9.         temp->next=space;//////////////////
  10.         space->next = *head;////////
  11.         *head = space;/////////
  12.        

  13.        
  14.        /* if (space->next != NULL)
  15.         {
  16.                 printf("空间满了\n");
  17.         }
  18.         else
  19.         {
  20.                 space->next = *head;
  21.         }*/
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 16:14:45 | 显示全部楼层
如果你要用头插法,还不带头节点,那就必须添加一个尾指针,每次添加节点时都要调整尾部节点指向新节点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 17:12:54 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct SUN {
  4.     int num;
  5.     struct SUN *next;
  6. };

  7. void Pint(struct SUN *head) {
  8.     struct SUN *temp;
  9.     temp = head;

  10.     do {
  11.         printf("%d", temp->num);
  12.         temp = temp->next;
  13.     } while(temp != head);

  14. #if 0
  15.     while(temp != NULL) {
  16.         printf("%d", temp->num);
  17.         temp = temp->next;
  18.     }
  19. #endif
  20. }

  21. void add(struct SUN **head, int num) {
  22.     //struct SUN *space, *temp;
  23.     //space = (struct SUN *)malloc(sizeof(struct SUN));
  24.     struct SUN *space = malloc(sizeof(struct SUN));

  25.     space->num = num;

  26.     if(!*head) {
  27.         space->next = space;
  28.         *head = space;
  29.         return;
  30.     }

  31.     struct SUN *prev = (*head)->next;
  32.     while(prev->next != *head) prev = prev->next;
  33.     space->next = prev->next;
  34.     prev->next = space;

  35. #if 0
  36.     temp = *head;
  37.     *head = space;
  38.     space->next = temp;

  39.     if(space->next != NULL) {
  40.         printf("空间满了\n");
  41.     } else {
  42.         space->next = *head;
  43.     }
  44. #endif
  45. }

  46. void free_list(struct SUN *head) {
  47.     struct SUN *current = head, *temp;
  48.     do {
  49.         temp = current;
  50.         current = current->next;
  51.         free(temp);
  52.     } while(current != head);
  53. }

  54. int main(void) {
  55.     int num;
  56.     struct SUN *head = NULL; //链头

  57.     printf("input num:");
  58.     scanf("%d", &num);

  59.     for(int i = 0; i < num; i++) {
  60.         add(&head, i);
  61.     }
  62.     Pint(head); //若循环num ~ 0 则成功连接
  63.     free_list(head);

  64.     puts("");
  65.     return 0;
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-23 18:18:06 | 显示全部楼层
  1. void link(struct SUN *head)
  2. {
  3.         struct SUN* temp;
  4.         temp = head->next;

  5.         while (temp->next != NULL)
  6.         {
  7.                 printf("2\n");
  8.                 temp = temp->next;
  9.         }
  10.         temp->next = head;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-23 18:23:40 | 显示全部楼层
傻眼貓咪 发表于 2022-5-23 17:39
我的思路:

第一次赋值:

虽然问题我找到了,也谢谢你,你字多给你了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-19 20:20:26 | 显示全部楼层
循环链表吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 16:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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