为什么我的代码不能反转循环链表?

时间:2013-04-26 07:08:34

标签: c linked-list structure reverse

我正在尝试编写一个可以反转循环链接列表(1个标记,双链接)顺序的函数。以下是我的代码。原始列表是15,14,11,12。我希望新列表是12,11,14和15.但我一直得到15,14,11和12.有人可以查看我的代码并给出我有些暗示吗?谢谢!

 void reverseCirListDeque(struct cirListDeque *q)
{

    assert(q != 0);
    assert(!isEmptyCirListDeque(q));


    struct DLink *start = q->Sentinel->next;
    struct DLink *next;
    struct DLink *prev = NULL;

    while (start != NULL)
    {
        //Swap the next and previous link
        next = start->next;
        start->next = prev;
        prev = start;
        start = next;
     }     

}

1 个答案:

答案 0 :(得分:1)

next = start->next;
start->next = prev;
start->prev = next;//This line is missing
prev = start;
start = next;

它会错误地访问(或更新)变量,逻辑似乎没有问题。