def test_pop_single_item_list(): """Test pop on single item list.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.pop() assert my_list.head is None
def test_DLL_pop(mk_dll): zeroth = DoublyLinkedList() with pytest.raises(IndexError): zeroth.pop() populated = mk_dll for twice in range(2): for x in range(20): assert populated.pop() == x
def test_pop_non_empty(): """Assert first node gets removed and returned.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.insert(5) assert my_list.pop() == 5
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 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
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
class Queue(object): """First in first out queue structure.""" def __init__(self, iterable=None): """Construct queue.""" try: self._dll = DoublyLinkedList(iterable) except ValueError: raise ValueError("Queue optional parameter must be iterable.") def enqueue(self, val): """Add value to the queue.""" self._dll.append(val) def dequeue(self): """Remove item from the queue and returns an error if queue empty.""" try: return self._dll.pop() except: raise IndexError('Cannot dequeue from an empty queue.') def peek(self): """Return the next value in the queue without dequeueing it. If the.""" """queue is empty, returns None.""" return self.head.val if self.head else None def size(self): """Return the size of the queue, if empty return 0.""" return self._dll._length def clear(self): """Empty queue.""" self._dll.head = None self._dll.tail = None self._dll._length = 0 def __len__(self): """Return length of queue.""" return self.size() @property def head(self): """Read only head property.""" return self._dll.head @property def tail(self): """Read only tail property.""" return self._dll.tail
def depth_first_traversal(self, starting_point): """Steps through the graph depth-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.pop() if working_node not in result: result.append(working_node) for node in self.neighbors(working_node): dll.push(node) return result
def test_pop_empty(): from dll import DoublyLinkedList my_list = DoublyLinkedList() with pytest.raises(IndexError): my_list.pop()