Question: *****PYTHON***** previous code class Node: def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self): return self.next def set_data(self, new_data):
*****PYTHON*****
previous code
class Node:
def __init__(self, init_data):
self.data = init_data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = new_data
def set_next(self, new_next):
self.next = new_next
class LinkedListIterator:
def __init__( self, head):
self.current = head
def __next__( self ):
if self.current != None:
item = self.current.get_data()
self.current = self.current.get_next()
return item
else :
raise StopIteration
class OrderedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def size(self):
curr = self.head
count = 0
while curr != None:
count = count + 1
curr = curr.get_next()
return count
def add(self, item):
new_node = Node(item)
new_node.set_next(self.head)
current = self.head
stop = False
while current != None and not stop:
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next()
if current == self.head:
new_node.set_next(self.head)
self.head = new_node
else:
new_node.set_next(current)
previous.set_next(new_node)
def remove(self,item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
def __iter__(self):
return LinkedListIterator(self.head)
Q1:
Extend the OrderedList class by adding the __str__(self) method that returns a reasonable string representation of an ordered list using space and square brackets. You also need to re-write the add(self, item) method such that no duplicate items will be appeared on the ordered list. The implementations of the Node is provided to you as part of this exercise. You can simply use: Node(), get_next(), set_next(), as well as get_data() and set_data() as necessary in your function definition. Note: You should include the entire OrderedList class definition in your answer to this question.
For example:
| Test | Result |
|---|---|
name_list = ["Gill", "Tom", "Eduardo", "Raffaele", "Serena", "Bella"] my_orderedlist = OrderedList() for name in name_list: my_orderedlist.add(name) print(my_orderedlist) | [Bella Eduardo Gill Raffaele Serena Tom] |
Q2:
Extend the OrderedList class by adding the get(self, index) method that takes an integer index as a parameter and returns the data value stored in the node at that index position in an ordered linked list. You also need to re-write the add(self, item) method such that no duplicate items will be appeared on the ordered list. The implementations of the Node is provided to you as part of this exercise. You can simply use: Node(), get_next(), set_next(), as well as get_data() and set_data() as necessary in your function definition. Note: You should include the entire OrderedList class definition in your answer to this question.
For example:
| Test | Result |
|---|---|
name_list = ["Gill", "Tom", "Eduardo", "Raffaele", "Serena", "Bella"] my_orderedlist = OrderedList() for name in name_list: my_orderedlist.add(name) print(my_orderedlist) print(my_orderedlist.get(4), my_orderedlist.get(1)) | [Bella Eduardo Gill Raffaele Serena Tom] Serena Eduardo |
You do not need to write all the codes, just extended code please.
Thank you! :)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
