在看 collections.OrderedD...">
美文网首页
python双向链表的疑问(Question)

python双向链表的疑问(Question)

作者: Nisen | 来源:发表于2016-11-23 00:07 被阅读75次

问题<a id="sec-1" name="sec-1"></a>

在看 collections.OrderedDict 的源码时,对于它如何构造有序的结构这一部分不是很理解,代码如下:

class OrderedDict(dict):
    'Dictionary that remembers insertion order'
    # An inherited dict maps keys to values.
    # The inherited dict provides __getitem__, __len__, __contains__, and get.
    # The remaining methods are order-aware.
    # Big-O running times for all methods are the same as regular dictionaries.

    # The internal self.__map dict maps keys to links in a doubly linked list.
    # The circular doubly linked list starts and ends with a sentinel element.
    # The sentinel element never gets deleted (this simplifies the algorithm).
    # Each link is stored as a list of length three:  [PREV, NEXT, KEY].

    def __init__(*args, **kwds):
        '''Initialize an ordered dictionary.  The signature is the same as
        regular dictionaries, but keyword arguments are not recommended because
        their insertion order is arbitrary.

        '''
        if not args:
            raise TypeError("descriptor '__init__' of 'OrderedDict' object "
                            "needs an argument")
        self = args[0]
        args = args[1:]
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        try:
            self.__root
        except AttributeError:
            self.__root = root = []                     # sentinel node
            root[:] = [root, root, None]
            self.__map = {}
        self.__update(*args, **kwds)

    def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
        'od.__setitem__(i, y) <==> od[i]=y'
        # Setting a new item creates a new link at the end of the linked list,
        # and the inherited dictionary is updated with the new key/value pair.
        if key not in self:
            root = self.__root
            last = root[0]
            last[1] = root[0] = self.__map[key] = [last, root, key]
        return dict_setitem(self, key, value)

    def __delitem__(self, key, dict_delitem=dict.__delitem__):
        'od.__delitem__(y) <==> del od[y]'
        # Deleting an existing item uses self.__map to find the link which gets
        # removed by updating the links in the predecessor and successor nodes.
        dict_delitem(self, key)
        link_prev, link_next, _ = self.__map.pop(key)
        link_prev[1] = link_next                        # update link_prev[NEXT]
        link_next[0] = link_prev                        # update link_next[PREV]

    def __iter__(self):
        'od.__iter__() <==> iter(od)'
        # Traverse the linked list in order.
        root = self.__root
        curr = root[1]                                  # start at the first node
        while curr is not root:
            yield curr[2]                               # yield the curr[KEY]
            curr = curr[1]                              # move to next node

    def __reversed__(self):
        'od.__reversed__() <==> reversed(od)'
        # Traverse the linked list in reverse order.
        root = self.__root
        curr = root[0]                                  # start at the last node
        while curr is not root:
            yield curr[2]                               # yield the curr[KEY]
            curr = curr[0]                              # move to previous node

    def clear(self):
        'od.clear() -> None.  Remove all items from od.'
        root = self.__root
        root[:] = [root, root, None]
        self.__map.clear()
        dict.clear(self)

主要是对于初始化里和set方法里的做法不清楚, wtf doing here…:

            self.__root = root = []                     # sentinel node
            root[:] = [root, root, None]
            self.__map = {}
# 和
            root = self.__root
            last = root[0]
            last[1] = root[0] = self.__map[key] = [last, root, key]

后来在网上提问并且自己查询了相关资料后明白这是个带哨兵的双向链表的实现,关于双向链表的知识自己补了下,可以参见这里这里

然而对于它为什么要这样实现我还不是很清楚,留待以后继续探究。

这里 是我在v2ex上关于这个问题的提问。

相关文章

  • python双向链表的疑问(Question)

    问题 在看 collections.OrderedD...

  • 双向链表python实现

    python 双向链表实现 双向链表实现 链表头部添加 链表尾部添加 插入 删除 查询结点

  • Python实现双向链表

    Python实现双向链表的增删改查,反转链表

  • Python数据结构-链表

    自己实现一遍果然感觉不一样 Python实现单链表 Python实现单项循环链表 Python实现双向链表

  • 双向链表&双向循环链表

    链表分为:单链表、单向循环链表、双向链表、双向循环链表本节主要说明:双向链表、双向循环链表 定义结点 一、双向链表...

  • 线性表-双向链表与双向循环链表

    双向链表 双向链表示意图如下: 数据结构定义 创建双向链表 双向链表插入元素 双向链表删除元素 双向链表打印元素 ...

  • 双向链表和双向循环链表

    双向链表 线性表-双向链表的结点结构: 带头结点的双向链表: 1.双向链表初始化 2.遍历双向链表 2.双向链表插...

  • 双向链表&双向循环链表

    一、双向链表 带有前驱结点、后区节点 双向链表的创建 双向链表插入-逻辑 双向链表删除 删除双向链表指定的元素 二...

  • 9.双向链表DoubleLinkList

    目录:1.双向链表的定义2.双向链表的图解3.双向链表定义操作4.双向链表的实现 1.双向链表的定义 2.双向链表...

  • day03-双向链表

    双向链表: 单向链表只能单向查找,双向链表可以双向查找。 啥是双向链表? 双向链表可以双向查数据,所以就不存在单向...

网友评论

      本文标题:python双向链表的疑问(Question)

      本文链接:https://www.haomeiwen.com/subject/zlvmpttx.html