Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python have built-in linkedList data structure?

Tags:

python-2.7

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 queue).

like image 398
learner Avatar asked Feb 03 '13 02:02

learner


People also ask

Why Python doesn't have linked list?

Python doesn't ship with a built-in linked list data type in the “classical” sense. Python's list type is implemented as a dynamic array—which means it doesn't suit the typical scenarios where you'd want to use a “proper” linked list data structure for performance reasons.

Are Python lists stored as linked lists?

Computer Memory Store the list items in consecutive locations in memory (python list) Store "nodes" anywhere in memory. A Node consists of the list item, plus the address (or something like the address) of where the next node can be found. This is known as a linked list.

Is linked list built-in data structure?

Linked lists are built using a data structure called a Node to store data. Along with the data, each node stores a pointer that points to the next node in the list. You can think of the pointer as the memory address of the next node, or the next node itself.

Is Python list linked or array?

Python lists are internally represented as arrays.


1 Answers

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;
like image 187
Łukasz Rogalski Avatar answered Sep 28 '22 09:09

Łukasz Rogalski