class BetterQueue(): """A stack class implemented as a linked list. Push, pop, peek, and len are all O(1). str is O(n).""" def __init__(self, initialData=None): """Initializer.""" self.head = None self.size = 0 if initialData != None: self.push(initialData) def __len__(self): """Returns the length of the queue.""" return self.size def __str__(self): """Returns a string representing the queue, in the same style as a list does.""" returnVal = [] node = self.head for i in range(self.size): returnVal.append(node.data) node = node.next return str(returnVal) def push(self, newData): """Add data to the end of the stack.""" if self.head == None: self.head = Link(newData) else: self.head.previous.insertAfter(Link(newData)) self.size += 1 def pop(self): """Return data from the beginning of the stack.""" data = None if self.size > 0: data = self.head.data self.size -= 1 newHead = self.head.next self.head.remove() self.head = newHead if self.size == 0: head = None else: raise IndexError("Queue is empty.") return data def peek(self): """Return data from the beginning of the stack without popping it.""" data = None if self.size > 0: data = self.head.data else: raise IndexError("Queue is empty.") return data def clear(self): """Clears the queue.""" self.size = 0 self.head = None
class LLObj(): """Linked list object""" def __init__(self, data=None): """Initializer.""" self.head = LLLink(data) self.head.next = self.head self.head.previous = self.head def findData(self, data): """Searches the list for the desired data. Returns the FIRST link which holds it, or None.""" curLink = self.head returnVal = None if curLink.data == data: returnVal = curLink else: while curLink.data != data: curLink = curLink.next if curLink.data == data: returnVal = curLink return returnVal def insertBefore(self, newData, oldData=None): """Insert before the chosen data, or at the front of the list if None is passed, or append to the end if no matching data. If inserting before the head node, the new node is set to the head.""" newLink = LLLink(newData) if oldData == None: self.head.insertBefore(newLink) self.head = newLink else: desiredLink = self.findData(oldData) if desiredLink != None: desiredLink.insertBefore(newLink) if desiredLink == self.head: self.head = newLink else: self.head.insertBefore(newLink) def insertAfter(self, newData, oldData=None): """Insert after specified data, or at the end of the list if data == None or not present.""" newLink = LLLink(newData) if oldData == None: self.head.insertBefore(newLink) else: desiredLink = self.findData(oldData) if desiredLink != None: desiredLink.insertAfter(newLink) else: self.head.insertBefore(newLink) def getList(self): """Returns the LinkedList as a standard Python List.""" curLink = self.head returnVal = [] returnVal.append(curLink.data) curLink = curLink.next while curLink != self.head: returnVal.append(curLink.data) curLink = curLink.next return returnVal
def push(self, newData): """Add data to the end of the stack.""" if self.head == None: self.head = Link(newData) else: self.head.previous.insertAfter(Link(newData)) self.size += 1
def insertAfter(self, newData, oldData=None): """Insert after specified data, or at the end of the list if data == None or not present.""" newLink = LLLink(newData) if oldData == None: self.head.insertBefore(newLink) else: desiredLink = self.findData(oldData) if desiredLink != None: desiredLink.insertAfter(newLink) else: self.head.insertBefore(newLink)
def insertBefore(self, newData, oldData=None): """Insert before the chosen data, or at the front of the list if None is passed, or append to the end if no matching data. If inserting before the head node, the new node is set to the head.""" newLink = LLLink(newData) if oldData == None: self.head.insertBefore(newLink) self.head = newLink else: desiredLink = self.findData(oldData) if desiredLink != None: desiredLink.insertBefore(newLink) if desiredLink == self.head: self.head = newLink else: self.head.insertBefore(newLink)
def __init__(self, data=None): """Initializer.""" self.head = LLLink(data) self.head.next = self.head self.head.previous = self.head
def test_link(self): testLink = LLLink("A") self.assertEqual(testLink.next, testLink) self.assertEqual(testLink.previous, testLink) self.assertEqual(testLink.data, "A")