无法扭转一个链表 [英] Unable to reverse a linked list

查看:95
本文介绍了无法扭转一个链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扭转一个链表,但是每当我执行下面的功能,我只得到了最后一个元素。例如,如果列表中包含11,12,13更早。执行该功能后,它仅包含13请指出这个错误在我的code


 无效reverselist()
{
    结构节点* A,* B,* C;
    一个= NULL;
    B = C =启动;    而(C!= NULL)
    {
        C = B->接下来,
        B->接下来=一;
        A = B;
        B = C;
    }
    启动= C;
}


解决方案

c是一个辅助指针。

 无效reverselist()
{
    结构节点* A,* B,* C;
    一个= NULL;
    B =启动;
    而(B!= NULL)
    {
        C = B->接下来
        B->接下来=一;
        A = B
        B = C
    }
    开始=一个;
}

I was trying to reverse a linked list, however whenever I execute the following function, I get only the last element. For example, if the list contained 11,12,13 earlier. After executing the function, it contains only 13. Kindly point out the bug in my code


void reverselist()
{
    struct node *a,*b,*c;
    a=NULL;
    b=c=start;

    while(c!=NULL)
    {
        c=b->next;
        b->next=a;
        a=b;
        b=c;
    }
    start=c;
}

解决方案

c is a helper pointer.

void reverselist()
{
    struct node *a, *b, *c;
    a=NULL;
    b=start;
    while(b!=NULL)
    {
        c=b->next
        b->next=a;
        a=b
        b=c
    }
    start=a;
}

这篇关于无法扭转一个链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆