python language implement the pop operation as described on page 137. write test
ID: 3576978 • Letter: P
Question
python language
implement the pop operation as described on page 137. write test program to test this new method.
here is the describtion :
pop() removes and returns the last item in the list. it needs nothing and returns an item. Assume the list has at least one item.
here is the orderd class that you need to work on it.:
====================Ordered class==========================
from node import Node
class OrderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def traverse(self):
current = self.head
while current != None:
print(current.getData())
current = 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)
Explanation / Answer
from node import Node
class OrderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def traverse(self):
current = self.head
while current != None:
print(current.getData())
current = 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)
def pop(self):
current = self.head
while current.getNext() != None:
current = current.getNext()
current.setNext(None)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.