|
|
|
|
|
by rectangletangle
3408 days ago
|
|
Python has many solid example's of properly applied data structures, with nice encapsulated high-level interfaces. It's a good language to learn if you're interested in the application of data structures. For instance you have your standard list, which IIRC is implemented as a dynamically allocated array of pointers to Python objects. This gives very fast append and pop operations on it's right side, making it a wonderful stack. However it's interface specifically lacks a dedicated prepend/appendleft operation, because appending to the left of the array involves shifting the entire structure over one place in memory. This is a very slow operation, because it must copy the entire structure to simply insert a single element. Python get's away with append on the right, by allocating a bit of extra "padding" memory on the right side, to avoid having to copy the structure. For the situation where you want to append to either side, Python offers `collections.deque`. This structure is implemented with a linked list of pointers, IIRC. Which gives very fast append operations from either side, because adding a new element only involves "hooking" the new element to the preferred side, kind of like a chain. However for understanding how these structures are implemented C based languages are the way to go. |
|
Not quite sure I get this. What's this got to do with Python? Arrays, or more specifically resizing arrays, are supposed to work like what you just described. Appends are constant time and you almost always have extra memory because of the resizing nature. Is this different in other languages?