关于使用数组表示约瑟夫斯难题

发布于 2024-10-04 18:19:32 字数 292 浏览 5 评论 0原文

Robert Sedwick 的算法,有人提到链表可以使用数组表示,在以下链接

http://flylib.com/books/en/3.55.1.34/1/

图 3.8,如果根据我的理解删除 5,那么接下来的 4 应该更改为索引 6,因为 val 5 被删除,我们继续虽然第 4 项的数字被删除,但接下来第 3 项的数字被更改。我没有遵循该图的逻辑。任何人都可以帮助我吗?

谢谢!

Algorithms by Robert Sedwick, It was mentioned that linked list can be represented using Arrays, at following link

http://flylib.com/books/en/3.55.1.34/1/

Fig 3.8, here if 5 is removed from my understanding next 4 should be changed to index 6 as val 5 is removed, as we go thourgh the figure at item 4 is removed next of val 3 is chaned. I am not following the logic of the figure. can aany one please help me.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平生欢 2024-10-11 18:19:33

索引是从零开始的,而不是值本身(字母是更好的值)。
移除值5示例:移除前,值为4的节点的下一个索引为4,指向值5;删除后,下一个索引更改为 5,指向值 6(下一个从 4 更改为 5)。

或者,使用前缀 v 来指示值:

before

    index ...  3  4  5 ...
    ----------------------
    value     v4 v5 v6
    next       4  5  6

after

    index ...  3  4  5 ...
    ----------------------
    value     v4 v5 v6
    next       5  5  6  

,您可以看到节点 v4 后面是v6(索引 5)实际上从链中删除了 v5

the index is zero-based as opposed to the value itself (letters would be better values).
Example of removing the value 5: before removing, next index of the node with value 4 is 4, which points to value 5; after removing, next index is changed to 5, pointing to value 6 (next changed from 4 to 5).

Or, using the prefix v to indicate values:

before

    index ...  3  4  5 ...
    ----------------------
    value     v4 v5 v6
    next       4  5  6

after

    index ...  3  4  5 ...
    ----------------------
    value     v4 v5 v6
    next       5  5  6  

as you can see the node v4 is followed by v6 (index 5) practically removing v5 from the chain.

无声无音无过去 2024-10-11 18:19:33

next 数组中的值是链中下一个值的索引。如果要删除位于索引 4 处的值 5,则必须将 next[3] 从 4 更改为 5,以便有效地将索引 4 处的值从圆中删除,并且将在以后的遍历中被跳过。

约瑟夫斯数组实现

更新高亮后,值为4的“节点”指向值为6的“节点”。

The values in the next array are the index of the next value in the chain. If you want to remove the value 5, which sits at index 4, you have to change next[3] from 4 to 5, so that the value at index 4 is effectively removed from the circle and will be skipped on later traversals.

Josephus array implementation

After the update highlighted, the "node" with a value of 4 points to the "node" with a value of 6.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文