class Queue(object): """Establish class for Queue. Methods composed from DblLinkedList class.""" def __init__(self, seq): """Initialize Queue, based on Double Linked List constructor.""" self._queue = DblLinkedList(seq) def enqueue(self, val): """Add val to end of the queue.""" self._queue.append(val) def dequeue(self): """Remove and return first item in queue.""" try: return self._queue.pop() except IndexError: print("IndexError caught from DBLL") raise IndexError("Queue is empty.") def peek(self): """Return value of the first object in the queue.""" if self._queue.head: return self._queue.head.val else: return None def size(self): """Loop through the queue and count the number of items.""" cur_node = self._queue.head count = 0 while cur_node: cur_node = cur_node.next_node count += 1 return count
def test_append(seq, append_val, head_val, tail_val, tail_prev_val): """Test the append method of DblLinkedList class.""" from dbl_linked_list import DblLinkedList test_list = DblLinkedList(seq) test_list.append(append_val) assert test_list.head.val == head_val assert test_list.tail.val == tail_val assert test_list.tail.prev_node.val == tail_prev_val
def test_insert(seq, insert_val, head_val, head_next_val, tail_val): """Test the insert method of DblLinkedList class.""" from dbl_linked_list import DblLinkedList test_list = DblLinkedList(seq) test_list.insert(insert_val) assert test_list.head.val == head_val assert test_list.head.next_node.val == head_next_val assert test_list.tail.val == tail_val
def test_pop(seq, popped_val, head_val): """Test the pop method of DblLinkedList class.""" from dbl_linked_list import DblLinkedList test_list = DblLinkedList(seq) if not seq: with pytest.raises(IndexError): test_list.pop() else: assert test_list.pop() == popped_val assert test_list.head.val == head_val
def test_remove(seq, rm_value, expected): """Test the remove method of DblLinkedList class.""" from dbl_linked_list import DblLinkedList test_list = DblLinkedList(seq) if rm_value not in seq: with pytest.raises(ValueError): test_list.remove(rm_value) else: test_list.remove(rm_value) assert display_dll(test_list) == expected
class Queue(object): """The Queue Data structure is a compoisition of a Double Linked List. Methods: enqueue(val): Add a new node to the end (tail) of the queue. dequeue(): Remove the node at the head of the queue. peek(): Return value at head of queue. size(): Return size of queue. """ def __init__(self, maybe_an_iterable=None): """Initialize Queue as a DblLinkedList-esque object.""" try: self._container = DblLinkedList(maybe_an_iterable[::-1]) except TypeError: self._container = DblLinkedList(maybe_an_iterable) def enqueue(self, value): """Add a new node with given value to the end (tail) of the queue.""" self._container.append(value) def dequeue(self): """Remove the node at the head of the queue and return the value.""" try: return self._container.pop() except IndexError: raise IndexError("Cannot dequeue from empty queue.") def peek(self): """Return the value at the head of the queue. None if empty.""" try: return self._container.head.value except AttributeError: return None def size(self): """Return the size of the queue.""" return self._container._size def __len__(self): """Allow use of len() function.""" return self.size()
class Deque(object): """This is the Deque data structure. Methods: append(): Append a Node with given value to the tail. appendleft(): Push a Node with given value to the head. pop(): Remove the node from the head, return the value. popleft(): Remove the node from the tail, return the value. peek(): Return the value from the tail node. peekleft(): Return the value from the head node. """ def __init__(self, maybe_an_iterable=None): """Initialize a deque data structure.""" self._container = DblLinkedList(maybe_an_iterable) def append(self, val): """Append a node with given value to the tail.""" self._container.append(val) def appendleft(self, val): """Append a node with given value to the head.""" self._container.push(val) def pop(self): """Remove the node from the tail and return the value.""" try: return self._container.shift() except IndexError: raise IndexError("Cannot pop from empty Deque.") def popleft(self): """Remove the node from the head and return the value.""" try: return self._container.pop() except IndexError: raise IndexError("Cannot popleft from empty Deque.") def peek(self): """Give the value for the tail node.""" try: return self._container.tail.value except AttributeError: return None def peekleft(self): """Give the value for the head node.""" try: return self._container.head.value except AttributeError: return None def size(self): """Return the length of the deque.""" return self._container._size def __len__(self): """Call the size method.""" return self.size()
class Deque(object): """Establish class for deque. Methods composed from dbl_linked_list.""" def __init__(self, seq=None): """Initialize deque.""" self._deque = DblLinkedList(seq) def append(self, val): """Append value to end of deque.""" self._deque.append(val) def appendleft(self, val): """Append value to front of deque.""" self._deque.insert(val) def pop(self): """Remove last item from deque and return its value.""" try: return self._deque.shift() except IndexError: raise IndexError("Cannot pop from end of an empty deque.") def popleft(self): """Remove first item from deque and return its value.""" try: return self._deque.pop() except IndexError: raise IndexError("Cannot pop from head of an empty deque.") def peek(self): """Return value of last item in deque.""" try: return self._deque.tail.val except AttributeError: return None def peekleft(self): """Return value of first item in deque.""" try: return self._deque.head.val except AttributeError: return None def size(self): """Calculate number of items in the deque and return it.""" cur_node = self._deque.head counter = 0 while cur_node: counter += 1 cur_node = cur_node.next_node return counter
def __init__(self, seq=None): """Initialize deque.""" self._deque = DblLinkedList(seq)
def test_shift(seq, shift_val, tail_val): """Test the shift method of DblLinkedList class.""" from dbl_linked_list import DblLinkedList test_list = DblLinkedList(seq) assert test_list.shift() == shift_val assert test_list.tail.val == tail_val
def dbl_ll_2(): from dbl_linked_list import DblLinkedList new_dll = DblLinkedList(TEST_ITER_2) return new_dll
def dbl_ll_one_value(): from dbl_linked_list import DblLinkedList new_dll = DblLinkedList(TEST_ITER[-1]) return new_dll
def dbl_ll_empty(): from dbl_linked_list import DblLinkedList new_dll = DblLinkedList() return new_dll
def __init__(self, maybe_an_iterable=None): """Initialize Queue as a DblLinkedList-esque object.""" try: self._container = DblLinkedList(maybe_an_iterable[::-1]) except TypeError: self._container = DblLinkedList(maybe_an_iterable)
def __init__(self, seq): """Initialize Queue, based on Double Linked List constructor.""" self._queue = DblLinkedList(seq)
def __init__(self, maybe_an_iterable=None): """Initialize a deque data structure.""" self._container = DblLinkedList(maybe_an_iterable)