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