def test_append_non_empty(): """Assert node gets appended to the end of the lsit.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.insert(4) my_list.append(8) assert my_list.tail.val == 8
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
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
def test_DLL_app(): dbl = DoublyLinkedList() dbl.append('append') assert dbl.head.data == 'append' assert dbl.tail.data == 'append' assert dbl.head.next is None assert dbl.head.prev is None assert dbl.tail.next is None assert dbl.tail.prev is None dbl.append('next') assert dbl.head.data == 'append' assert dbl.tail.data == 'next' assert dbl.tail.prev.next.data == 'next' dbl.append('final') assert dbl.head.data == 'append' assert dbl.tail.data == 'final' assert dbl.tail.prev.next.data == 'final'
def test_append_empty(): """Assert node gets appended to empty list.""" from dll import DoublyLinkedList my_list = DoublyLinkedList() my_list.append(4) assert my_list.tail.val == 4
def mk_dll(request): something = DoublyLinkedList() for a in range(2): for x in range(20): something.append(x) return something