可视化解释反向链接列表数据结构代码所需的指导? [英] Visual Explanation Guidance needed for reversal of Linked List datastructure code?

查看:154
本文介绍了可视化解释反向链接列表数据结构代码所需的指导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来反转链表。我在while循环中感到困惑,所以如果有人可以提供其实际工作的视觉解释,那么肯定会感激。

  static void Reverse(struct node ** headRef)
{
struct node * result = NULL;
struct node * current = * headref;
struct node * next;

while(current!= NULL)
{
next = current-> next;
current-> next = result;
result = current;

current = next;
}
* headRef = result;

}


解决方案

这是我试图让valya的答案更清楚(虽然我以为这已经很好了):



说我们有这个列表:

  // a-> b-> c-> d-> e-> NULL 
/ pre>

我们从第一个节点开始, a ,其中包含一个指针( $ b b

  / a-> b ... 

next = current- < next; set next to b (够简单)。下一行 current-> next = result; 执行此操作:

  // NULL< -ab ...(注意不再有从a到b的指针)

然后我们有 result = current; result 设置为 a (再次简单)。最后,我们将所有重要的 current = next; 当前设置为 b



所以在while循环的下一个迭代中,将 next 设置为 b result 设置为 a 当前设置为 b ,我们重新开始:

  next = current-> next; 

// NULL< -a&-b@cb
current-> next = result;

result = current;

然后我们再次执行:

  next = current-> next; 

// NULL< -a< -b< -c d ...
current-> next = result;

result = current;

一旦我们得到了链表中的最后一个项目( e 在这个例子中),这发生:

  next = current-> next; // next变为NULL 

// NULL< -a< -b< -c< -d< -e
current-> next =

result = current; // result is now e

current = next; // current is now NULL

现在,因为当前为NULL,while循环终止,我们留下:

  * headRef =结果; 

你现在可以看到,使 headRef 指向 e ,将 e 作为我们的链接列表中的新的第一项, e-> next 指向 d d-> next 指向 c 等。


I have following piece of code for reversing the linked list. I am getting confused in while loop and so would certainly appreciate if someone can provide visual explanation of how actually it's working.

 static void Reverse (struct node** headRef)
{
     struct node* result = NULL;
     struct node* current = *headref;
     struct node* next;

     while(current != NULL)
     {
        next = current->next;
        current->next = result;
        result = current;

        current = next;
     }     
     *headRef = result;

}

解决方案

OK, here's my attempt to make valya's answer even clearer (though I thought it was pretty good already):

Say we have this list:

// a->b->c->d->e->NULL

We start at the first node, a, which contains a pointer (next) to b:

// a->b ...

The line next = current->next; sets next to b (simple enough). The next line current->next = result; does this:

// NULL<-a  b ... (notice there is no longer a pointer from a to b)

Then we have result = current; which sets result to a (again, simple enough). And finally, we have the all important current = next;, which set current to b.

So on the next iteration of the while loop, with next set to b, result set to a, and current set to b, we start over:

next = current->next;

// NULL<-a<-b  c ...
current->next = result;

result = current;

Then we do it again:

next = current->next;

// NULL<-a<-b<-c  d ...
current->next = result;

result = current;

Once we've gotten to the last item in the linked list (e in this example), this happens:

next = current->next; // next becomes NULL

// NULL<-a<-b<-c<-d<-e
current->next = result;

result = current; // result is now e

current = next; // current is now NULL

Now, since current is NULL, the while loop terminates, and we are left with:

*headRef = result;

which, as you can see now, makes headRef point to e, treating e as the new first item in our linked list, with e->next pointing to d, d->next pointing to c, etc.

这篇关于可视化解释反向链接列表数据结构代码所需的指导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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