Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Needs help Python 3.6 Data Structure! from node import Node class OrderedList: def __init__(self): self.head = None def isEmpty(self): return self.head == None def length(self):
Needs help Python 3.6 Data Structure!
Design an algorithm for the following methods for the OrderedList class: indexlitem)- returns the position of item in the list. It needs the item and returns the index. Assume the list has at least one item pop(pos)-removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list from node import Node
class OrderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def length(self):
current = self.head
count = 0
while current != None:
count += 1
current = current.getNext()
return count
def __str__(self):
msg = ""
current = self.head
while current != None:
msg += str(current.getData()) + " "
current = current.getNext()
return msg
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
def search(self, item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:
found = True
else:
if current.getData() > item:
stop = True
else:
current = current.getNext()
return found
def add(self, item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started