def test_append_empty():
    dll = Dll()
    dll.append(500)
    assert dll.head.prev is None
    assert dll.head.next_node is None
    assert dll.head.value == 500
    assert dll.head is dll.tail
def test_append_two_nodes():
    """Test append function to add to tail."""
    from dll import Dll
    test_dll = Dll()
    test_dll.append(9)
    test_dll.append('goat')
    assert test_dll.tail.data == 'goat'
def test_shift():
    """Test shift function.

    Should remove last node and return value.
    """
    from dll import Dll
    test_dll = Dll()
    test_dll.append(8)
    assert test_dll.shift() == 8
def test_remove_head():
    dll = Dll()
    dll.append(500)
    dll.append(600)
    dll.append(700)
    dll.remove(500)
    assert dll.head.value == 600
    assert dll.head.next_node is dll.tail
    assert dll.head.prev is None
def test_append():
    dll = Dll()
    dll.append(500)
    first_node = dll.tail
    dll.append(600)
    assert dll.tail.prev is first_node
    assert dll.tail.next_node is None
    assert dll.tail.value == 600
    assert dll.head.next_node is dll.tail
Exemple #6
0
class Deque(object):
    """Create deque class object."""
    def __init__(self):
        """Initialize new deque class object with a front and back."""
        self._dll = Dll()
        self.head = self._dll.head
        self.tail = self._dll.tail
        self._length = self._dll._length

    @property
    def length(self):
        """Use dll length method to return size of the deque."""
        return self._dll._length

    def append(self, data):
        """Use dll append method to add node with data at tail."""
        self._dll.append(data)

    def append_left(self, data):
        """Use dll push method to add node with data at front(head)."""
        self._dll.push(data)

    def pop(self):
        """Use dll shift method to remove node at back(tail)."""
        return self._dll.shift()

    def pop_left(self):
        """Use dll pop method to remove node from front(head)."""
        return self._dll.pop()

    def peek(self):
        """Use dll peek method to view node at back(tail)."""
        if self.length == 0:
            return None
        return self._dll.tail.data

    def peek_left(self):
        """Use dll peek method to view node at front(head)."""
        if self.length == 0:
            return None
        return self._dll.head.data

    def size(self):
        """Function uses build-in len function to show length."""
        return self.length
Exemple #7
0
class Deque(object):
    """Create deque data structure."""
    def __init__(self):
        """Initalize a Deque."""
        self._que = Dll()

    def append(self, val):
        """Add an item to the deque."""
        self._que.push(val)

    def appendleft(self, val):
        """Append value to the front of the deque."""
        self._que.append(val)

    def pop(self):
        """Pop value from end of the deque and returns it."""
        return self._que.pop()

    def popleft(self):
        """Pop value from front of the deque and return it."""
        return self._que.shift()

    def peek(self):
        """Return next value in deque without dequeueing it."""
        if self._que.head is not None:
            return self._que.head.value
        return None

    def peekleft(self):
        """Return value of popleft but leaves value in deque."""
        if self._que.tail is not None:
            return self._que.tail.value
        return None

    def size(self):
        """Get length of deque."""
        return self._que.__len__()
def test_append():
    """Test append function to add to tail."""
    from dll import Dll
    test_dll = Dll()
    test_dll.append(9)
    assert test_dll.tail.data == 9
def test_remove_not_found():
    dll = Dll()
    dll.append(600)
    dll.insert(700)
    with pytest.raises(LookupError):
        dll.remove(500)
def test_shift():
    dll = Dll()
    dll.append(500)
    dll.append(600)
    assert dll.shift() == 600
    assert dll.tail.next_node is None
def test_pop():
    dll = Dll()
    dll.append(500)
    dll.append(600)
    assert dll.pop() == 500
    assert dll.head.prev is None