def test_pop_non_empty(): """Assert first node gets removed and returned.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.insert(5) assert my_list.pop() == 5
def test_pop_single_item_list(): """Test pop on single item list.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.pop() assert my_list.head is None
def test_insert_on_non_emtpy_list(): """Assert insert on non-empty list works.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.insert('Diana') assert my_list.head.val == 'Diana'
def test_append_non_empty(): """Assert node gets appended to the end of the lsit.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.append(8) assert my_list.tail.val == 8
def test_assert_previous(): """Assert second node points to its previous.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert('Daniel') my_list.insert('Diana') assert my_list.head.val == 'Diana'
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_shift(): """Assert shift works on non empty list.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(1) my_list.insert(2) my_list.insert(3) assert my_list.shift() == 1
def test_tail(): """Assert the last node is the tail.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.insert(5) my_list.insert(6) assert my_list.tail.val == 4
class Deque(object): """Deque class.""" def __init__(self): """Initilizer of deque class.""" self.container = DoublyLinkedList() def append_left(self, val): """Append val to the head of the list.""" self.container.insert(val) def append(self, val): """Append val to the tail of the list.""" self.container.append(val) def pop(self): """Return and remove head from the list.""" self.container.shift() def pop_left(self): """Remove head of deque and return that value.""" self.container.pop() def peek(self): """Check the next node in the deque.""" if self.container.tail is None: return None return self.container.tail.val def peek_left(self): """Return the tail of the deque.""" if self.container.head is None: return None return self.container.head.val def size(self): """Return the size of the deque.""" 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
class Deque(object): """Deque class.""" def __init__(self): """Initilizer of deque class.""" self.container = DoublyLinkedList() def append_left(self, val): """Append val to the head of the list.""" self.container.insert(val) def append(self, val): """Append val to the tail of the list.""" self.container.append(val) def pop(self): """Return and remove head from the list.""" self.container.shift() def pop_left(self): """Remove head of deque and return that value.""" self.container.pop() def peek(self): """Check the next node in the deque.""" if self.container.tail is None: return None return self.container.tail.val def peek_left(self): """Return the tail of the deque.""" if self.container.head is None: return None return self.container.head.val def size(self): """Return the size of the deque.""" 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_ins(): dbl = DoublyLinkedList() # First case with an empty list dbl.insert('insert') assert dbl.head.data == 'insert' assert dbl.tail.data == 'insert' assert dbl.head.next is None assert dbl.head.prev is None assert dbl.tail.next is None assert dbl.tail.next is None dbl.insert('next') assert dbl.head.data == 'next' # Test for updating of prev in in the second item assert dbl.head.next.prev.data == 'next' assert dbl.tail.data == 'insert' dbl.insert('final') assert dbl.head.data == 'final' assert dbl.head.next.prev.data == 'final' assert dbl.tail.data == 'insert'
def test_insert_on_empty_list(): """Assert insert works.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) assert my_list.head.val == 4