鱼C论坛

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

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

[复制链接]
发表于 2022-5-23 15:15:30 | 显示全部楼层 |阅读模式
23鱼币
struct SUN
{
        int num;
        struct SUN* next;
};
void Pint(struct SUN* head)
{
        struct SUN* temp;
        temp = head;
        
        while (temp != NULL)
        {
                printf("%d", temp->num);
                temp = temp->next;
        }
}


void add(struct SUN** head, int num)
{
        struct SUN* space, * temp;
        space = (struct SUN*)malloc(sizeof(struct SUN));

        space->num = num;

        temp = *head;
        *head = space;
        space->next = temp;
        
        if (space->next != NULL)
        {
                printf("空间满了\n");
        }
        else
        {
                space->next = *head;
        }
}
int main()
{
        int num;
        struct SUN* head = NULL;//链头

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

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

我的思路:

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

接下来的赋值:

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

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

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

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

typedef struct Node {
    int data;
    struct Node* next;
}LinkedList;

void insert(LinkedList** head, int data) {
    if (!(*head)) {
        (*head) = (LinkedList*)malloc(sizeof(LinkedList));
        if (*head) {
            (*head)->data = data;
            (*head)->next = (*head);
        }
    }
    else {
        LinkedList* p = (LinkedList*)malloc(sizeof(LinkedList));
        if (p) {
            p->data = data;
            p->next = (*head)->next;
            (*head)->next = p;
        }
    }
}

void freeAll(LinkedList** head) {
    LinkedList* p, * q = (*head);
    if (*head) {
        p = (*head);
        (*head) = (*head)->next;
        free(p);
    }while (q != head);
    free(head);
}

void print(LinkedList* head) {
    LinkedList* p = head;
    if (head) {
        do {
            head = head->next;
            printf("%d ", head->data);
        } while (p != head);
    }
}

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

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

    print(head);
    freeAll(&head);

    return 0;
}

最佳答案

查看完整内容

我的思路: 第一次赋值: 头指针赋值后,next 指向自己便可。 接下来的赋值: 赋值函数,先传入头指针 头指针 -> 指针 A -> ............ -> 尾指针 -> 头指针 设新指针 p,将值赋值于 p 将 p 指向头指针->next(如上,也就是指针 A) 将头指针->next 指向 p 示例:头指针 -> p -> 指针 A -> ............ -> 尾指针 -> 头指针
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

使用道具 举报

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

我的思路:

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

接下来的赋值:

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

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

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

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

typedef struct Node {
    int data;
    struct Node* next;
}LinkedList;

void insert(LinkedList** head, int data) {
    if (!(*head)) {
        (*head) = (LinkedList*)malloc(sizeof(LinkedList));
        if (*head) {
            (*head)->data = data;
            (*head)->next = (*head);
        }
    }
    else {
        LinkedList* p = (LinkedList*)malloc(sizeof(LinkedList));
        if (p) {
            p->data = data;
            p->next = (*head)->next;
            (*head)->next = p;
        }
    }
}

void freeAll(LinkedList** head) {
    LinkedList* p, * q = (*head);
    if (*head) {
        p = (*head);
        (*head) = (*head)->next;
        free(p);
    }while (q != head);
    free(head);
}

void print(LinkedList* head) {
    LinkedList* p = head;
    if (head) {
        do {
            head = head->next;
            printf("%d ", head->data);
        } while (p != head);
    }
}

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

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

    print(head);
    freeAll(&head);

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:24:32 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

主要是我想知道我错哪了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 15:37:38 | 显示全部楼层
是想构建循环单链表吗
想知道小甲鱼最近在做啥?请访问 -> 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

这个if是为了找出尾节点,顺便打印下开辟了多少节点而已无其他意思!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

问题是你找得到吗?
如果是尾插法,你需要自己设置NULL
如果是头插法,你也需要自己设置NULL
而我就没看到你设置NULL  也没看明白你是有头链表还是无头链表  是头插法还是尾插法
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

        space->num = num;
        if(*head==NULL)*head=space,space->next=*head;////////
        temp = *head;//////
        while(temp->next!=*head)temp=temp->next;///////////////////楼上说的无头节点的头插法
        temp->next=space;//////////////////
        space->next = *head;////////
        *head = space;/////////
        

        
       /* if (space->next != NULL)
        {
                printf("空间满了\n");
        }
        else
        {
                space->next = *head;
        }*/
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

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

struct SUN {
    int num;
    struct SUN *next;
};

void Pint(struct SUN *head) {
    struct SUN *temp;
    temp = head;

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

#if 0
    while(temp != NULL) {
        printf("%d", temp->num);
        temp = temp->next;
    }
#endif
}

void add(struct SUN **head, int num) {
    //struct SUN *space, *temp;
    //space = (struct SUN *)malloc(sizeof(struct SUN));
    struct SUN *space = malloc(sizeof(struct SUN));

    space->num = num;

    if(!*head) {
        space->next = space;
        *head = space;
        return;
    }

    struct SUN *prev = (*head)->next;
    while(prev->next != *head) prev = prev->next;
    space->next = prev->next;
    prev->next = space;

#if 0
    temp = *head;
    *head = space;
    space->next = temp;

    if(space->next != NULL) {
        printf("空间满了\n");
    } else {
        space->next = *head;
    }
#endif
}

void free_list(struct SUN *head) {
    struct SUN *current = head, *temp;
    do {
        temp = current;
        current = current->next;
        free(temp);
    } while(current != head);
}

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

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

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

    puts("");
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

        while (temp->next != NULL)
        {
                printf("2\n");
                temp = temp->next;
        }
        temp->next = head;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

第一次赋值:

虽然问题我找到了,也谢谢你,你字多给你了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-19 20:20:26 | 显示全部楼层
循环链表吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-8 03:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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