# this should be transparent to the user. # non-contiguous range, we do need to create a copy. # For cases where the slice stops before the end, or uses a # Normal list indexing and slice operations can be performed. # Or prepending arbitrary sequences (Still no copy of l performed): # Prepending is O(1) and can be done with: It will have the desired big O performance characteristics however.Įxample of usage: > l = LinkedList() It won't be as space or time efficient as lisp cons cells, as python classes are obviously a bit more heavyweight (You could improve things slightly with " _slots_ = '_head','_tail'" to reduce memory usage). It should have O(1) prepend, doesn't copy data unless it needs to and can be used pretty interchangably with tuples. supports indexing, slicing, concatenation with arbitrary sequences etc). Here's a slightly more complex version of a linked list class, with a similar interface to python's sequence types (ie. Out = 'LinkedList [\n' +str(current.value) +'\n' class Node:ĭef _init_(self, value = None, next = None): It is a simple LinkedList class based on the straightforward C++ design and Chapter 17: Linked lists, as recommended by Thomas Watnedal. Here is a more standard design: L = LinkedList() The accepted answer is rather complicated. class Node:ĭef _init_(self, cargo=None, next=None):
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |