class Queue(object): """Make queue object from doubly-linked list object.""" def __init__(self, val=None): """Initialize queue.""" self._container = DoublyLinked(val) def enqueue(self, val): """Push item to queue.""" self._container.append(val) def dequeue(self): """Remove item from head queue and return its value.""" return self._container.pop() def peek(self): """Return next value in queue.""" if self._container.head is self._container._mark: return None else: return self._container.head.get_data() def size(self): """Return the length of the list.""" counter = 0 current_node = self._container.head while current_node is not self._container._mark: counter += 1 current_node = current_node.get_next() return counter
def test_pop(li): """Test pop function.""" from doubly_linked import DoublyLinked new_list = DoublyLinked() new_list.insert(li) assert new_list.pop() == 'one' try: new_list.head.prev_node except AttributeError: assert True
def test_insert(li, result): """Test insert function.""" from doubly_linked import DoublyLinked new_list = DoublyLinked() new_list.insert(li) new_list.pop() assert result == 'one'
def test_remove(): from doubly_linked import DoublyLinked new_list = DoublyLinked() new_list.insert([1, 2, 3]) new_list.remove(3) assert new_list.shift() == 2
def test_shift_empty(li): from doubly_linked import DoublyLinked new_list = DoublyLinked() new_list.insert(li) with pytest.raises(IndexError): new_list.shift()
def test_shift(li, result): from doubly_linked import DoublyLinked new_list = DoublyLinked() new_list.insert(li) assert new_list.shift() == result
def test_append(li, result): from doubly_linked import DoublyLinked new_list = DoublyLinked() new_list.insert(li) new_list.append('four') assert result == new_list.shift()
def test_new_list(): """Test list constructor.""" from doubly_linked import DoublyLinked new_list = DoublyLinked() assert isinstance(new_list, DoublyLinked)
def __init__(self, val=None): """Initialize queue.""" self._container = DoublyLinked(val)