【问题标题】:What does it mean to unravel a circular linked list?解开循环链表是什么意思?
【发布时间】:2020-04-28 15:44:46
【问题描述】:

我在查看 React 的 hooks 实现时遇到了this 评论。作为背景,内部钩子以链表的形式存储在每个组件的纤维对象上,根据此注释,每个钩子上的更新第一次存储为循环链表。

对于第一次更新,队列是一个循环链表,其中queue.last.next = queue.first。一旦第一次更新提交,baseUpdate 不再为空,我们就可以解开列表。

我的计算机科学知识不是很强,虽然我对链表有一点了解,但我从未遇到过“unravel”这个词。看看实现,我认为它只是意味着将其转换回常规链表是否正确?

【问题讨论】:

    标签: javascript reactjs data-structures linked-list frontend


    【解决方案1】:

    您所指的完整上下文是:

    // The last update in the entire queue
    const last = queue.last;
    // The last update that is part of the base state.
    const baseUpdate = hook.baseUpdate;
    const baseState = hook.baseState;
    
    // Find the first unprocessed update.
    let first;
    if (baseUpdate !== null) {
      if (last !== null) {
        // For the first update, the queue is a circular linked list where
        // `queue.last.next = queue.first`. Once the first update commits, and
        // the `baseUpdate` is no longer empty, we can unravel the list.
        last.next = null;
      }
      first = baseUpdate.next;
    } else {
      first = last !== null ? last.next : null;
    }
    

    你的想法是正确的; last.next = null; 通过将 last.next 引用设置为 null 来“解开”循环链表,将其变成线性链而不是循环。

    至于术语,我认为它不是特别常见,也没有通过一些网络搜索看到直接的先例,所以这可能是他们当场想出的东西,以随意描述代码的作用。

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2018-09-18
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多