如何更新循环链表中的后部?
如果我在初始后部之后移动一组节点,我需要帮助更新循环链表的后部。
假设rear是后节点,rear.next循环回到[1]
。
[1][2][3][4][5]
<-------------/
如果我移动 [1][2][3]
节点 [5]
后给我
[4][5][ 1][2][3]
,它打破了循环链表,
如何将[3]
更新为rear和rear.next以指向[1] ?
I need help updating the rear of a circular linked list if I move a set of nodes after the initial rear.
Lets assume rear is the rear node and rear.next circulates back to [1]
.
[1][2][3][4][5]
<-------------/
If I move [1][2][3]
after node [5]
giving me
[4][5][1][2][3]
, which breaks the circular linked list,
How can I approach updating [3]
as rear and rear.next to point to [1]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于链接集的性质是圆形,因此您必须断开链接并在移动后重新建立它们。这里重要的链接是那些将被“破坏”的链接,即(如果它是一个单链表,正如我假设的那样),来自
[3] -> 的链接。 [4]
以及来自[5] -> 的链接[1]
。您的列表中有两个部分,
[1][2][3]
(我们可以称之为A
)和[4][5]< /code>,我们可以称之为
B
。下面显示的-->
链接是指向列表中第一个元素的指针。您想要做的是重置链接,以便
A
中的最后一个元素指向B
中的第一个元素,以及B
中的最后一个元素> 指向A
中的第一个元素。另外,列表的开头现在是 B 中的第一个元素。所以现在我们只需在需要的地方断开并重置链接即可。
B
的第一个元素,即[4]
B
的最后一个元素,即 < code>[5],指向A
中的第一个元素,即[1]
。碰巧已经这样设置了,但不要指望这一点。 :)A
中的最后一个元素(即[3]
)设置为指向B
中的第一个元素(即) [4]
。正如您所看到的,我们真正关心的唯一节点是列表每个部分的第一个和最后一个元素,即
A
和B
的开始和结束元素。在这种情况下,其中一个部分也包含第一个元素,因此我们必须移动哪个元素是“第一个”元素的概念。希望这有帮助。Because the nature of the set of links is a circle, you have to break the links and reestablish them after moving. The important links here are the ones that are going to be 'broken', which is (if it is a singly linked list, as I am assuming), the link from
[3] -> [4]
and the link from[5] -> [1]
.You have two sections in your list here,
[1][2][3]
, which we can callA
, and[4][5]
, which we can callB
. The-->
link shown below is the pointer to the first element in the list.What you want to do is reset the link so that the last element in
A
points to the first element inB
, and the last element inB
points to the first element inA
. In addition, the beginning of the list is now the first element in B.So now we just break and reset the links where they are needed.
B
, which is[4]
B
, which is[5]
, to point to the first element inA
, which is[1]
. This happens to already be set that way, but don't count on that. :)A
, which is[3]
, to point to the first element inB
, which is[4]
.As you can see, the only nodes we really care about are the first and last elements of each part of the list, i.e., the beginning and ending element of
A
andB
. In this case, one of those sections also containd the first element, so we had to move the notion of which element was the 'first' element. Hope this helps.这是作业吗?
你的“举动”做得怎么样?
我会将其设为一个函数,例如
list.move(first, last, after)
,在本例中为list.move(1, 3, 5)
。并且您将有变量跟踪列表的前部/头部和后部/尾部。我假设它们被称为
list.front
和list.rear
。所以在每种情况下,你都想做:
然后我可以看到两种特殊情况:
(
list[after] == list.rear
)(
list[start] == list.front
)因此您可以使用
if
语句来处理这些。Is this homework?
How are you doing the "move"?
I would make it a function, something like
list.move(first, last, after)
, which in this case would belist.move(1, 3, 5)
.And you'd have variables tracking the front/head and the rear/tail of the list. I'll assume they're called
list.front
andlist.rear
.So in every case, you want to do:
and then there's two special cases I can see:
(
list[after] == list.rear
)(
list[start] == list.front
)So you can handle those using
if
statements.我认为这回答了它(假设我理解正确)
I think this answers it(provided i understand this correctly)
在以 [1][2][3][4][5] 开始并且希望以 [4][5][1][2][3] 结束的基础上进行工作,您可以使用指针并不会改变数据结构。
正如您所指出的,我们有一个循环列表,其中指针“rear.next”指向第一个存储桶,每个后续存储桶都有一个 .next 指针将其链接到其最右边的邻居,直到我们再次回到后存储桶。基本上是一堆彼此相连的桶。
如果我们创建一个“尾”节点和一个“头”节点,我们可以使用它们来对列表进行排序。这些是标记,不是圆圈的一部分,它们只是指向起点或终点。
因此,要获得 [1][2][3][4][5],我们可以将尾节点设置为指向 [5],然后跟随 [5] 的下一个指针,该指针引导我们到达我们设置标头的 [1]指向。所以 header=[1];tail=[5]
从 header 开始,每次我们都可以按这个顺序访问我们的项目时,按照 next 指针遍历列表 [1][2][3][4][5] 。这并不奇怪!
让我们改变一下,因为我们希望 [3] 位于后面。因此,让我们将尾节点设置为指向 [3]。然后沿着[3]的下一个指针我们到达[4]。为此,我们分配 header.So header=[4];tail=[3]
现在从我们的 header 指向的任何内容开始处理我们的列表,我们有 [4][5][1][2][3]。
看起来我们已经执行了移动操作,但我们没有篡改我们的数据结构或其链接 - 只是我们使用它的方式。我们所改变的只是头部和尾部。 (我们的指点)。这就是循环列表的美妙之处。
Working on the basis that you start with [1][2][3][4][5] and you want to end up with [4][5][1][2][3] you can do this using pointers and not change the data structure.
As you have indicated we have a circular list where the pointer "rear.next" points to the first bucket and each subsequent bucket has a .next pointer linking it to its right most neighbour until we get back to the rear bucket again. Basically a bunch of buckets linked to one another.
If we create a "tail" node and a "header" node we can use these to order the list. These are markers and not part of the circle they just point to a start or end point.
So to get [1][2][3][4][5] we can set the tail node to point to [5] then follow [5]s next pointer which leads us to [1] which we set our header to point to. So header=[1];tail=[5]
Starting at the header and working through the list following the next pointers each time we can access our items in this order [1][2][3][4][5]. No surprise there!
Lets change that though as we want [3] to be the rear. So lets set our tail node to point to [3]. Then follow [3]s next pointer we arrive at [4]. To this we assign the header.So header=[4];tail=[3]
Now working through our list starting with whatever our header points to we have [4][5][1][2][3].
It appears we have performed a move operation but we have not tampered with our data structure or its links - just the way we work with it. All we have altered is our header and tail. (Our pointers). Thats the beauty of the circular list.