Skip to content

Instantly share code, notes, and snippets.

@chuckha
Created August 1, 2013 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckha/6131932 to your computer and use it in GitHub Desktop.
Save chuckha/6131932 to your computer and use it in GitHub Desktop.
An empty node object for a linked list with full test coverage.
class Node(object):
"""A node to be used for a linked list"""
def __init__(self, data):
"""Create a new Node instance
>>> head = Node(4)
>>> print(head.next)
None
>>> head.data
4
"""
pass
def __len__(self):
"""Get the length of the list from the current node
>>> head = Node(4)
>>> len(head)
1
>>> head.insert_after(Node(1))
>>> len(head)
2
"""
pass
def __str__(self):
"""Print the data the node contains and what it points to
>>> head = Node(4)
>>> print(head)
4 => None
>>> head.insert_after(Node(5))
>>> print(head)
4 => 5 => None
"""
pass
def insert_after(self, new):
"""Insert a node after the current node.
>>> head = Node(4)
>>> head.insert_after(Node(5))
>>> print(head)
4 => 5 => None
"""
pass
def insert_beginning(self, new):
"""Insert a node at the beginning of a list.
>>> head = Node(5)
>>> print(head)
5 => None
>>> head = head.insert_beginning(Node(2))
>>> print(head)
2 => 5 => None
"""
pass
def remove_after(self):
"""Remove the node following the current node.
>>> head = Node(2)
>>> head.insert_after(Node(3))
>>> head.remove_after()
>>> print(head)
2 => None
"""
pass
def remove_beginning(self):
"""Remove the current head.
>>> head = Node(1)
>>> head.insert_after(Node(2))
>>> head = head.remove_beginning()
>>> print(head)
2 => None
"""
pass
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment