Commit 31c18c23 authored by Lucianna Osucha's avatar Lucianna Osucha

Major update to RobustLinkedList

RobustLinkedList.insert() now works with list and other RobustLinkedList objects to (optionally) integrate them
many smaller data checks and repair methods in RobustLinkedList
parent 3c13d000
......@@ -6,7 +6,7 @@ import sys
class RobustLinkedList:
class RobustLinkedList:
class ListNode:
def __init__(self, data, n_next=None, n_prev=None):
self.data = data
......@@ -18,55 +18,101 @@ class RobustLinkedList:
def __init__(self, curNode=None, n_head=None, n_foot=None, size=-1):
self.curNode = curNode
self.head = n_head
if curNode != None:
if n_head == None:
n_head = curNode
if n_foot == None:
n_foot = curNode
self.foot = n_foot
if curNode == None:
self.size = 0
else:
self.size = size
self.head = n_head
self.size = size
getSize()
@classmethod
def from_list(cls, m_list):
head = RobustLinkedList.ListNode(m_list[0])
cur = head
for i in range(1, len(m_list) - 1):
for i in range(1, len(m_list)):
cur.next = RobustLinkedList.ListNode(m_list[i], None, cur)
cur = cur.next
return cls(head, head, cur, len(m_list))
foot = RobustLinkedList.ListNode(m_list[-1], None, cur)
cur.next = foot
return cls(head, head, foot, len(m_list))
def getData(self):
return self.curNode.data
def insert(self, data, before=False):
if self.size == 0:
self.curNode = RobustLinkedList.ListNode(data)
self.head = self.curNode
self.foot = self.curNode
else:
if before:
self.curNode.prev = RobustLinkedList.ListNode(data, self.curNode, self.curNode.prev)
if self.curNode == self.head:
self.head = self.curNode.prev
self.curNode = self.head
def getData(self, force=False):
if self.curNode != None:
print("Current node unset, returning to head")
self.curNode = self.head
if force:
if self.curNode == None:
return None
else:
self.curNode = self.curNode.prev
self.curNode.prev.next = self.curNode
return self.curNode.data
else:
self.curNode.next = RobustLinkedList.ListNode(data, self.curNode.next, self.curNode)
if self.curNode == self.foot:
self.foot = self.curNode.next
self.curNode = self.foot
return None
else:
return self.curNode.data
def insert(self, data, priorTo=False, ignoreType=False):
if !ignoreType and isinstance(data, list):
data = from_list(data)
if !ignoreType and isinstance(data, RobustLinkedList):
if self.size == 0:
self.curNode = data.curNode
self.head = data.head
self.foot = data.foot
self.size = data.size
else:
if len(data) == -1:
self.size = -1
else:
self.curNode = self.curNode.next
self.curNode.next.prev = self.curNode
self.size += 1
self.size += len(data)
if priorTo:
data.foot.next = self.curNode
data.head.prev = self.curNode.prev
if self.curNode.prev == None:
self.head = data.head
else:
self.curNode.prev.next = data.head
self.curNode.prev = data.foot
else:
data.head.prev = self.curNode
data.foot.next = self.curNode.next
if self.curNode.next == None:
self.foot = data.foot
else:
self.curNode.next.prev = data.foot
self.curNode.next = data.head
else:
if self.size == 0:
self.curNode = RobustLinkedList.ListNode(data)
self.head = self.curNode
self.foot = self.curNode
else:
if priorTo:
newNode = RobustLinkedList.ListNode(data, self.curNode, self.curNode.prev)
if self.curNode.prev == None:
self.head = newNode
self.curNode.prev = newNode
else:
newNode = RobustLinkedList.ListNode(data, self.curNode.next, self.curNode)
if self.curNode.next == None:
self.foot = newNode
self.curNode.next = newNode
self.curNode = newNode
self.size += 1
return self
def remove(self):
if self.head == self.foot:
self.head = None
......@@ -74,11 +120,12 @@ class RobustLinkedList:
self.curNode = None
self.size = 0
return self
elif self.curNode == self.head:
elif self.curNode.prev == None:
self.head = self.curNode.next
self.curNode = self.head
self.curNode.prev = None
elif self.curNode == self.foot:
elif self.curNode.next == None:
self.foot = self.curNode.prev
self.curNode = self.foot
self.curNode.next = None
......@@ -90,10 +137,14 @@ class RobustLinkedList:
return self
def len(self, force=False):
if force and self.size == -1:
def __len__(self):
return self.getSize()
def getSize(self, forceRecalc=False):
if forceRecalc or size == -1:
print("Recaculating size...")
node = self.head
self.size;
self.size = 0;
while node != None:
self.size += 1
node = node.next
......@@ -110,13 +161,37 @@ class RobustLinkedList:
self.curNode = self.foot
return self
def at_head(self):
if self.curNode.prev == None:
if self.curNode != self.head:
self.head = self.curNode
self.getSize(True)
return True
else:
return False
def at_foot(self):
if self.curNode.next == None:
if self.curNode != self.foot:
self.foot = self.curNode
self.getSize(True)
return True
else:
return False
def next(self):
self.curNode = self.curNode.next
return self
if self.at_foot():
return None
else:
self.curNode = self.curNode.next
return self.getData()
def prev(self):
self.curNode = self.curNode.prev
return self
if self.at_head():
return None
else:
self.curNode = self.curNode.prev
return self.getData()
# Traverse n entries, positive values go footwards and negatives go headwards.
def traverse(self, n = 1):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment