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).
标签:
python-2.7
相关问题
- Flush single app django 1.9
- How to stop a dbus gobject loop
- Python's difflib SequenceMatcher speed up
- Getting 'Missing required field: member' w
- What can be the maximum “POST” size I can have?
相关文章
- Is there a size limit for HTTP response headers on
- Does there exist empty class in python?
- ImportError: No module named twisted.persisted.sty
- Get a header with Python and convert in JSON (requ
- python unit testing methods inside of classes
- Requiring tensorflow with Python 2.7.11 occurs Imp
- Generate Fortran subroutine with SymPy codegen for
- Python - What is a lazy property?
Unless you actually want an explicit linked list structure for something specific, Python's built-in list has all the functionality that you get from a linked list. For example, you can use it as a stack, as follows:
Or, to insert an element after a given element:
See, for example, the Python documentation on data structures.
However, this is using the "list" abstract data type (ADT). In contrast, a "linked list" is not an ADT but one of many possible ways of implementing that ADT.
If efficiency of prepending is an issue, Łukasz Rogalski's answer points out that
collections.deque
is implemented using a linked list. As Using Lists as Queues notes:To test the efficiency impacts of using
list
vs.deque
, I used the following program:... and got the following results for Python 3.6 and Python 2.7:
Thus,
list
takes roughly twice as much time to append an element as doesdeque
anddeque
takes less time to prepend (i.e. append to left) than to append.There is no built in linked list in python but u can use dequeue, it gives u access to head and tail both, but if u want to implement yours own linked list may be u can use
Python Linked List
I believe the deque class in the collections package is implemented as a doubly linked list, with head and tail guards. It supports all the usual API of the default list. To append to the head, use
leftappend
function.Python has collections.deque, which is a doubly linked list of small list()s.
You're almost always better off using a Python list() instead of a linked list though. Even though linked lists have a superior big-O for some operations, the list() is frequently faster because of better locality of reference.
I actually played with linked lists in Python a bit: http://stromberg.dnsalias.org/~strombrg/linked-list/ ...prior to doing a performance comparison.
I used my "count" program as a test bed; with a linked list, it was both slower and used more RSS (RAM) than a version that used list(). count is at http://stromberg.dnsalias.org/~strombrg/count.html I threw away the linked list version of it; it wasn't terribly useful.
It kind of makes sense that a list() would be faster at times. Consider doing a traversal from the first element to the last in both a list() and a linked list. list() tends to do a single RAM cache hit for n element references, while a linked list tends to do a single RAM cache hit for every element. That's because the list() references are all next to each other in RAM, but the linked list references are in a different class instance for every value. With caches being much faster than regular RAM, that can matter quite a bit.
collections.deque does not fall prey to this as much, because of the small arrays making up each node in the linked list.
On the other hand, if you want to avoid people using random access to your collection of values, a linked list might be a good idea. That is, sometimes for the sake of abstraction you may prefer the linked list over a list(). But Python developers tend to assume that "we're all adults."
Also, inserting in the middle of a list, if you already know where to put your new value (that is, you don't need an O(n) search to figure out where to put it), it's quite a bit faster with a linked list.
Yes, Python's collections module provides C-implemented
deque
object, which uses linked list ofBLOCK
s internally.