Does python have built-in linkedList data structure?

后端 未结 5 1974
情深已故
情深已故 2021-02-07 05:05

Does anyone know if Python (maybe 2.7) has a built-in linkedList data structure? I know the queue is implemented using list, and there is no stack (there is LIFO qu

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 05:45

    Yes, Python's collections module provides C-implemented deque object, which uses linked list of BLOCKs internally.

    typedef struct BLOCK {
        struct BLOCK *leftlink;
        PyObject *data[BLOCKLEN];
        struct BLOCK *rightlink;
    } block;
    
    typedef struct {
        PyObject_VAR_HEAD
        block *leftblock;
        block *rightblock;
        Py_ssize_t leftindex;       /* 0 <= leftindex < BLOCKLEN */
        Py_ssize_t rightindex;      /* 0 <= rightindex < BLOCKLEN */
        size_t state;               /* incremented whenever the indices move */
        Py_ssize_t maxlen;          /* maxlen is -1 for unbounded deques */
        PyObject *weakreflist;
    } dequeobject;
    
    static PyTypeObject deque_type;
    

提交回复
热议问题