class Queue(): """implementing queue functionality from LL""" def __init__(self, data): """init""" self.linked_list = LL(LLN(data)) def __str__(self): """tostring method""" return str(self.linked_list).replace('Linked List', 'Queue') def enqueue(self, new_value): """enqueuing stuff onto list (onto top of LL))""" self.linked_list.insert(new_value) def dequeue(self): """takes the last thing off of the list""" curr = self.linked_list.head prev = None while curr.get_next(): prev = curr curr = curr.get_next() if prev: prev.update_next(None) return curr else: raise ValueError("Cant remove last element of queue")
class Stack(): """implementing stack functionality as a linked list""" def __init__(self, data): """initialization""" self.linked_list = LL(LLN(data)) def __str__(self): """tostring method""" return str(self.linked_list).replace('Linked List', 'Stack') def peek(self): """get top of stack""" return self.linked_list.head def pop(self): """popping off the last value (first value in LL)""" node = self.linked_list.head self.linked_list.head = self.linked_list.head.get_next() return node def push(self, new_value): """pushing stuff onto list (onto top of LL))""" self.linked_list.insert(new_value)
class TestLinkedList(unittest.TestCase): def setUp(self): self.list = LL() def tearDown(self): self.list = None def test_insert(self): self.list.insert("David") self.assertTrue(self.list.head.get_data() == "David") self.assertTrue(self.list.head.get_next() is None) def test_insert_two(self): self.list.insert("David") self.list.insert("Thomas") self.assertTrue(self.list.head.get_data() == "Thomas") head_next = self.list.head.get_next() self.assertTrue(head_next.get_data() == "David") def test_nextNode(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") self.assertTrue(self.list.head.get_data() == "Rasmus") head_next = self.list.head.get_next() self.assertTrue(head_next.get_data() == "Pallymay") last = head_next.get_next() self.assertTrue(last.get_data() == "Jacob") def test_positive_search(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") found = self.list.search("Jacob") self.assertTrue(found.get_data() == "Jacob") found = self.list.search("Pallymay") self.assertTrue(found.get_data() == "Pallymay") found = self.list.search("Jacob") self.assertTrue(found.get_data() == "Jacob") def test_searchNone(self): self.list.insert("Jacob") self.list.insert("Pallymay") # make sure reg search works found = self.list.search("Jacob") self.assertTrue(found.get_data() == "Jacob") with self.assertRaises(ValueError): self.list.search("Vincent") def test_delete(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") # Delete the list head self.list.delete("Rasmus") self.assertTrue(self.list.head.get_data() == "Pallymay") # Delete the list tail self.list.delete("Jacob") self.assertTrue(self.list.head.get_next() is None) def test_delete_value_not_in_list(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") with self.assertRaises(ValueError): self.list.delete("Sunny") def test_delete_empty_list(self): with self.assertRaises(ValueError): self.list.delete("Sunny") def test_delete_next_reassignment(self): self.list.insert("Jacob") self.list.insert("Cid") self.list.insert("Pallymay") self.list.insert("Rasmus") self.list.delete("Pallymay") self.list.delete("Cid") self.assertTrue(self.list.head.next_node.get_data() == "Jacob")