简体   繁体   English

反向链接列表-C ++

[英]Reversing Linked List - C++

I wrote a function that should reverse a list. 我写了一个应该反转列表的函数。

So far, I can reverse only two items, but no more. 到目前为止,我只能撤消两项,但不能撤消更多。 I checked and double checked and still can't find the problem. 我检查并再次检查,仍然找不到问题。 I even used the Debugger to see the value of each pointer. 我什至使用调试器查看每个指针的值。 When running the debugger, I received the message: 运行调试器时,我收到以下消息:

An access violation (segmentation fault) raised in your program. 程序中出现访问冲突(段错误)。

This is my first assignment with linked lists so I am still learning. 这是我对链接列表的第一次分配,因此我仍在学习。

Here is the code I wrote in Dev-C++: 这是我在Dev-C ++中编写的代码:

List::ListNode *List::Reverse_List(ListNode *head)
{
    ListNode *cur = head;
    ListNode *forward = NULL;
    ListNode *previous = NULL;

    while (cur != NULL)
    {
        head = cur; //set the head to last node
        forward = head->next;  //save the next pointer in forward
        cur->next = previous;  //change next to previous
        previous = cur;
        cur = forward;

        cout << "cur= " << cur->item << endl; //this is just to display the current value of cur

        return head;
    }
}

Your code is close, it is returning early. 您的代码已经关闭,正在提早返回。

List::ListNode *List::Reverse_List(ListNode *head) 
{
    ListNode *cur = head;
    ListNode *forward = NULL;
    ListNode *previous = NULL;

    while (cur != NULL) {
        //There is no need to use head here, cur will suffice
        //head = cur; //set the head to last node
        forward = cur->next; //save the next pointer in forward

        cur->next = previous; //change next to previous
        previous = cur;
        cur = forward;

        cout << "cur= " << cur->item << endl; //this is just to display the current value of cur

        //don't return here you have only adjusted one node
        //return head;
    }

    //at this point cur is NULL, but previous still holds the correct node
    return previous;
}

Sorry for answering late and I am sure that you would have found the answer by now but yet it might be helpful for others. 对不起,我回答很晚,到现在为止,您一定会找到答案的,但对其他用户可能会有所帮助。 Answer is simply taking return statement (ie return head;) out of while loop will fix your problem. 答案就是简单地从while循环中取出return语句(即return head;)将解决您的问题。 Though there are ways you can avoid extra pointer and assignments to optimise your code. 尽管有一些方法可以避免额外的指针和分配来优化代码。

Everyone must have the same homework assignment today. 今天每个人都必须完成相同的作业。

I think it would be more helpful to these people to show them what happens to the state of the list while it is being reversed. 我认为这对向人们展示列表在被颠倒时会发生什么变化会更有用。 This should help them better than showing them code or code problems. 这应该比向他们显示代码或代码问题更好地帮助他们。

Here is what should happen (with the algorithm I would use) 这是应该发生的事情(使用我将使用的算法)

[] = head () = current [] =头()=当前

([1])->2->3->4, [2]->(1)->3->4, [3]->2->(1)->4, [4]->3->2->(1) done because current now doesn't have a new next ([1])-> 2-> 3-> 4,[2]->(1)-> 3-> 4,[3]-> 2->(1)-> 4,[4]-> 3-> 2->(1)已完成,因为当前当前没有新的下一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM