def test_dll_remove(): """Test for dll remove.""" from dll import DoublyLinkedList one_dll, empty_dll, new_dll = sample_dll() new_dll.remove(3) assert new_dll.length == 4 assert new_dll.head_node.next_node.next_node.contents == 2 assert new_dll.head_node.next_node.next_node.previous_node.contents == 4 try: new_dll.remove(10) except NameError: assert True new_dll.remove(5) assert new_dll.head_node.contents == 4 new_dll.remove(1) assert new_dll.tail_node.contents == 2 empty_dll = DoublyLinkedList() try: empty_dll.remove(100) except NameError: assert True one_dll = DoublyLinkedList([1]) one_dll.remove(1) assert one_dll.head_node is None assert one_dll.tail_node is None assert one_dll.length == 0
class Queue(object): """Queue class. Startrek. Nerds.""" def __init__(self): """Init the Queue.""" self.container = DoublyLinkedList() def enqueue(self, val): """Insert into front position.""" self.container.insert(val) def dequeue(self, val): """Remove item in the list.""" self.container.remove(val) def peek(self): """Check the next node in the queue.""" if self.container.tail is None: return None return self.container.tail.previous.val def size(self): """Return size of container.""" current = self.container.head counter = 1 if current is None: return 0 while current.next is not None: counter += 1 current = current.next return counter
def test_remove(): """Test if selected Node is removed from list.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert('last') my_list.insert('second') my_list.insert('head') my_list.remove('second') assert my_list.head.next.val == 'last'
def test_DLL_rm(mk_dll): zeroth = DoublyLinkedList() with pytest.raises(IndexError): zeroth.remove(3) populated = mk_dll # Remove from the head assert populated.head.data == 0 populated.remove(0) assert populated.head.data == 1 # Remove from the tail assert populated.tail.data == 19 populated.remove(19) assert populated.tail.data == 18 # Remove from the middle current = populated.head for a in range(5): current = current.next the_nexts_value = current.next.data populated.remove(the_nexts_value) assert the_nexts_value + 1 == current.next.data # Try to remove a value that isn't there with pytest.raises(ValueError): populated.remove('not there')
def test_remove_empty(): """Assert remove works on empty list.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() with pytest.raises(AttributeError): my_list.remove('chicken')