In: Computer Science
Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When the input list is empty, the output Python list should be empty as well.Example: When the input unordered list is [1,2,3], the output Python list should be [1,2,3] .
Use the following codes:
class Node:
def __init__(self, data):
self.data = data
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, data):
self.data = data
def setNext(self, node):
self.next = node
class UnorderedList:
def __init__(self):
self.head = None
def add(self, data):
newNode = Node(data)
newNode.setNext(self.head)
self.head = newNode
def length(self):
cur = self.head
count = 0
while cur:
cur = cur.getNext()
count += 1
return count
def isEmpty(self):
return self.head == None
def search(self, data):
cur = self.head
while cur:
if cur.getData() == data:
return True
cur = cur.getNext()
return False
def remove(self, data):
if self.head.getData() == data:
self.head = self.head.getNext()
else:
cur = self.head
while cur.getNext() and cur.getNext().getData() != data:
cur = cur.getNext()
if cur.getNext():
cur.setNext(cur.getNext().getNext())
--------------------------------------------------------------------------------
"""
2. transform(lst)
Transform an unordered list into a Python list
Input: an (possibly empty) unordered list
Output: a Python list
"""
def transform(lst):
# Write your code here
Program
Text Format:
class Node: def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self, data): self.data = data def setNext(self, node): self.next = node class UnorderedList: def __init__(self): self.head = None def add(self, data): newNode = Node(data) newNode.setNext(self.head) self.head = newNode def length(self): cur = self.head count = 0 while cur: cur = cur.getNext() count += 1 return count def isEmpty(self): return self.head == None def search(self, data): cur = self.head while cur: if cur.getData() == data: return True cur = cur.getNext() return False def remove(self, data): if self.head.getData() == data: self.head = self.head.getNext() else: cur = self.head while cur.getNext() and cur.getNext().getData() != data: cur = cur.getNext() if cur.getNext(): cur.setNext(cur.getNext().getNext()) # -------------------------------------------------------------------------------- """ 2. transform(lst) Transform an unordered list into a Python list Input: an (possibly empty) unordered list Output: a Python list """
def transform(lst): olist = [] node = lst.head for i in range(lst.length()): tm = node.getData() olist.append(tm) node = node.getNext() return olist
lst = UnorderedList() lst.add(7) lst.add(5) lst.add(3) olist = transform(lst) print(olist)
Output:
[3, 5, 7]
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Thank you.