nowater1 2021-08-23 10:11 采纳率: 100%
浏览 48
已结题

【C语言】单向链表反转,运行结果不对

刚刚开始学习数据结构,自己尝试性地写了一个链表反转的代码,编译似乎没有问题,但运行后没有显示任何数字。
想了半天找不到原因。
请问有哪位能帮忙看看问题出在哪里吗?
十分感谢!

#include <stdio.h>
#include <stdlib.h>
struct Node{
    int data;
    struct Node* link;
};
struct Node* head = NULL;
void Print()
{
    struct Node* temp = head;
    while (temp != NULL)
    {
        printf(" %d", temp->data);
        temp= temp->link;
    }
    printf("\n");
}
int Insert(int n)
{
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
    temp = head;
    
    while(temp->link != NULL)
    {
        temp = temp->link;
    }
    struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node*));
    temp2->data = n;
    temp2->link = NULL;
    temp->link = temp2; 
    if(head == NULL)
    {
        head = temp;
    }


}
int Reverse()
{
    struct Node* current, *prev , *next;
    current = head;
    prev = NULL;
    while (current != NULL)
    {
        next = next->link;
        current->link = prev;
        prev = current;
        current = next;
    }
    head = prev;
    
}
int main()
{
    Insert(2);
    Insert(4);
    Insert(6);
    Insert(8);
    Print();
    Reverse();
    Print(); 
}

  • 写回答

3条回答 默认 最新

  • 浪客 2021-08-23 10:40
    关注
    #include <stdio.h>
    #include <stdlib.h>
    struct Node
    {
        int data;
        struct Node *link;
    };
    struct Node *head = NULL;
    
    void Print()
    {
        struct Node *temp = head->link; //struct Node *temp = head;
        while (temp != NULL)
        {
            printf(" %d", temp->data);
            temp = temp->link;
        }
        printf("\n");
    }
    void Insert(int n)
    {
        //struct Node *temp = (struct Node *)malloc(sizeof(struct Node *));
        //temp = head;
        // while (temp->link != NULL)
        // {
        //     temp = temp->link;
        // }
        struct Node *temp;
        temp = head;
        while (temp->link != NULL)
        {
            temp = temp->link;
        }
        struct Node *temp2 = (struct Node *)malloc(sizeof(struct Node *));
        temp2->data = n;
        temp2->link = NULL;
        temp->link = temp2;
        // if (head == NULL)
        // {
        //     head = temp;
        // }
    }
    
    void Reverse()
    {
        struct Node *current, *prev, *next;
        next = current = head->link; //current = head;
        prev = NULL;
        while (current != NULL)
        {
            next = next->link;
            current->link = prev;
            prev = current;
            current = next;
        }
        head->link = prev; //head = prev;
    }
    int main()
    {
        head = (struct Node *)calloc(1, sizeof(struct Node *));
        Insert(2);
        Insert(4);
        Insert(6);
        Insert(8);
        Print();
        Reverse();
        Print();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月17日
  • 已采纳回答 9月9日
  • 创建了问题 8月23日

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答