鱼C论坛

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

[已解决]C语言 单链表03

[复制链接]
发表于 2024-4-19 11:18:49 | 显示全部楼层 |阅读模式

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

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

x

                               
登录/注册后可看大图


#include <stdio.h>
#include <stdlib.h>

struct Dode
{
        int value;
        struct Dode *next;
};

void insertDode(struct Dode **head, int value)
{
        struct Dode *previous;
        struct Dode *current;
        struct Dode *new;

        previous = NULL;
        current = *head;
        
        while(current != NULL && current -> value < value)
        {
                previous = current;
                current = current -> next;
        }

        new = (struct Dode *)malloc(sizeof(struct Dode));

        if(new == NULL)
        {
                printf("内存分配失败!");
                exit(1);
        }

        new -> value = value;
        new -> next = current;

        if(previous == NULL)
        {
                *head = new;
        }
        else
        {
                previous -> next = new;
        }
}

void printDode(struct Dode *head)
{
        struct Dode *current;

        current = head;
        while(current != NULL)
        {
                printf("%d ", current -> value);
                current = current -> next;
        }
        putchar('\n');
}

void turnDode(struct Dode **head, int value)
{
        struct Dode *current;
        static struct Dode *tail;

        current = *head;                
        if(*head != NULL)
        {
                tail -> next = current;
                current -> next = NULL;
        }
        else
        {
                *head = current;
                current -> next = NULL;
        }

        tail = current;

        while(tail != NULL)
        {
                printf("%d ", tail -> value);
                tail = tail -> next;
        }
}

int main(void)
{
        struct Dode *head = NULL;
        int input;
        
        printf("开始录入数据到单链表a...\n");

        while(1)
        {
                printf("请输入一个整数(输入-1表示结束):");
                scanf("%d", &input);
                if(input == -1)
                {
                        break;
                }
                insertDode(&head, input);
                printDode(head);
        }
        putchar('\n');
        printf("下面将单链表a原地反转...\n");
        turnDode(&head, input);
        printDode(head);

        return 0;
}

我写的哪里出问题了?
开始录入数据到单链表a...
请输入一个整数(输入-1表示结束):1
1
请输入一个整数(输入-1表示结束):2
1 2
请输入一个整数(输入-1表示结束):3
1 2 3
请输入一个整数(输入-1表示结束):-1

下面将单链表a原地反转...
zsh: bus error  ./a.out
最佳答案
2024-4-19 11:19:04
问题出在turnDode函数中的逻辑错误。在反转链表时,应该遍历整个链表,将每个节点的next指针指向其上一个节点,最后将头结点指向最后一个节点。但是你的代码只是将每个节点的next指针都指向了第一个节点,造成了循环链表。

以下是修正后的代码:
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int value;
    struct Node *next;
};

void insertNode(struct Node **head, int value)
{
    struct Node *previous;
    struct Node *current;
    struct Node *newNode;

    previous = NULL;
    current = *head;

    while (current != NULL && current->value < value)
    {
        previous = current;
        current = current->next;
    }

    newNode = (struct Node *)malloc(sizeof(struct Node));

    if (newNode == NULL)
    {
        printf("内存分配失败!\n");
        exit(1);
    }

    newNode->value = value;
    newNode->next = current;

    if (previous == NULL)
    {
        *head = newNode;
    }
    else
    {
        previous->next = newNode;
    }
}

void printList(struct Node *head)
{
    struct Node *current = head;

    while (current != NULL)
    {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n");
}

void reverseList(struct Node** head)
{
    struct Node* previous = NULL;
    struct Node* current = *head;
    struct Node* next = NULL;

    while (current != NULL) {
        // 保存下一个节点
        next = current->next;
      
        // 反转节点的指针
        current->next = previous;
      
        // 更新指针位置
        previous = current;
        current = next;
    }
  
    // 更新头指针
    *head = previous;
}

int main(void)
{
    struct Node *head = NULL;
    int input;

    printf("开始录入数据到单链表...\n");

    while (1)
    {
        printf("请输入一个整数(输入-1表示结束):");
        scanf("%d", &input);
        if (input == -1)
        {
            break;
        }
        insertNode(&head, input);
        printList(head);
    }
    printf("\n");

    printf("下面将单链表反转...\n");
    reverseList(&head);
    printList(head);

    return 0;
}

这样就可以正确地实现单链表的插入和反转功能了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-19 11:19:04 | 显示全部楼层    本楼为最佳答案   
问题出在turnDode函数中的逻辑错误。在反转链表时,应该遍历整个链表,将每个节点的next指针指向其上一个节点,最后将头结点指向最后一个节点。但是你的代码只是将每个节点的next指针都指向了第一个节点,造成了循环链表。

以下是修正后的代码:
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int value;
    struct Node *next;
};

void insertNode(struct Node **head, int value)
{
    struct Node *previous;
    struct Node *current;
    struct Node *newNode;

    previous = NULL;
    current = *head;

    while (current != NULL && current->value < value)
    {
        previous = current;
        current = current->next;
    }

    newNode = (struct Node *)malloc(sizeof(struct Node));

    if (newNode == NULL)
    {
        printf("内存分配失败!\n");
        exit(1);
    }

    newNode->value = value;
    newNode->next = current;

    if (previous == NULL)
    {
        *head = newNode;
    }
    else
    {
        previous->next = newNode;
    }
}

void printList(struct Node *head)
{
    struct Node *current = head;

    while (current != NULL)
    {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n");
}

void reverseList(struct Node** head)
{
    struct Node* previous = NULL;
    struct Node* current = *head;
    struct Node* next = NULL;

    while (current != NULL) {
        // 保存下一个节点
        next = current->next;
      
        // 反转节点的指针
        current->next = previous;
      
        // 更新指针位置
        previous = current;
        current = next;
    }
  
    // 更新头指针
    *head = previous;
}

int main(void)
{
    struct Node *head = NULL;
    int input;

    printf("开始录入数据到单链表...\n");

    while (1)
    {
        printf("请输入一个整数(输入-1表示结束):");
        scanf("%d", &input);
        if (input == -1)
        {
            break;
        }
        insertNode(&head, input);
        printList(head);
    }
    printf("\n");

    printf("下面将单链表反转...\n");
    reverseList(&head);
    printList(head);

    return 0;
}

这样就可以正确地实现单链表的插入和反转功能了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-18 19:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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