Use hash table. Each node is a single entry in a Python dictionary. The key models a pointer, and is a randomly generated hex integer, just as a real pointer would be. The value models the attributes of a node and is implemented as a tuple containing (data, key_of_next_node).
The beginning of the list is stored in self.root.
Initialization requires at least one item in the list. Example:
>>> import linkedlist as L
>>> x = L.LinkedList(5)
>>> x.print()
5
Functions:
insert(datum[, prior_node]): ifdatumis list or string, each index will be inserted into its own node of the linked list; otherwise, insertion is at the root. Returns key of inserted node. Only the initial creation of the list does not return anything, but the list itself is then accessible atx.root.delete(datum); returns deleted key of elseNone. On empty list, callsdestroy_on_empty().find(datum). Returns key of found node or elseNone.length()print(verbose = False): Prints whole list in a single line. Onverbose = True, prints both key and data of each node on a successive line.garbage_collect(). On empty list, callsdestroy_on_empty().return_list()destroy_on_empty(): destroysllistandrootto prevent further use of object. Used whendelete(datum)orgarbage_collect()produce an empty list.
Maybe no function for traversal — the cost of calling a function is expensive compared with implementing its content in place.
- Test suite.
- Option for doubly linked list using XOR.
- Option for circular linked list.
insertat a particular index
[end]