liyifeng295641 发表于 2024-4-19 11:18:49

C语言 单链表03

https://xxx.ilovefishc.com/forum/202112/22/054059qb3sh34dw9vaxlas.png


#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

FishC 发表于 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 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: C语言 单链表03