Exemple #1
0
def test_DLL_shift(mk_dll):
    zeroth = DoublyLinkedList()
    with pytest.raises(IndexError):
        zeroth.shift()
    populated = mk_dll
    for x in range(19, -1, -1):
        assert populated.shift() == x
Exemple #2
0
def test_shift_single_item_list():
    """Test pop on single item list."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    my_list.shift()
    assert my_list.head is None
Exemple #3
0
class Queue(object):
    """Creates a Queue class."""

    def __init__(self):
        """Initialization of the queue."""
        self._dll = DoublyLinkedList()
        self._counter = self._dll._counter
        self.head = self._dll.head
        self.tail = self._dll.tail

    def __len__(self):
        """Overwrite Python built in len function."""
        return self._counter

    def length(self):
        """Will use DLL counter for length."""
        return self._dll._counter

    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 a value without removing it."""
        if self.length == 0:
            return None
        return self._dll.tail.data
Exemple #4
0
class Queue(object):
    """Class implementation of queue.

    1.  Enqueue: Add new head node.
    2.  Dequeue: Remove and return tail node value.
    3.  Peek: Display tail node,
    4.  Size: Display queue length.

    """
    def __init__(self, iterable=None):
        """Instatiate Queue."""
        self.dll = DoublyLinkedList(iterable)

    def enqueue(self, contents):
        """Add new head node."""
        self.dll.push(contents)

    def dequeue(self):
        """Remove and return last node value."""
        try:
            old_tail_node_contents = self.dll.shift()
            return old_tail_node_contents
        except IndexError:
            raise IndexError('Queue is already empty.')

    def peek(self):
        """Display but don't remove the contents of tail node."""
        try:
            return self.dll.tail_node.contents
        except AttributeError:
            return None

    def size(self):
        """Return Queue length."""
        return self.dll.length
Exemple #5
0
 def breadth_first(self, starting_point=None):
     """
     This internal method is a generator that will output breadth first
     traversal of a binary tree(left child, right child, parent),
     one value at a time.
     """
     if self.length == 0:
         raise IndexError("You can't breadth-first traverse an empty Tree.")
     from dll import DoublyLinkedList
     unvisited = DoublyLinkedList()
     if starting_point is None:
         starting_point = self.root
     elif self.contains(starting_point) is False:
         raise IndexError('Starting point is not in the Tree.')
     unvisited.push(starting_point)
     visited = []
     while unvisited.size() > 0:
         current = unvisited.shift()
         if current not in visited:
             visited.append(current)
             if current.left:
                 unvisited.push(current.left)
             if current.right:
                 unvisited.push(current.right)
             yield current.val
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
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 implements a simple Python deque data structure."""
    def __init__(self, iterable=None):
        """Init deque, iterate through data if provided as an argument."""
        self.dll = DoublyLinkedList(iterable)

    def append(self, data):
        """Append a node containing data to the head(end) of the deque."""
        self.dll.push(data)

    def appendleft(self, data):
        """Append a node containing data to the tail(front) of the deque."""
        self.dll.append(data)

    def pop(self):
        """Remove a value from the head(end) of the deque and return it.

        Will raise an exception if the deque is empty."""
        try:
            return self.dll.pop()
        except AttributeError:
            raise IndexError("The deque is empty.")

    def popleft(self):
        """Remove a value from the tail(front) of the deque and return it.

        Will raise an exception if the deque is empty."""
        try:
            return self.dll.shift()
        except AttributeError:
            raise IndexError("The deque is empty.")

    def peek(self):
        """Returns the value of the head(end) of the deque.

        Returns None if the deque is empty."""
        if self.dll.head is None:
            return None
        return self.dll.head.data

    def peekleft(self):
        """Returns a value from the tail(front) of the deque.

        Returns None if the deque is empty."""
        if self.dll.tail is None:
            return None
        return self.dll.tail.data

    def size(self):
        """Returns the count of nodes in the queue, 0 if empty."""
        count = 0
        current = self.dll.head
        while current is not None:
            count += 1
            current = current._next
        return count
Exemple #10
0
class Deque(object):
    """Implementation of double-ended queue."""
    def __init__(self, iterable=None):
        """Construct deque."""
        self._dll = DoublyLinkedList(iterable)

    def appendleft(self, val):
        """Add value at the from of deque."""
        self._dll.push(val)

    def append(self, val):
        """Add value to the back of deque."""
        self._dll.append(val)

    def pop(self):
        """Remove value from back of deque."""
        try:
            return self._dll.shift()
        except IndexError:
            raise IndexError('Cannot pop from an empty deque.')

    def popleft(self):
        """Remove value from front of deque."""
        try:
            return self._dll.pop()
        except IndexError:
            raise IndexError('Cannot pop from an empty deque.')

    def peek(self):
        """Return last value of deque."""
        return self.tail.val if self.tail else None

    def peekleft(self):
        """Return first value of deque."""
        return self.head.val if self.head else None

    def size(self):
        """Return size of deque."""
        return self._dll._length

    def __len__(self):
        """Return size of deque."""
        return self.size()

    @property
    def head(self):
        """Return head."""
        return self._dll.head

    @property
    def tail(self):
        """Return tail."""
        return self._dll.tail
class Deque(object):
    """Using a doubly-linked list to build a deque."""
    def __init__(self, iterable=None):
        """Initialize the deque."""
        self.dll = DoublyLinkedList(iterable)
        self.head_node = self.dll.head_node
        self.tail_node = self.dll.tail_node

    def append(self, contents):
        """Add node to this dll."""
        self.dll.append(contents)
        self.head_node = self.dll.head_node
        self.tail_node = self.dll.tail_node

    def append_left(self, contents):
        """Add a node to the left side of this dll."""
        self.dll.push(contents)
        self.head_node = self.dll.head_node
        self.tail_node = self.dll.tail_node

    def pop_left(self):
        """Pop a node from the left side of this dll."""
        popped = self.dll.pop()
        if popped is None:
            raise ValueError
        self.head_node = self.dll.head_node
        return popped

    def pop(self):
        """Pop a node from the right side of this dll."""
        popped = self.dll.shift()
        if popped is None:
            raise ValueError
        self.tail_node = self.dll.tail_node
        return popped

    def peek(self):
        """Look at the tail of the dll."""
        if self.dll.length == 0:
            return None
        return self.dll.tail_node.contents

    def peek_left(self):
        """Look at the head of the dll."""
        if self.dll.length == 0:
            return None
        return self.dll.head_node.contents

    def size(self):
        """Report the length of the queue."""
        return self.dll.length
Exemple #12
0
    def breadth_first_traversal(self, starting_point):
        """Steps through the graph breadth-first.

        Expects a starting point == value of a node in the graph."""
        from dll import DoublyLinkedList
        dll = DoublyLinkedList()
        if self.has_node(starting_point) is False:
            raise IndexError("That starting point is not in the graph.")
        dll.push(starting_point)
        result = []
        while dll.size() > 0:
            working_node = dll.shift()
            if working_node not in result:
                result.append(working_node)
                for node in self.neighbors(working_node):
                    dll.push(node)
        return result
Exemple #13
0
def test_shift_empty():
    """Assert shift works on empty list."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    with pytest.raises(IndexError):
        my_list.shift()