javascript - 解开循环链表意味着什么?

标签 javascript reactjs data-structures linked-list frontend

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

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.

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

最佳答案

您所指的完整上下文是:

// 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 来“解开”循环链表,将其转换为线性链表链而不是循环。

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

关于javascript - 解开循环链表意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691035/

相关文章:

java - 使用大集合或带有集合的等效 map 好吗?

xhtml - Nokogiri 替换标签值

c++ - 这会在哪些平台上崩溃,我该如何改进它?

javascript - "this"关键字在函数中如何工作?

javascript - 替换 iframe 中的顶部文档而不更改当前 URL

javascript - 如何使用 React useState hook 防止竞争条件

javascript - React,无法访问在 useEffect() 中传递给 setInterval() 的函数内状态变量的更新值

javascript - 在 Angular JS 脚本中读取 JSON 数据并仅在 JS 中进一步使用该数据

javascript - 当用户手动滚动时,Jquery .animate() 停止滚动?

javascript - 如何从 React Native 一次性导入所有组件?