class SLLStack(object): def __init__(self): self.size = 0 self._data = SinglyLinkedList() def __len__(self): return self.size def build(self, list_object): if not isinstance(list_object, list): return TypeError('build method only accepts object of type list.') for i in list_object: self._data.add_value(i) self.size += 1 def is_empty(self): return self.size == 0 def push(self, e): self.size += 1 self._data.add_value(e) def pop(self): if self.is_empty(): raise IndexError("Stack is empty") top = self._data.head.value self._data.delete_node(self._data.head) self.size -= 1 return top def top(self): if self.is_empty(): raise IndexError("Stack is empty") return self._data.head.value
def test_delete_node_method_removes_node_with_given_data(): """Test that the delete_node method of the Singly Linked List class removes a Node from the list with the given data.""" ll = SinglyLinkedList() ll.add_node(1) ll.delete_node(1) assert ll.head is None
def test_delete_node_method_reassigns_next_node_to_none_if_deleting_end_Node(): """Test that the delete_node method of the Singly Linked List correctly reassigns the next_node to Node if it's removing the last Node of the list .""" ll = SinglyLinkedList() ll.add_node(1) ll.add_node(2) ll.delete_node(1) assert ll.head.next_node is None
def test_delete_node_method_reassigns_head_of_list_if_head_is_deleted(): """Test that the delete_node method of the Singly Linked List class correctly reassigns the head Node of the list to the next Node if the head Node is removed.""" ll = SinglyLinkedList() ll.add_node(1) ll.add_node(2) ll.delete_node(2) assert ll.head.data == 1
def test_delete_node_method_correctly_reassigns_pointers_if_deleting_middle(): """Test that the delete_node method of the Singly Linked List class correctly reassigns pointers if deleting a Node that has two Nodes on either side of it.""" ll = SinglyLinkedList() for i in range(3): ll.add_node(i) ll.delete_node(1) assert ll.head.data == 2 and ll.head.next_node.data == 0
def test_delete_node_method_raises_exception_if_list_is_empty(): """Test that the delete_node method of the Singly Linked List class raises an exception if used on an empty list.""" ll = SinglyLinkedList() with pytest.raises(LookupError): assert ll.delete_node(1)
def test_delete_node_method_returns_data_value_of_deleted_node(): """Test that the delete_node method of the Singly Linked List class returns the data value of the Node that it deletes.""" ll = SinglyLinkedList() ll.add_node(1) assert ll.delete_node(1) == 1
def test_delete_node_raises_exception_if_node_with_given_data_not_in_list(): """Test that the delete_node method of the Singly Linked List class raises an exception if no Node with the given data exists in the list.""" ll = SinglyLinkedList() ll.add_node(1) with pytest.raises(LookupError): assert ll.delete_node(5)