def __init__(self, init_value="0"): self._head = TwoWayNode(init_value[0]) self._tail = self._head self.negative = True if int(init_value) < 0 else False self.size = len(init_value) for i in range(1, len(init_value)): self._tail.next = TwoWayNode(int(init_value[i]), self._tail) self._tail = self._tail.next
def append(self, value): newNode = TwoWayNode(value) if self.size == 0: self.tail = newNode self.head = newNode else: self.tail.next = newNode newNode.previous = self.tail self.tail = self.tail.next self.size += 1
def __init__(self, initValue="0"): self.initValue = initValue self._head = TwoWayNode("") # print(len(initValue)) for i in range(0, len(initValue)): # if self._ rest = self._head # print("J") self._head = TwoWayNode(initValue[i]) self._head.next = rest
def make_two_way(singlyll): if singlyll == None: return (None, None) temp = singlyll doublyll = TwoWayNode(temp.data) temp2 = doublyll while temp.next != None: temp = temp.next new_node = TwoWayNode(temp.data, previous=doublyll) doublyll.next = new_node doublyll = doublyll.next return (temp2, doublyll)
def insert(self, item): """Inserts item after the current cursor position.""" newNode = TwoWayNode(item, self._cursor.previous, self._cursor) self._cursor.previous.next = newNode self._cursor.previous = newNode self._size += 1 self._lastItemPos = None
def __init__(self): self._head = TwoWayNode(None, None, None) self._head.next = self._head self._head.previous = self._head self._cursor = self._head self._lastItemPos = None self._size = 0
def __init__(self, sourceCollection=None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" # Uses a circular linked structure with a dummy header node self._head = TwoWayNode() self._head.previous = self._head.next = self._head AbstractList.__init__(self, sourceCollection)
def append(self, data): node = TwoWayNode(data) if self.head == None: self.head = node self.head.next = self.head self.tail = self.head self.size += 1 else: probe = self.head while probe.next != self.head: probe = probe.next probe.next = node node.next = self.head node.previous = probe self.tail = node self.size += 1
def enqueue(self, data): new_node = TwoWayNode(data) if self.count == 0: self.head = new_node self.head.next = self.head self.tail = self.head else: probe = self.head while probe.next != self.head: probe = probe.next probe.next = new_node new_node.next = self.head new_node.previous = probe self.tail = new_node self.count += 1
def makeTwoWay(head): """Creates and returns a doubly linked structure that contains the items in the structure referred to by head.""" if head is None: # Empty structure return None else: # Set the first node twoWayHead = TwoWayNode(head.data) twoWayProbe = twoWayHead probe = head # Set remaining nodes, if any while probe.next != None: newNode = TwoWayNode(probe.next.data, twoWayProbe) twoWayProbe.next = newNode twoWayProbe = newNode probe = probe.next return twoWayHead
def insert(self, i, item): if i < 0: i = 0 elif i > len(self): i = len(self) theNode = self._getNode(i) newNode = TwoWayNode(item, theNode.previous, theNode) theNode.previous.next = newNode theNode.previous = newNode self._size += 1 self.incModCount()
def insert(self, i, item): """Inserts the item at position i.""" if i < 0: i = 0 elif i > len(self): i = len(self) theNode = self.getNode(i) newNode = TwoWayNode(item, theNode.previous, theNode) theNode.previous.next = newNode theNode.previous = newNode self.size += 1 self.incModCount()
def __init__(self, sourceCollection=None) -> None: '''uses a circular structure with a "dummy" sentinel node the empty TwoWayNode instance self.head is the sentinel node... the cursor will set its head NOT to the head node, which is the dummy sentinel, but to the next node, which is the first node containing data (if it exists) when the cursor cycles around to the dummy head sentinel node, the iterators loop terminates. ''' self.head = TwoWayNode() self.head.previous = self.head.next = self.head super().__init__(sourceCollection=sourceCollection)
def insert(self, i, item): """Inserts the item at position i.""" if i < 0: i = 0 elif i > len(self): i = len(self) pointer = self._getNode(i) newNode = TwoWayNode(item, pointer.previous, pointer) pointer.previous.next = newNode pointer.previous = newNode self._size += 1 self.incModCount()
def two_way(): """ Two-way linked example """ # Create a doubly linked structure with one node head = TwoWayNode(1) tail = head # Add four nodes to the end of the doubly linked structure for data in range(2, 6): node = TwoWayNode(data, tail) print('1', node.data, node.next, node.previous) tail.next = node tail = tail.next print('2', node.data, node.next, node.previous) # Print the contents of the linked structure in reverse order probe = tail while probe is not None: print(probe.data) probe = probe.previous
def __add__(self, other): """ self + other """ new_big_integer = "" if self.negative != other.negative: if self.negative: new_big_other_integer = BigInteger(self.to_string()) new_big_other_integer.negative = False return other - self elif other.negative: new_big_other_integer = BigInteger(other.to_string()) new_big_other_integer.negative = False return self - other elif self.negative == other.negative and self.negative: new1 = BigInteger(self.to_string()) new1.negative = False new2 = BigInteger(other.to_string()) new2.negative = False new_big_integer = BigInteger(str(self + other)) new_big_integer.negative = True return new_big_integer elif self.negative == other.negative and not other.negative: if self.size > other.size: while self.size != other.size: other._head.previous = TwoWayNode(0, None, other._head) other._head = other._head.previous other.size += 1 new_big_integer = self._add(other) other._rem_zeros() elif self.size < other.size: while self.size != other.size: self._head.previous = TwoWayNode(0, None, self._head) self._head = self._head.previous self.size += 1 new_big_integer = self._add(other) self._rem_zeros() elif self.size == self.size: new_big_integer = self._add(other) return new_big_integer
def makeTwoWay(listHead): ''' This function takes in the "head" of a singly-linked-list, creates a doubly-linked-list with the same data, and returns the head and tail of the created list. @param listHead: the pointer to the first node of a singly-linked-list ''' probe = listHead newListHead = TwoWayNode(probe.data) #creates first node and point to it tempPointer = newListHead #pointer used to move forward while creating newListTail = None #to point at the end of the list later while probe.next != None and probe.next != listHead: probe = probe.next tempPointer.next = TwoWayNode(probe.data, tempPointer, None) tempPointer = tempPointer.next newListTail = tempPointer #assigns tail pointer to last node tempPointer.next = newListHead #points the "next" pointer of last node to first node newListHead.previous = newListTail #points the "previous" pointer of first node to last node print("Circular Doubly linked list created.") return (newListHead, newListTail)
def replace(self, i, item): '''do not increment mod count with replace method''' if i < 0: i = 0 elif i > len(self): i = len(self) theNode = self.getNode(i) newNode = TwoWayNode(item, theNode.previous, theNode.next) # break link of the node being replaced theNode.previous.next = newNode theNode.next.previous = newNode return theNode.data
def makeTwoWay(linkedList): """This function converts a linked list into a double linked list. Element that is fed will be made into the head element""" cur = linkedList #marks what element the tail will be tail_tracker = None #iterates through linked list and coverts it into a double linked list by adding previous element, tail, and head. #head and tail are accessible by the first element while cur is not None: TwoWayNode(cur) if cur.next is None: cur.tail = cur tail_tracker = cur break holder = cur cur = cur.next TwoWayNode(cur) cur.previous = holder print(cur.data) TwoWayNode(linkedList) linkedList.head = linkedList linkedList.previous = None linkedList.tail = tail_tracker
def insert(self, item): """Preconditions: The list has not been modified except by this iterator's mutators. Adds item to the end if the current position is undefined, or inserts it at that position.""" if self._modCount != self._backingStore.getModCount(): raise AttributeError("List has been modified illegally.") if self._lastItemPos is None: self._backingStore.add(item) else: newNode = TwoWayNode(item, self._lastItemPos.previous, self._lastItemPos) self._lastItemPos.previous.next = newNode self._lastItemPos.previous = newNode self._backingStore.incModCount() self._backingStore._size += 1 self._lastItemPos = None self._modCount += 1
def insert(self, item): """ Preconditions: the current position is defined The list has not been modified except by this iterator's mutators. Inserts the item at current cursor position.""" if self._modCount != self._backingStore.getModCount(): raise AttributeError("List has been modified illegally.") if self._lastMovement == None: #insert to last position theNode = self._backingStore._head else: theNode = self._cursor # this portion ensures a constant time operation b/c # the cursor is already where we want it. newNode = TwoWayNode(item, theNode.previous, theNode) theNode.previous.next = newNode theNode.previous = newNode self._backingStore._size += 1 self._backingStore.incModCount() self._modCount += 1 self._lastMovement = None
def __init__(self, sourceCollection=None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" #Uses a circular struture with a sentinel node self._head = TwoWayNode() self._head.previous = self._head.next = self._head AbstractList.__init__(self, sourceCollection) # Accessor methods def __iter__(self): """Supports iteration over a view of self.""" cursor = self._head.next while cursor != self._head: yield cursor.data cursor = cursor.next # Helper method returns node at position i def _getNode(self, i): """Helper method: returns a pointer to the node at positon i.""" if i == len(self): return self._head if i == len(self) - 1: return self._head.previous probe = self._head.next while i > 0: probe = probe.next i -= 1 return probe #Mutator methods def __getitem__(self, i): """Precondition: 0 <= i < len(self) Returns the item at position i.""" if i < 0 or i >= len(self): raise IndexError("List index out of range") return self._getNode(i).data def __setitem__(self, i, item): """Precondition: 0 <= i < len(self) Replace the item at position i. Raises: IndexError.""" if i < 0 or i >= len(self): raise IndexError("List index out of range") self._getNode(i).data = item def insert(self, i, item): """Inserts the item at position i.""" if i < 0: i = 0 elif i > len(self): i = len(self) theNode = self._getNode(i) newNode = TwoWayNode(item, theNode.previous, theNode) theNode.previous.next = newNode theNode.previous = newNode self._size += 1 self.incModCount() def pop(self, i=None): """Precondition: 0 <= i < len(self) Removes and returns the item at position i. If i ins None, i is given a default of len(self) - 1. Raises: IndexError.""" if i == None: i = len(self) - 1 if i < 0 or i >= len(self): raise IndexError("List index out of range") theNode = self._getNode(i) item = theNode.data theNode.previous.next = theNode.next theNode.next.previous = thenode.previous self._size -= 1 self.incModCount() return item
""" File: test_twowaynode.py Tests the TwoWayNode class. """ from node import TwoWayNode # Create a doubly linked structure with one node head = TwoWayNode(1) tail = head # Add four nodes to the end of the doubly linked structure for data in range(2, 6): node = TwoWayNode(data, tail) print('1', node.data, node.next, node.previous) tail.next = node tail = tail.next print('2', node.data, node.next, node.previous) # Print the contents of the linked structure in reverse order probe = tail while probe is not None: print(probe.data) probe = probe.previous
""" File: two_way_nodes_test.py Tests the TwoWayNode class. """ from node import TwoWayNode # Create a doubly linked structure with one node head = TwoWayNode(1) tail = head # Add four nodes to the end of the doubly linked structure for data in range(2, 6): tail.next = TwoWayNode(data, tail) tail = tail.next # Print the contents of the linked structure in reverse order probe = tailwhile probe != None: print(probe.data) probe = probe.previous
def clear(self): """Makes self become empty.""" self._size = 0 self._head = TwoWayNode() self._head.previous = self._head.next = self._head
def clear(self): self._size = 0 self._head = TwoWayNode() self._head.previous = self._head.next = self._head
def addFirst(self, value): if not self.size: self.head = TwoWayNode(value, self.head) else: self.head = TwoWayNode(value, None, self.head) self.size += 1
def __init__(self, sourceCollection): self._head = TwoWayNode() self._head.previous = self._head.next = self._head AbstractList.__init__(self, sourceCollection)