Skip to main content

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

# create a list with three nodes
ll = LinkedList(["a", "b", "d"])

# get the node with element "b"
bNode = ll.findFirstNode("b")

# insert "c" after bNode
ll.insertAfter(bNode, "c")

print(list(ll))
# prints ['a', 'b', 'c', 'd']

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) # a
lastNode: node
print(ll.lastNode.element) # c

Methods

append(element)
ll.append('d')
print(list(ll)) # ['a', 'c', 'd']
copy() => LinkedList
appendAll(list)
ll.appendAll(['d', 'e'])
print(list(ll)) # ['a', 'c', 'd', 'e']
findFirstNode(element) => node
cNode = ll.findFirstNode(['c'])
print(cNode.element) # c
insertAfter(node, element)
ll.insertAfter(ll.firstNode, 'b')
print(list(ll)) # ['a', 'b', 'c']
insertAllAfter(node, list)
ll.insertAllAfter(ll.firstNode, ['b', 'd'])
print(list(ll)) # ['a', 'b', 'd', 'c']
insertAllBefore(node, list)
ll.insertAllBefore(ll.lastNode, ['b', 'd'])
print(list(ll)) # ['a', 'b', 'd', 'c']
insertBefore(node, element)
ll.insertBefore(ll.lastNode, 'b')
print(list(ll)) # ['a', 'b', 'c']
prepend(element)
ll.prepend('z')
print(list(ll)) # ['z', 'a', 'c']
prependAll(list)
ll.prependAll(['y', 'z'])
print(list(ll)) # ['y', 'z', 'a', 'c']
removeFirstElement(element)
ll.removeFirstElement('c')
print(list(ll)) # ['a']
removeNode(node)
ll.removeNode(ll.firstNode)
print(list(ll)) # ['c']

node

  • a node is just an instance of SimpleNamespace with three attributes
    • element: <can be anything>
    • next_: node
    • previous: node
print(ll.firstNode.element) # a
print(ll.firstNode.next_.element) # c
print(ll.lastNode.previous.element) # a
print(ll.firstNode.previous is None) # True

Test

#
# you must have poetry installed
#
$ poetry shell
$ poetry install
$ python runTests.py

Release files for another-linked-list 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for another-linked-list 0.1.1
File Size Uploaded
another_linked_list-0.1.1.tar.gz 362.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for another-linked-list 0.1.1
File Interpreter ABI Platform
another_linked_list-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 790.2 kB

Release files / another_linked_list-0.1.1.tar.gz

Download URL another_linked_list-0.1.1.tar.gz
Size 362.4 kB
Tags Source
SHA-256 checksum
How to use checksums
15af2f29a24c71e6b6027fd1b048a56c8aa2fdc58e5e89f435a80c2851e5abeb
BLAKE2b-256 checksum
How to use checksums
f67da35354a043cdc7d6141d84dca518b50649fed98162a2831143192ce9e2fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.15 CPython/3.7.3 Linux/5.1.2-arch1-1-ARCH

Release files / another_linked_list-0.1.1-py3-none-any.whl

Download URL another_linked_list-0.1.1-py3-none-any.whl
Size 427.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ccc2d2a7336b07443830bd50e0d9f47d6760b8bbd2e60b8c1b947f1d75aeb97d
BLAKE2b-256 checksum
How to use checksums
a16534d2c1c1eda9239d37fbbbe879b92d0b8d12cc7e629346e6b7294bdcdd58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.15 CPython/3.7.3 Linux/5.1.2-arch1-1-ARCH

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page