class Stack: def __init__(self): self.stack_list = DoublyLinkedList() @property def size(self): return self.stack_list.size @property def is_empty(self): return self.size == 0 def push(self, element): self.stack_list.add_last(element) def pop(self): if self.is_empty: raise Exception("Empty Stack") return self.stack_list.remove_last() def peek(self): if self.is_empty: raise Exception("Empty Stack") return self.stack_list.peek_first() def __iter__(self): return DoublyLinkedListIterator(self.stack_list.head)
def test_remove_first_on_one_element_list(self): """ Should make the list empty """ dll = DoublyLinkedList() dll.add_last(5) element = dll.remove_first() self.assertEqual(element, 5) self.assertEqual(dll.count, 0) self.assertEqual(list(dll), [])
def test_add_several_last_elements(self): dll = DoublyLinkedList() dll.add_last(5) dll.add_last(10) dll.add_last(15) self.assertEqual(dll.count, 3) self.assertEqual(list(dll), [5, 10, 15])
def test_remove_first_several_elements(self): dll = DoublyLinkedList() dll.add_last(5) dll.add_last(6) dll.add_last(7) first_el = dll.remove_first() self.assertEqual(first_el, 5) self.assertEqual(dll.count, 2) self.assertEqual(list(dll), [6, 7])
def test_cast_to_list_multiple_elements(self): dll = DoublyLinkedList() dll.add_last("Five") dll.add_last("Six") dll.add_last("Seven") self.assertEqual(list(dll), ["Five", "Six", "Seven"])
def test_iterate_multiple_elements(self): dll = DoublyLinkedList() dll.add_last("Five") dll.add_last("Six") dll.add_last("Seven") items = [] for item in dll: items.append(item) self.assertEqual(items, ["Five", "Six", "Seven"])
def test_cast_to_list_one_element(self): dll = DoublyLinkedList() dll.add_last(5) self.assertEqual(list(dll), [5])
def test_add_last_empty_list(self): dll = DoublyLinkedList() dll.add_last(5) self.assertEqual(dll.count, 1) self.assertEqual(list(dll), [5])