实现单链表反转(C语言实现)

问题描述:

单链表反转就是把原单向链表顺序倒置。虽然这仅仅是链表操作中比较基础的一个,但是熟练写出单链表反转,对很多刚刚接触代码不足一年的同学来说仍然有一定难度。

这篇博客将使用 C语言简单实现这一操作,并与完整的输入输出功能一起实现,方便读者运行代码进行测试。


代码如下:

/**
 * (C) Copyright Bob Ding, Xidian University.  2021.
 */

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

/**
 * @brief inits the type of node.
 * @brief creates data to instore data.
 * @brief creates next to instore the address of other node.
 */
typedef struct Node
{
    int data;
    struct Node *next;
}node;

node *init_list();

node *reverse_list(node *head);

void display_list(node *head);

void free_list(node *head);

int main()
{
    node *head = init_list();

    reverse_list(head);
     
    display_list(head);

    free_list(head);

    return 0;
}
    
/**
 * @brief  inits the linklist.
 * @brief  creates head node without data.
 * @brief  creates cur node in order to traverse the list.
 * @brief  inserts N numbers into the list.
 * @note   notices that head is a node without data.
 * @param
 * @return head – the first node of this list
 */
node *init_list()
{
    node *head = (node *)malloc(sizeof(node));
    
    node *cur = head;
    
    int N;
    
    printf("请先输入你要输入的数字个数:\n");
    
    scanf("%d", &N);
    
    if (N <= 0)
        return NULL;
    
    for (int i = 0; i < N; i++)
    {
        node *a = (node *)malloc(sizeof(node));
        scanf("%d", &(a -> data));
        a -> next = NULL;
        cur -> next = a;
        cur = cur -> next;
    }
    
    return head;
}

/**
 * @brief displays this list.
 * @param head – the first node of this list
 * @return
 */
void display_list(node *head)
{
    node *cur = (node *)malloc(sizeof(node));
    
    cur = head;
    
    while ((cur = cur -> next))
    {
        printf("%d ", cur -> data);
    }
    
    free(cur);
    
    printf("\n");
}

/**
 * @brief  reverses this list.
 * @note   notices that head is a node without data.
 * @param  head – the first node of the list
 * @return pre – the new first node of the reversed list
 */
node *reverse_list(node *head)
{
    node *cur = NULL;
    
    node *next = NULL;
    
    cur = head -> next;
    
    next = head -> next -> next;
    
    while (next != NULL)
    {
        cur -> next = next -> next;
        /* Removes the next node from this list. */
        next -> next = head -> next;
        /* Moves the next node before the cur node in this list. */
        head -> next = next;
        next = cur -> next;
    }
    
    return head;
}

/**
 * @brief creates free_list function to free all space of the list.
 * @note  head is a node without data.
 * @param head – the first node of this list
 * @return
 */
void free_list(node *head)
{
    if (head -> next == NULL)
        return;
    
    node *cur = head;
    
    while ((cur = cur -> next))
    {
        head -> next = cur -> next;
        free(cur);
        cur = head;
    }
    
    free(head);
}


思路

单链表反转的代码在reverse_list()函数里,主要利用的指针操作。这个函数先传入一个自己定义的链表节点类型的指针变量head,这是一个存储了链表第一个节点地址的变量。请读者仔细阅读init_list()函数的实现。实际上这个链表设计了一个空的头节点,也就是说,链表的第一个节点里虽然存有下一个节点的地址,但是不存数据。

然后我在reverse_list()函数里定义了cur "current"还有next 两个节点类型的指针变量。用cur去遍历链表的各个节点,用next存储每个cur节点连接的下一个节点的地址。

要想反转,就是要把前一个节点变成下一个节点,把下一个节点变成前一个节点。因为头节点数据为空,所以数据实际上是从第二个节点开始存储。我先让第二个节点指向第三个节点的指针指到第四个节点。这样就把第三个节点从链表中移出来了。我们接着把原来第三个节点指向下一个节点的指针指到第二个节点。最后把头节点指向原第二个节点的指针指到原第三个节点,这样就完成了把第三个节点移到第二个节点前到操作,一直移动到最后一个节点就能完成反转。

h e a d → 2 → 3 → 4 → . . . → t a i l head \rightarrow 2\rightarrow3 \rightarrow4 \rightarrow... \rightarrow tail head234...tail

第一次移动节点

h e a d → 3 → 2 → 4 → . . . → t a i l head \rightarrow 3\rightarrow2 \rightarrow4\rightarrow... \rightarrow tail head324...tail

第二次移动节点

h e a d → 4 → 3 → 2 → . . . → t a i l head \rightarrow 4\rightarrow3 \rightarrow2\rightarrow... \rightarrow tail head432...tail

直至遍历到最后一个节点

h e a d → t a i l → . . . → 4 → 3 → 2 head \rightarrow tail \rightarrow... \rightarrow 4\rightarrow 3\rightarrow 2 headtail...432

这样便顺利完成了反转

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盛世危言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值