Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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'
Пример #5
0
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
Пример #6
0
def test_shift_empty(li):
    from doubly_linked import DoublyLinked
    new_list = DoublyLinked()
    new_list.insert(li)
    with pytest.raises(IndexError):
        new_list.shift()
Пример #7
0
def test_shift(li, result):
    from doubly_linked import DoublyLinked
    new_list = DoublyLinked()
    new_list.insert(li)
    assert new_list.shift() == result
Пример #8
0
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()
Пример #9
0
def test_new_list():
    """Test list constructor."""
    from doubly_linked import DoublyLinked
    new_list = DoublyLinked()
    assert isinstance(new_list, DoublyLinked)
Пример #10
0
 def __init__(self, val=None):
     """Initialize queue."""
     self._container = DoublyLinked(val)
Пример #11
0
 def __init__(self, val=None):
     """Initialize queue."""
     self._container = DoublyLinked(val)