Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
0
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
Esempio n. 4
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
Esempio n. 5
0
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
Esempio n. 6
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
Esempio n. 7
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