class Queue(object): """Queue class that contains a DLL.""" def __init__(self, iter=None): self._container = DLL() if iter: for val in iter: self._container.append(val) def enqueue(self, val): """Adds a value to the end of the queue.""" self._container.append(val) def dequeue(self): """Removes and returns value from front of queue.""" temp_value = self._container.pop() if temp_value is not None: return temp_value raise ValueError def peek(self): """Returns the next value in the queue without dequeing it.""" try: return self._container.head.val except AttributeError: return None def size(self): """Returns the size of the queue.""" return self._container.size()
class Deque(object): """Structure for values in a deque, a double-ended queue.""" def __init__(self, iterable=None): """Construct a ne deque.""" self._values = DLL(iterable) @property def front(self): """Get the node at front of the deque.""" return self._values.tail @property def end(self): """Get the node at end of the deque.""" return self._values.head def __len__(self): """Overwrite the default return for len function.""" return len(self._values) def append(self, val): """Add a value to the end of the deque.""" self._values.push(val) def appendleft(self, val): """Add a value to the front of the deque.""" self._values.append(val) def pop(self): """Remove the value from the end of the deque and return it.""" try: return self._values.pop() except IndexError: raise IndexError('Cannot pop from empty deque.') def popleft(self): """Remove the value from the front of the deque and return it.""" try: return self._values.shift() except IndexError: raise IndexError('Cannot popleft from empty deque.') def peek(self): """Get the value from the end of the deque without removing it.""" if self.end: return self.end.val def peekleft(self): """Get the value from the front of the deque without removing it.""" if self.front: return self.front.val def size(self): """Get the size of the deque.""" return len(self)
def parenthetics(string): """The parenthetics module checks for open, broken, or balanced sets.""" matches = DLL() for char in string: if char == "(": matches.append(char) if char == ")": if matches.head is None or matches.pop() != "(": return -1 if matches.head is None: return 0 else: return 1
class Deque(object): """Python Implementation of Deque Data Structure""" def __init__(self, iter=None): """Constructor Function for Deque.""" self.container = DLL() if iter: for val in iter: self.container.append(val) def append(self, val): """Add val to the end of the Deque.""" self.container.append(val) def appendleft(self, val): """Add val to the start of the Deque.""" self.container.insert(val) def pop(self): """Remove and return the value at the end of the Deque.""" rtn_val = self.container.shift() if rtn_val is not None: return rtn_val raise AttributeError def popleft(self): """Remove and the return the value at the front of the Deque.""" rtn_val = self.container.pop() if rtn_val is not None: return rtn_val raise AttributeError def peek(self): """Return the next value that would be popped.""" try: return self.container.tail.val except AttributeError: return None def peekleft(self): """Return the next value that would popleft.""" try: return self.container.head.val except AttributeError: return None def size(self): """Return the size of the Deque.""" return self.container.size()
def findPairsWithSumX(head, tail, x): result = [] if head is None or tail is None: return result while head != tail and tail.next != head: if head.data + tail.data == x: result.append((head.data, tail.data) head, tail = head.next, tail.prev elif head.data + tail.data < x: head = head.next else: tail = tail.prev return result s = [1, 2, 4, 5, 6, 8, 9] dll = DLL() for i in s: dll.append(i) dll.printList() print findPairsWithSumX(dll.head, dll.tail, 7)
while current and current.next: if current.next.data > newNode.data: break current = current.next if current is None: dll.head = newNode elif current.next is None: current.next = newNode newNode.prev = current dll.tail = newNode else: newNode.next = current.next newNode.prev = current newNode.next.prev = newNode current.next = newNode dll = DLL() for i in range(7): dll.append(2 * i + 1) dll.printList() sortedInsert(dll, 0) dll.printList() sortedInsert(dll, 4) dll.printList() sortedInsert(dll, 16) dll.printList() sortedInsert(dll, 0) dll.printList() sortedInsert(dll, 7) dll.printList()
dll.head = None dll.tail = None return if k == 1: node = dll.head dll.head = node.next node.next = None dll.head.prev = None else: current = dll.head while current and k > 1: current = current.next k -= 1 if current is None: return elif current.next is None: dll.tail = current current.prev.next = None current.prev = None else: current.prev.next = current.next current.next.prev = current.prev dll = DLL() for i in range(8): dll.append(i) dll.printList() deleteAt(dll, 8) print dll.tail dll.printList()
tail.next.prev = tail tail = tail.next if a: tail.next = a tail.next.prev = tail if b: tail.next = b tail.next.prev = tail return head a = DLL() b = DLL() s = [1, 3, 5, 7] for i in s: a.append(i) b.append(i + 1) a.printList() b.printList() c = DLL() c.head = merge(a.head, b.head) c.printList() current = c.head while current: if current.prev: print current.prev.data, print current.data, if current.next: print current.next.data, print current = current.next
def three_dll(): l = DLL() l.append(10) l.append('String') l.append([]) return l
def two_dll(): l = DLL() l.append(10) l.append('String') return l
def one_dll(): l = DLL() l.append(10) return l