Skip to content

Instantly share code, notes, and snippets.

@santosh
Last active June 1, 2019 05:03
Show Gist options
  • Save santosh/d175565ec09abdea35c038cd513da6ca to your computer and use it in GitHub Desktop.
Save santosh/d175565ec09abdea35c038cd513da6ca to your computer and use it in GitHub Desktop.
Linked List implementation in Python. #DataStructure
class Node:
"""This Node class has been created for you.
It contains the necessary properties for the solution, which are:
- name
- matric
- year
- next"""
def __init__(self, name, matric, year):
self.name = name
self.matric = matric
self.year = year
self.__next = None
def set_next(self, node):
"""Set next node to be passed node."""
if isinstance(node, Node) or node is None:
self.__next = node
else:
raise TypeError("The 'next' node must be of type Node or None.")
def get_next(self):
return self.__next
def print_details(self):
print("{}: {} (year {})".format(self.matric, self.name, self.year))
def __str__(self):
return "<Node: {}>".format(self.name)
class LinkedList:
"""Implementation of Linked List."""
def __init__(self):
self.__root = None
def get_root(self):
return self.__root
def add_to_list(self, node):
"""Adds node to the beginning of the list.
:param Node node: Node to be added to the linked list."""
if self.__root:
# Sets the next node of the new node to be __root of LinkedList
node.set_next(self.__root)
self.__root = node
def print_list(self):
marker = self.__root
while marker:
marker.print_details()
marker = marker.get_next()
def find(self, name):
"""Finds a node in the linked list with a given name.
:param name: the name of the node to find in this list.
:return: Found Node object, or else raises a LookupError."""
marker = self.__root
while marker:
if marker.name == name:
return marker
marker = marker.get_next()
raise LookupError("Name {} not in the linked list.".format(name))
from unittest import TestCase
from linkedlist import LinkedList, Node
class TestLinkedList(TestCase):
"""
This class tests that your LinkedList class was implemented correctly.
All you have to do is run this file.
If any tests fail, then you are not done yet.
If all tests pass, good job! You can move on to the next challenge.
"""
def test_node_creation(self):
name = "Jose"
matric = "1234"
year = 2
node = Node(name, matric, year)
self.assertEqual(name, node.name)
self.assertEqual(matric, node.matric)
self.assertEqual(year, node.year)
def test_list_creation(self):
linked_list = LinkedList()
self.assertIsNone(linked_list.get_root())
def test_add_to_list(self):
name = "Jose"
matric = "1234"
year = 2
node = Node(name, matric, year)
linked_list = LinkedList()
linked_list.add_to_list(node)
self.assertEqual(linked_list.get_root(), node)
def test_add_many_to_list(self):
names = ("Jose", "1234", 2), ("Rolf", "2345", 3), ("Anna", "3456", 7)
nodes = [Node(name, matric, year) for name, matric, year in names]
linked_list = LinkedList()
for node in nodes:
linked_list.add_to_list(node)
marker = linked_list.get_root()
for i in range(len(nodes)-1, -1, -1):
self.assertEqual(marker, nodes[i])
marker = marker.get_next()
def test_find_in_list(self):
names = ("Jose", "1234", 2), ("Rolf", "2345", 3), ("Anna", "3456", 7)
nodes = [Node(name, matric, year) for name, matric, year in names]
linked_list = LinkedList()
for node in nodes:
linked_list.add_to_list(node)
marker = linked_list.get_root()
for i in range(len(nodes) - 1, -1, -1):
self.assertEqual(linked_list.find(marker.name), nodes[i])
marker = marker.get_next()
def test_find_missing_in_list(self):
linked_list = LinkedList()
with self.assertRaises(LookupError):
linked_list.find("Smith")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment