📜  Python中 llist 模块的 dllist 类

📅  最后修改于: 2022-05-13 01:54:30.531000             🧑  作者: Mango

Python中 llist 模块的 dllist 类

llist 是 CPython 的扩展模块,提供了基本的链表结构。它们比出列甚至标准列表要快得多。

双向链表

它是一种链表,在每个节点中存储数据以及两个地址(其前后节点的地址)。一个更简单的定义是,在双向链表中,每个节点都指向它之前的节点以及紧随其后的节点。
下图更好地解释了它:

在 llist 中,有一个 dllist 对象可以帮助成功实现双向链表。

Dlllist 对象

类 llist.dllist([iterable])
返回从提供的可迭代对象初始化的新双向链表。如果没有给出可迭代对象,则生成链表但为空。

import llist
from llist import dllist
  
lst = llist.dllist(['first', 'second', 'third'])
print(lst)

输出:

dllist([first, second, third])

dllist 支持以下属性:

  • first :只读属性,打印列表的第一个属性,如果列表为空,则打印 None
    print(lst.first)
    

    输出:

    dllistnode(first)
  • last :只读属性,返回列表的最后一个元素(尾),如果列表为空,则返回 None。
    print(lst.last)
    

    输出:

    dllistnode(third)
  • size : 返回列表大小的只读属性
    print(lst.size)
    

    输出:

    3

dllist 还支持以下方法:

  • append(x) :将 x 添加到列表的右侧并返回一个插入的 dllist 节点。如果 x 已经是一个 dlist 节点,则创建一个新节点并使用从 x 中提取的值进行初始化。
    lst.append('fourth')
    print(lst)
    

    输出:

    dllist([first, second, third, fourth])
  • appendleft(x) :将 x 添加到列表的左侧并返回一个插入的 dllist 节点。如果 x 已经是 dllist 节点,则创建一个新节点并使用从 x 中提取的值进行初始化。
    lst.appendleft('fourth')
    print(lst)
    

    输出:

    dllist([fourth, first, second, third])
  • appendright(x) :将 x 添加到列表的右侧并返回一个插入的 dllist 节点。如果 x 已经是 dllist 节点,则创建一个新节点并使用从 x 中提取的值进行初始化。
    lst.appendright('fourth')
    print(lst)
    

    输出:

    dllist([first, second, third, fourth])
  • clear() :从列表中删除所有节点
    lst.clear()
    print(lst)
    

    输出:

    dllist()
  • extend([iterable]) :将可迭代的元素添加到列表的右侧。
    lst.extend(['fourth', 'fifth'])
    print(lst)
    

    输出:

    dllist([first, second, third, fourth, fifth])
  • extendleft([iterable]) :将可迭代的元素添加到列表的左侧
    lst.extendleft(['fourth', 'fifth'])
    print(lst)
    

    输出:

    dllist([fifth, fourth, first, second, third])
  • extendright([iterable]) :将可迭代的元素添加到列表的右侧
    lst.extendright(['fourth', 'fifth'])
    print(lst)
    

    输出:

    dllist([first, second, third, fourth, fifth])
  • insert() :将提供的元素添加到列表的右侧。它通常用于在列表中的任何位置插入元素,并且应该提供应该在它之前插入的元素。
    lst.insert('fourth')
    node = lst.nodeat(2)
    lst.insert('fifth', node)
    print(lst)
    

    输出:

    dllist([first, second, fifth, third, fourth])
  • nodeat(index) :返回指定索引处的节点。允许使用负地址。
    print(lst.nodeat(2))
    print(lst.nodeat(-2))
    

    输出:

    dllistnode(third)
    dllistnode(second)
  • pop() :从列表的右侧移除并返回一个元素的值。
    lst.pop()
    print(lst)
    

    输出:

    dllist([first, second])
  • popleft() :从列表的左侧移除并返回一个元素的值。
    lst.popleft()
    print(lst)
    

    输出:

    dllist([second, third])
  • popright :从列表的右侧移除并返回一个元素的值。
    lst.popright()
    print(lst)
    

    输出:

    dllist([first, second])
  • remove() :从列表中删除指定节点并返回存储在其中的元素。
    node = lst.nodeat(1)
    lst.remove(node)
    print(lst)
    

    输出:

    dllist([first, third])
  • rotate(n) :如果 n 为正,则将列表向右旋转 n 步,如果为负,则向左旋转 n 步
    lst.rotate(4)
    print(lst)
    

    输出:

    dllist([third, first, second])

除了这些方法之外,dllist 还支持迭代、cmp(lst1, lst2)、丰富的运算符、常数时间 len(lst)、hash(lst) 以及用于按索引访问元素的下标引用 lst[1234]。

让我们进一步讨论更多与 dllist 相关的 llist 对象:

1) dlllist节点

在双向链表中实现一个节点,如果提供了值,则可以选择对其进行初始化。

node = llist.dllistnode('zeroth')
print(node)

输出:

dllistnode(zeroth)

该对象还支持以下属性:

  • next : 只读属性,打印列表中的下一个节点
  • prev :只读属性,打印列表中的上一个节点
  • value :打印存储在列表中的值
node = lst.nodeat(1)
print(node.next)
print(node.prev)
print(node.value)

输出:

dllistnode(third)
dllistnode(first)
second

2) dllistiterator

返回一个新的双向链表迭代器。这些对象不是由用户创建的,而是由 dllist.__iter__() 方法返回以保持迭代状态。通过 dllistiterator 接口迭代将直接产生存储在节点中的值。

import llist
from llist import dllist
  
lst = llist.dllist(['first', 'second', 'third'])
  
for value in lst:
  print(value)

输出:

first
second
third