Ejemplo n.º 1
0
class Queue(object):
    """Create Queue Class object."""
    def __init__(self):
        """Queue initiliazation.

        Composed of Doubly-Linked List attributes and methods.
        """
        self._dll = Dll()
        self._length = self._dll._length
        self.head = self._dll.head
        self.tail = self._dll.tail

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

    def enqueue(self, data):
        """Add node to queue at head."""
        self._dll.push(data)

    def dequeue(self):
        """Remove node from queue at tail."""
        return self._dll.shift()

    def peek(self):
        """Display next value that will be removed if dequeued is called."""
        if self.length == 0:
            return None
        return self._dll.tail.data

    def __len__(self):
        """Function overwrites built-in len function to show length."""
        return self.length
Ejemplo n.º 2
0
def test_remove_with_one_node():
    """Test remove if only one node."""
    from dll import Dll
    test_dll = Dll()
    test_dll.push('target')
    test_dll.remove('target')
    assert test_dll.head is None
Ejemplo n.º 3
0
def test_remove_head_of_list_with_multiple_nodes():
    """Test for remove method on list with multiple nodes."""
    from dll import Dll
    test_dll = Dll()
    test_dll.push(11)
    test_dll.push('target')
    check = test_dll.head
    assert test_dll.remove('target') == check
Ejemplo n.º 4
0
def test_pop():
    """Test pop function.

    Should remove first node and return value.
    """
    from dll import Dll
    test_dll = Dll()
    test_dll.push(1)
    assert test_dll.pop() == 1
Ejemplo n.º 5
0
def test_dll_next_push_two():
    """Test list with two nodes.

    Second node should point back to head.
    """
    from dll import Dll
    test_dll = Dll()
    test_dll.push(1)
    test_dll.push(2)
    assert test_dll.head.next_node.prev.data == 2
Ejemplo n.º 6
0
def test_dll_head_push_two():
    """Test list with two nodes.

    Head should point to next node.
    """
    from dll import Dll
    test_dll = Dll()
    test_dll.push(1)
    test_dll.push(2)
    assert test_dll.head.next_node.data == 1
Ejemplo n.º 7
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
Ejemplo n.º 8
0
class Queue(object):
    """Create queue data structure."""
    def __init__(self):
        """Initalize a Queue."""
        self._que = Dll()

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

    def dequeue(self):
        """Remove item from queue."""
        return self._que.shift()

    def peek(self):
        """Returns next value in queue without dequeueing it."""
        if self._que.tail is not None:
            return self._que.tail.value
        return None

    def size(self):
        """Get length of queue."""
        return self._que.__len__()
Ejemplo n.º 9
0
def test_remove_returns_node():
    """Test that correct node returned from search."""
    from dll import Dll
    test_dll = Dll()
    test_dll.push(11)
    test_dll.push('target')
    check = test_dll.head
    test_dll.push(34)
    assert test_dll.remove('target') == check
Ejemplo n.º 10
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__()
Ejemplo n.º 11
0
def test_remove_to_ensure_node_no_longer_exists():
    """Test remove method to ensure node is gone once remove is called."""
    from dll import Dll
    test_dll = Dll()
    test_dll.push(11)
    test_dll.push('target')
    test_dll.push(34)
    test_dll.remove('target')
    with pytest.raises(ValueError):
        test_dll._linkedlist.search('target')
Ejemplo n.º 12
0
def test_dll_push_one():
    """Test pushing to an empty list."""
    from dll import Dll
    test_dll = Dll()
    test_dll.push(1)
    assert test_dll.head.data == 1
Ejemplo n.º 13
0
def test_dll_length_one():
    """Test pushing to an empty list."""
    from dll import Dll
    test_dll = Dll()
    test_dll.push(1)
    assert test_dll._length == 1