Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

链表操作的图是不是画错了 #118

Open
xincmm opened this issue Dec 17, 2023 · 1 comment
Open

链表操作的图是不是画错了 #118

xincmm opened this issue Dec 17, 2023 · 1 comment

Comments

@xincmm
Copy link

xincmm commented Dec 17, 2023

image 正确的画法应该是: image

源码和注释:

const sharedQueue: SharedQueue<State> = (updateQueue: any).shared;
  const pending = sharedQueue.pending;
  if (pending === null) { // 若链表中没有元素,则创建单向环状链表,next指向它自己
     update.next = update;
   } else {
     // 有元素,现有队列(pending)指向的是链表的尾部update,
     // pending.next就是头部update,新update会放到现有队列的最后并首尾相连
     // 将新队列的尾部(新插入的update)的next指向队列的首部,实现首位相连
     update.next = pending.next; 
    // 现有队列的最后一个元素的next指向新来的update,实现把新update接到现有队列上
     pending.next = update;
   } 
  // 现有队列的指针总是指向最后一个update,可以通过最后一个寻找出整条链表
  sharedQueue.pending = update;
@xincmm
Copy link
Author

xincmm commented Dec 18, 2023

抱歉,我重新看了一下,你的画法是把 update 1 作为头节点,我下意识地以为最右边的 update 2 才是头节点。如果能在 update 1 下面标出是头节点的话就不会产生误解了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant