链表 (双向)

    要创建一个双向链表,你需要做的就是导入和创建它的一个新的实例:

    MVC:

    构造函数没有任何参数.

    有几种方法可以在链表中创建新节点,这些方法都可以单独使用,也可以通过 addaddMany 方法.

    addHead(value)

    1. addHead(value: T): ListNode<T>

    将给定值添加到链表的第一个节点:

    1. list.addHead('a');
    2. // "a"
    3. list.addHead('b');
    4. // "b" <-> "a"
    5. list.addHead('c');
    6. // "c" <-> "b" <-> "a"

    addManyHead(values)

    1. addManyHead(values: T[]): ListNode<T>[]

    将给定的多个值添加到链表的第一个节点:

    1. list.addManyHead(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. list.addManyHead(['x', 'y', 'z']);
    4. // "x" <-> "y" <-> "z" <-> "a" <-> "b" <-> "c"

    addTail(value)

    1. addTail(value: T): ListNode<T>

    将给定值添加到链表的最后一个节点:

    1. list.addTail('a');
    2. // "a"
    3. list.addTail('b');
    4. // "a" <-> "b"
    5. list.addTail('c');
    6. // "a" <-> "b" <-> "c"

    addManyTail(values)

    1. addManyTail(values: T[]): ListNode<T>[]

    将给定多个值添加到链表的最后一个节点:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. list.addManyTail(['x', 'y', 'z']);
    4. // "a" <-> "b" <-> "c" <-> "x" <-> "y" <-> "z"

    addAfter(value, previousValue [, compareFn])

    1. addAfter(value: T, previousValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>

    添加给定值到previousValue节点后:

    1. list.addTail('a');
    2. list.addTail('b');
    3. list.addTail('b');
    4. list.addTail('c');
    5. // "a" <-> "b" <-> "b" <-> "c"
    6. list.addAfter('x', 'b');
    7. // "a" <-> "b" <-> "x" <-> "b" <-> "c"

    你可以自定义比较器:

    1. list.addTail({ x: 1 });
    2. list.addTail({ x: 2 });
    3. list.addTail({ x: 3 });
    4. // {"x":1} <-> {"x":2} <-> {"x":3}
    5. list.addAfter(
    6. { x: 0 },
    7. 2,
    8. (value, searchedValue) => value.x === searchedValue
    9. );
    10. // {"x":1} <-> {"x":2} <-> {"x":0} <-> {"x":3}

    addManyAfter(values, previousValue [, compareFn])

    1. addManyAfter(values: T[], previousValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>[]

    添加给定的多个值到previousValue节点后:

    1. list.addManyTail(['a', 'b', 'b', 'c']);
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. list.addManyAfter(['x', 'y'], 'b');
    4. // "a" <-> "b" <-> "x" <-> "y" <-> "b" <-> "c"

    你可以自定义比较器:

    1. list.addManyTail([{ x: 1 },{ x: 2 },{ x: 3 }]);
    2. // {"x":1} <-> {"x":2} <-> {"x":3}
    3. list.addManyAfter(
    4. [{ x: 4 }, { x: 5 }],
    5. 2,
    6. (value, searchedValue) => value.x === searchedValue
    7. );
    8. // {"x":1} <-> {"x":2} <-> {"x":4} <-> {"x":5} <-> {"x":3}

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    addBefore(value, nextValue [, compareFn])

    1. addBefore(value: T, nextValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>

    添加给值到previousValue节点前:

    1. list.addTail('a');
    2. list.addTail('b');
    3. list.addTail('b');
    4. list.addTail('c');
    5. // "a" <-> "b" <-> "b" <-> "c"
    6. list.addBefore('x', 'b');
    7. // "a" <-> "x" <-> "b" <-> "b" <-> "c"

    你可以自定义比较器:

    1. list.addTail({ x: 1 });
    2. list.addTail({ x: 2 });
    3. list.addTail({ x: 3 });
    4. // {"x":1} <-> {"x":2} <-> {"x":3}
    5. list.addBefore(
    6. { x: 0 },
    7. 2,
    8. (value, searchedValue) => value.x === searchedValue
    9. );
    10. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":3}

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    addManyBefore(values, nextValue [, compareFn])

    1. addManyBefore(values: T[], nextValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>[]

    添加给定的多个值到previousValue节点前:

    1. list.addManyTail(['a', 'b', 'b', 'c']);
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. list.addManyBefore(['x', 'y'], 'b');
    4. // "a" <-> "x" <-> "y" <-> "b" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.addManyTail([{ x: 1 },{ x: 2 },{ x: 3 }]);
    2. // {"x":1} <-> {"x":2} <-> {"x":3}
    3. list.addManyBefore(
    4. [{ x: 4 }, { x: 5 }],
    5. 2,
    6. (value, searchedValue) => value.x === searchedValue
    7. );
    8. // {"x":1} <-> {"x":4} <-> {"x":5} <-> {"x":2} <-> {"x":3}

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    addByIndex(value, position)

    1. addByIndex(value: T, position: number): ListNode<T>

    在链表的指定位置添加节点:

    1. list.addTail('a');
    2. list.addTail('b');
    3. list.addTail('c');
    4. // "a" <-> "b" <-> "c"
    5. list.addByIndex('x', 2);
    6. // "a" <-> "b" <-> "x" <-> "c"

    它也适用于负索引:

    1. list.addTail('a');
    2. list.addTail('b');
    3. list.addTail('c');
    4. // "a" <-> "b" <-> "c"
    5. list.addByIndex('x', -1);
    6. // "a" <-> "b" <-> "x" <-> "c"

    addManyByIndex(values, position)

    1. addManyByIndex(values: T[], position: number): ListNode<T>[]

    添加多个节点到链表的指定位置:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. list.addManyByIndex(['x', 'y'], 2);
    4. // "a" <-> "b" <-> "x" <-> "y" <-> "c"

    它也适用于负索引:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. list.addManyByIndex(['x', 'y'], -1);
    4. // "a" <-> "b" <-> "x" <-> "y" <-> "c"

    add(value).head()

    1. add(value: T).head(): ListNode<T>

    将添加的节点移动到链表头:

    1. list.add('a').head();
    2. // "a"
    3. list.add('b').head();
    4. // "b" <-> "a"
    5. list.add('c').head();
    6. // "c" <-> "b" <-> "a"

    它是 addHead 的替代API.

    add(value).tail()

    1. add(value: T).tail(): ListNode<T>

    将添加的节点移动到链表尾:

    1. list.add('a').tail();
    2. // "a"
    3. list.add('b').tail();
    4. // "a" <-> "b"
    5. list.add('c').tail();
    6. // "a" <-> "b" <-> "c"

    它是 addTail 的替代API.

    add(value).after(previousValue [, compareFn])

    1. add(value: T).after(previousValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>

    将添加的节点移动到指定节点后:

    1. list.add('a').tail();
    2. list.add('b').tail();
    3. list.add('b').tail();
    4. list.add('c').tail();
    5. // "a" <-> "b" <-> "b" <-> "c"
    6. list.add('x').after('b');
    7. // "a" <-> "b" <-> "x" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.add({ x: 1 }).tail();
    2. list.add({ x: 2 }).tail();
    3. list.add({ x: 3 }).tail();
    4. // {"x":1} <-> {"x":2} <-> {"x":3}
    5. list
    6. .add({ x: 0 })
    7. .after(2, (value, searchedValue) => value.x === searchedValue);
    8. // {"x":1} <-> {"x":2} <-> {"x":0} <-> {"x":3}

    它是 addAfter 的替代API.

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    add(value).before(nextValue [, compareFn])

    1. add(value: T).before(nextValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>

    将添加的节点移动到指定节点前:

    1. list.add('a').tail();
    2. list.add('b').tail();
    3. list.add('b').tail();
    4. list.add('c').tail();
    5. // "a" <-> "b" <-> "b" <-> "c"
    6. list.add('x').before('b');
    7. // "a" <-> "x" <-> "b" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.add({ x: 1 }).tail();
    2. list.add({ x: 2 }).tail();
    3. list.add({ x: 3 }).tail();
    4. list
    5. .add({ x: 0 })
    6. .before(2, (value, searchedValue) => value.x === searchedValue);
    7. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":3}

    它是 addBefore 的替代API.

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    add(value).byIndex(position)

    1. add(value: T).byIndex(position: number): ListNode<T>
    1. list.add('a').tail();
    2. list.add('b').tail();
    3. list.add('c').tail();
    4. // "a" <-> "b" <-> "c"
    5. list.add('x').byIndex(2);
    6. // "a" <-> "b" <-> "x" <-> "c"

    它也适用于负索引:

    addMany(values).head()

    1. addMany(values: T[]).head(): ListNode<T>[]

    将添加的多个节点移动到链表头:

    1. list.addMany(['a', 'b', 'c']).head();
    2. // "a" <-> "b" <-> "c"
    3. list.addMany(['x', 'y', 'z']).head();
    4. // "x" <-> "y" <-> "z" <-> "a" <-> "b" <-> "c"

    它是 addManyHead 的替代API.

    addMany(values).tail()

    1. addMany(values: T[]).tail(): ListNode<T>[]

    将添加的多个节点移动到链表尾:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.addMany(['x', 'y', 'z']).tail();
    4. // "a" <-> "b" <-> "c" <-> "x" <-> "y" <-> "z"

    它是 addManyTail 的替代API.

    addMany(values).after(previousValue [, compareFn])

    1. addMany(values: T[]).after(previousValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>[]

    将添加的多个节点移动到指定节点后:

    1. list.addMany(['a', 'b', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. list.addMany(['x', 'y']).after('b');
    4. // "a" <-> "b" <-> "x" <-> "y" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.addMany([{ x: 1 }, { x: 2 }, { x: 3 }]).tail();
    2. // {"x":1} <-> {"x":2} <-> {"x":3}
    3. list
    4. .addMany([{ x: 4 }, { x: 5 }])
    5. .after(2, (value, searchedValue) => value.x === searchedValue);
    6. // {"x":1} <-> {"x":2} <-> {"x":4} <-> {"x":5} <-> {"x":3}

    它是 addManyAfter 的替代API.

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    addMany(values).before(nextValue [, compareFn])

    1. addMany(values: T[]).before(nextValue: T, compareFn?: ListComparisonFn<T>): ListNode<T>[]

    将添加的多个节点移动到指定节点前:

    1. list.addMany(['a', 'b', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. list.addMany(['x', 'y']).before('b');
    4. // "a" <-> "x" <-> "y" <-> "b" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.addMany([{ x: 1 }, { x: 2 }, { x: 3 }]).tail();
    2. // {"x":1} <-> {"x":2} <-> {"x":3}
    3. list
    4. .addMany([{ x: 4 }, { x: 5 }])
    5. .before(2, (value, searchedValue) => value.x === searchedValue);
    6. // {"x":1} <-> {"x":4} <-> {"x":5} <-> {"x":2} <-> {"x":3}

    它是 addManyBefore 的替代API.

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    addMany(values).byIndex(position)

    1. addMany(values: T[]).byIndex(position: number): ListNode<T>[]

    将添加的多个节点移动到链表的指定位置:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.addMany(['x', 'y']).byIndex(2);
    4. // "a" <-> "b" <-> "x" <-> "y" <-> "c"

    它也适用于负索引:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.addMany(['x', 'y']).byIndex(-1);
    4. // "a" <-> "b" <-> "x" <-> "y" <-> "c"

    它是 addManyByIndex 的替代API.

    如何删除节点

    有几种方法可以在链表中删除节点,这些方法都可以单独使用,也可以通过 drop 方法.

    dropHead()

    1. dropHead(): ListNode<T> | undefined

    删除链表的第一个节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropHead();
    4. // "b" <-> "c"

    dropManyHead(count)

    1. dropManyHead(count: number): ListNode<T>[]

    删除指定数量的链表的头节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropManyHead(2);
    4. // "c"

    dropTail()

    1. dropTail(): ListNode<T> | undefined

    删除链表的最后一个节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropTail();
    4. // "a" <-> "b"

    dropManyTail(count)

    1. dropManyTail(count: number): ListNode<T>[]

    删除指定数量的链表的尾节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropManyTail(2);
    4. // "a"

    dropByIndex(position)

    1. dropByIndex(position: number): ListNode<T> | undefined

    删除链表中给定位置的节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropByIndex(1);
    4. // "a" <-> "c"

    它也适用于负索引:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropByIndex(-2);
    4. // "a" <-> "c"

    dropManyByIndex(count, position)

    1. dropManyByIndex(count: number, position: number): ListNode<T>[]

    删除链表中给定位置与数量的多个节点:

    1. list.addMany(['a', 'b', 'c', 'd']).tail();
    2. // "a" <-> "b" <-> "c" <-> "d
    3. list.dropManyByIndex(2, 1);
    4. // "a" <-> "d"

    它也适用于负索引:

    1. list.addMany(['a', 'b', 'c', 'd']).tail();
    2. // "a" <-> "b" <-> "c" <-> "d
    3. list.dropManyByIndex(2, -2);
    4. // "a" <-> "d"

    dropByValue(value [, compareFn])

    1. dropByValue(value: T, compareFn?: ListComparisonFn<T>): ListNode<T> | undefined

    删除链表中含有给定值的第一个节点:

    1. list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
    2. // "a" <-> "x" <-> "b" <-> "x" <-> "c"
    3. list.dropByValue('x');
    4. // "a" <-> "b" <-> "x" <-> "c"

    你可以自定义比较器

    1. list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
    2. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
    3. list.dropByValue(0, (value, searchedValue) => value.x === searchedValue);
    4. // {"x":1} <-> {"x":2} <-> {"x":0} <-> {"x":3}

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    dropByValueAll(value [, compareFn])

    1. dropByValueAll(value: T, compareFn?: ListComparisonFn<T>): ListNode<T>[]

    删除链表中含有给定值的所有节点:

    1. list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
    2. // "a" <-> "x" <-> "b" <-> "x" <-> "c"
    3. list.dropByValueAll('x');
    4. // "a" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
    2. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
    3. list.dropByValue(0, (value, searchedValue) => value.x === searchedValue);
    4. // {"x":1} <-> {"x":2} <-> {"x":3}

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    drop().head()

    1. drop().head(): ListNode<T> | undefined

    删除链表的头节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.drop().head();
    4. // "b" <-> "c"

    drop().tail()

    1. drop().tail(): ListNode<T> | undefined

    删除链表的尾节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.drop().tail();
    4. // "a" <-> "b"

    它是 dropTail 的替代API.

    drop().byIndex(position)

    1. drop().byIndex(position: number): ListNode<T> | undefined
    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. // "a" <-> "c"

    它也适用于负索引:

    它是 dropByIndex 的替代API.

    drop().byValue(value [, compareFn])

    1. drop().byValue(value: T, compareFn?: ListComparisonFn<T>): ListNode<T> | undefined

    删除链表中含有给定值的第一个节点:

    1. list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
    2. // "a" <-> "x" <-> "b" <-> "x" <-> "c"
    3. list.drop().byValue('x');
    4. // "a" <-> "b" <-> "x" <-> "c"

    你可以自定义比较器

    1. list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
    2. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
    3. list
    4. .drop()
    5. .byValue(0, (value, searchedValue) => value.x === searchedValue);
    6. // {"x":1} <-> {"x":2} <-> {"x":0} <-> {"x":3}

    它是 dropByValue 的替代API.

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    drop().byValueAll(value [, compareFn])

    删除链表中含有给定值的所有节点:

    1. list.addMany(['a', 'x', 'b', 'x', 'c']).tail();
    2. // "a" <-> "x" <-> "b" <-> "x" <-> "c"
    3. list.drop().byValueAll('x');
    4. // "a" <-> "b" <-> "c"

    你可以自定义比较器

    1. list.addMany([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]).tail();
    2. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
    3. list
    4. .drop()
    5. .byValueAll(0, (value, searchedValue) => value.x === searchedValue);
    6. // {"x":1} <-> {"x":2} <-> {"x":3}

    它是 dropByValueAll 的替代API.

    默认的比较函数检查深度相等性,因此你几乎不需要传递该参数.

    dropMany(count).head()

    1. dropMany(count: number).head(): ListNode<T>[]

    删除链表中指定数量的头节点:

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropMany(2).head();
    4. // "c"

    它是 dropManyHead 的替代API.

    dropMany(count).tail()

    1. dropMany(count: number).tail(): ListNode<T>[]

    删除链表中指定数量的尾节点::

    1. list.addMany(['a', 'b', 'c']).tail();
    2. // "a" <-> "b" <-> "c"
    3. list.dropMany(2).tail();
    4. // "a"

    它是 dropManyTail 的替代API.

    dropMany(count).byIndex(position)

    1. dropMany(count: number).byIndex(position: number): ListNode<T>[]

    删除链表中指定位置和数量的节点:

    1. list.addMany(['a', 'b', 'c', 'd']).tail();
    2. // "a" <-> "b" <-> "c" <-> "d
    3. list.dropMany(2).byIndex(1);
    4. // "a" <-> "d"

    它也适用于负索引:

    1. list.addMany(['a', 'b', 'c', 'd']).tail();
    2. // "a" <-> "b" <-> "c" <-> "d
    3. list.dropMany(2).byIndex(-2);
    4. // "a" <-> "d"

    它是 dropManyByIndex 的替代API.

    有几个方法找到链表特定节点.

    head

    1. head: ListNode<T> | undefined;

    链表中的第一个节点.

    tail

    1. tail: ListNode<T> | undefined;

    链表中的最后一个节点.

    length

    1. length: number;

    链表的节点总数.

    find(predicate)

    1. find(predicate: ListIteratorFunction<T>): ListNode<T> | undefined

    从链表中找到与给定谓词匹配的第一个节点:

    1. list.addManyTail(['a', 'b', 'b', 'c']);
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. var found = list.find(node => node.value === 'b');
    4. /*
    5. found.value === "b"
    6. found.previous.value === "a"
    7. found.next.value === "b"
    8. */

    findIndex(predicate)

    1. findIndex(predicate: ListIteratorFunction<T>): number

    从链表中找到与给定谓词匹配的第一个节点的位置:

    1. list.addManyTail(['a', 'b', 'b', 'c']);
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. var i0 = list.findIndex(node => node.next && node.next.value === 'b');
    4. var i1 = list.findIndex(node => node.value === 'b');
    5. var i2 = list.findIndex(node => node.previous && node.previous.value === 'b');
    6. var i3 = list.findIndex(node => node.value === 'x');
    7. /*
    8. i0 === 0
    9. i1 === 1
    10. i2 === 2
    11. i3 === -1
    12. */

    get(position)

    1. get(position: number): ListNode<T> | undefined

    查找并返回链表中特定位置的节点:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. var found = list.get(1);
    4. /*
    5. found.value === "b"
    6. found.previous.value === "a"
    7. found.next.value === "c"
    8. */

    indexOf(value [, compareFn])

    1. indexOf(value: T, compareFn?: ListComparisonFn<T>): number

    在链表中找到匹配给定值的第一个节点位置:

    1. list.addManyTail(['a', 'b', 'b', 'c']);
    2. // "a" <-> "b" <-> "b" <-> "c"
    3. var i0 = list.indexOf('a');
    4. var i1 = list.indexOf('b');
    5. var i2 = list.indexOf('c');
    6. var i3 = list.indexOf('x');
    7. /*
    8. i0 === 0
    9. i1 === 1
    10. i2 === 3
    11. i3 === -1
    12. */

    你可以自定义比较器

    1. list.addManyTail([{ x: 1 }, { x: 0 }, { x: 2 }, { x: 0 }, { x: 3 }]);
    2. // {"x":1} <-> {"x":0} <-> {"x":2} <-> {"x":0} <-> {"x":3}
    3. var i0 = indexOf(1, (value, searchedValue) => value.x === searchedValue);
    4. var i1 = indexOf(2, (value, searchedValue) => value.x === searchedValue);
    5. var i2 = indexOf(3, (value, searchedValue) => value.x === searchedValue);
    6. var i3 = indexOf(0, (value, searchedValue) => value.x === searchedValue);
    7. var i4 = indexOf(4, (value, searchedValue) => value.x === searchedValue);
    8. /*
    9. i0 === 0
    10. i1 === 2
    11. i2 === 4
    12. i3 === 1
    13. i4 === -1
    14. */

    如何检查所有节点

    有几种方法来遍历或显示一个链表.

    forEach(iteratorFn)

    1. forEach(iteratorFn: ListIteratorFn<T>): void

    从头到尾在链表中的所有节点上运行回调函数:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. list.forEach((node, index) => console.log(node.value + index));
    4. // 'a0'
    5. // 'b1'
    6. // 'c2'

    *[Symbol.iterator]()

    链表是可迭代的. 换句话说你可以使用诸如for ... of之类的方法.

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. for(var node of list) { /* ES6 for...of statement */
    4. console.log(node.value);
    5. }
    6. // 'a'
    7. // 'b'
    8. // 'c'

    toArray()

    1. toArray(): T[]

    转换链表值为数组:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. var arr = list.toArray();
    4. /*
    5. arr === ['a', 'b', 'c']
    6. */

    toNodeArray()

    1. toNodeArray(): T[]

    转换链表节点为数组:

    1. list.addManyTail(['a', 'b', 'c']);
    2. // "a" <-> "b" <-> "c"
    3. var arr = list.toNodeArray();
    4. /*
    5. arr[0].value === 'a'
    6. arr[1].value === 'a'
    7. arr[2].value === 'a'
    8. */

    toString(mapperFn)

    1. toString(mapperFn: ListMapperFn<T> = JSON.stringify): string

    将链表转换为节点及其关系的字符串表示形式:

    1. list.addManyTail(['a', 2, 'c', { k: 4, v: 'd' }]);
    2. // "a" <-> 2 <-> "c" <-> {"k":4,"v":"d"}
    3. var str = list.toString();
    4. /*
    5. str === '"a" <-> 2 <-> "c" <-> {"k":4,"v":"d"}'
    6. */

    你可以在对值进行字符串化之前通过自定义映射器函数来映射值:

    1. list.addMany([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }]).tail();
    2. // {"x":1} <-> {"x":2} <-> {"x":3} <-> {"x":4} <-> {"x":5}
    3. var str = list.toString(value => value.x);
    4. /*
    5. str === '1 <-> 2 <-> 3 <-> 4 <-> 5'
    6. */

    LinkedList

    1. export class LinkedList<T = any> {
    2. // properties and methods are explained above
    3. }

    ListNode

    1. export class ListNode<T = any> {
    2. next: ListNode | undefined;
    3. previous: ListNode | undefined;
    4. constructor(public readonly value: T) {}
    5. }

    ListNode 是存储在 LinkedList 中的每个记录的节点.

    • value value是存储在节点中的值,通过构造函数传递.
    • next 引用列表中的下一个节点.
    • previous引用列表中的上一个节点.
    1. list.addManyTail([ 0, 1, 2 ]);
    2. console.log(
    3. list.head.value, // 0
    4. list.head.next.value, // 1
    5. list.head.next.next.value, // 2
    6. list.head.next.next.previous.value, // 1
    7. list.head.next.next.previous.previous.value, // 0
    8. list.tail.value, // 2
    9. list.tail.previous.value, // 1
    10. list.tail.previous.previous.value, // 0
    11. list.tail.previous.previous.next.value, // 1
    12. list.tail.previous.previous.next.next.value, // 2
    13. );

    Types

    ListMapperFn

    1. type ListMapperFn<T = any> = (value: T) => any;

    该函数在 toString 方法中用于在生成列表的字符串形式之前映射节点值.

    ListComparisonFn

    ListIteratorFn

    1. type ListIteratorFn<T = any, R = boolean> = (
    2. node: ListNode<T>,
    3. index?: number,

    该函数在遍历列表时使用,可以对每个节点执行某些操作,也可以查找某个节点.