Another Linked List
Table of Contents
What is it?
- An incomprehensive implementation of a doubly-linked list
Why create it?
- I didn't like the api or documentation of other linked lists. I'm also new
to python so this was a good way to learn.
Simple usage
from another_linked_list import LinkedList
ll = LinkedList(["a", "b", "d"])
bNode = ll.findFirstNode("b")
ll.insertAfter(bNode, "c")
print(list(ll))
Api
class LinkedList([a list of elements])
- the linked list holds a list of nodes. Each node holds an element
(the value) along with pointers to the previous and next nodes. For the most
part the methods are intended to allow you to work with the elements moreso
than the nodes because that was my use-case. This design decision may change
in the future to be more focused around the nodes.
- all methods return
self
unless specified otherwise - all methods which take a list argument also accept an instance of LinkedList
- in all code examples below, assume
ll
starts with LinkedList(["a", "c"])
- the internal methods implemented are
- __copy__
- __iter__ (iterates over the elements, not the nodes)
- __len__
- __reversed__
Attributes
firstNode: node
print(ll.firstNode.element)
lastNode: node
print(ll.lastNode.element)
Methods
append(element)
ll.append('d')
print(list(ll))
copy() => LinkedList
appendAll(list)
ll.appendAll(['d', 'e'])
print(list(ll))
findFirstNode(element) => node
cNode = ll.findFirstNode(['c'])
print(cNode.element)
insertAfter(node, element)
ll.insertAfter(ll.firstNode, 'b')
print(list(ll))
insertAllAfter(node, list)
ll.insertAllAfter(ll.firstNode, ['b', 'd'])
print(list(ll))
insertAllBefore(node, list)
ll.insertAllBefore(ll.lastNode, ['b', 'd'])
print(list(ll))
insertBefore(node, element)
ll.insertBefore(ll.lastNode, 'b')
print(list(ll))
prepend(element)
ll.prepend('z')
print(list(ll))
prependAll(list)
ll.prependAll(['y', 'z'])
print(list(ll))
removeFirstElement(element)
ll.removeFirstElement('c')
print(list(ll))
removeNode(node)
ll.removeNode(ll.firstNode)
print(list(ll))
node
- a node is just an instance of SimpleNamespace with three attributes
- element: <can be anything>
- next_: node
- previous: node
print(ll.firstNode.element)
print(ll.firstNode.next_.element)
print(ll.lastNode.previous.element)
print(ll.firstNode.previous is None)
Test
$ poetry shell
$ poetry install
$ python runTests.py