def __init__(self): """ Creates an empty cursor-based list.""" self._header = Node2Way(None) self._trailer = Node2Way(None) self._trailer.setPrevious(self._header) self._header.setNext(self._trailer) self._current = None self._size = 0
def insertBefore(self, item): """Inserts item before the current item, or as the only item if the list is empty. The new item is the current item.""" if self.isEmpty(): self._current = Node2Way(item) self._header.setNext(self._current) self._trailer.setPrevious(self._current) self._current.setNext(self._trailer) self._current.setPrevious(self._header) else: new = Node2Way(item) new.setNext(self._current) new.setPrevious(self._current.getPrevious()) self._current.getPrevious().setNext(new) self._current.setPrevious(new) self._current = new self._size += 1
def replace(self, newItemValue): """Replaces the current item by the newItemValue. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Empty list has no next item") temp = Node2Way(newItemValue) temp.setNext(self._current.getNext()) temp.setPrevious(self._current.getPrevious()) self._current.getNext().setPrevious(temp) self._current.getPrevious().setNext(temp) self._current = temp
def insertBefore(self, item): """Inserts item before the current item, or as the only item if the list is empty. The new item is the current item.""" if self.isEmpty(): newNode = Node2Way(item) newNode.setNext(self._trailer) self._trailer.getPrevious(newNode) newNode.setPrevious(self._header) self._header.setNext(newNode) self._size = 1 self._current = newNode else: temp = self._current.getPrevious() newNode = Node2Way(item) newNode.setNext(self._current) self._current.setPrevious(newNode) newNode.setPrevious(temp) temp.setNext(newNode) self._current = newNode self._size += 1
def addRear(self, item, clock): """ Adds item to the front of the Deque. """ temp = Node2Way(item, clock) temp.setPrevious(self._rear) if self._size == 0: self._front = temp else: self._rear.setNext(temp) self._rear = temp self._size += 1
def insertBefore(self, item): """Inserts item before the current item, or as the only item if the list is empty. The new item is the current item.""" temp = Node2Way(item) if self.isEmpty(): temp.setPrevious(self._header) temp.setNext(self._trailer) self._header.setNext(temp) self._trailer.setPrevious(temp) else: temp.setNext(self._current) self._current.getPrevious().setNext(temp) temp.setPrevious(self._current.getPrevious()) self._current.setPrevious(temp) self._current = temp self._size += 1