Section 1: The Sorted LinkedList class add(self, item) adds a new Node with value=item to the list making sure that the ascending order is preserved. It needs the item and returns nothing but modifies the linked list. The items in the list might not be unique, but you can assume every value will be numerical (int and float). You are not allowed to traverse the linked list more than once (only one loop required). Input (excluding self) int or float item A numerical value that represents the value of a Node object Examples: >>> x=SortedLinked List() >>> x. add(8.76) >>> x. add (7) >>> x. add(3) >>> x. add(-6) >>> x. add (58) >>> x. add(33) >>> x. add(1) >>> x. add(-88) >>> print(x) Head: Node(-88) Tail:Node(58) List:-88 -> -6 -> 1 -> 3 -> 7 -> 8.76 -> 33 -> 58 replicate(self) Returns a new SortedLinkedList object where each element of the linked list appears its node's value number of times (3 -> 1 results in 3 -> 3 -> 3 -> 1). Negative numbers and floats are
Please help (in python) – Refer to questions attached in the images, and the starter code below (I cannot upload page 3 with example outputs for removeDuplicates(self)). Do not change the function names or given starter code in your script. You are not allowed to use any other data structures for the purposes of manipulating/sorting elements, nor may you use any modules from the Python to insert and remove elements from the list. You are not allowed to swap data from the nodes when adding the node to be sorted. Traversing the linked list and updating ‘next’ references are the only required and acceptable operations. You are not allowed to use any kind of built-in sorting method or import any other libraries. If you are unable to complete a function, use the pass statement to avoid syntax errors.
Starter code -
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return "Node({})".format(self.value)
__repr__ = __str__
class SortedLinkedList:
'''
>>> x=SortedLinkedList()
>>> x.add(8.76)
>>> x.add(1)
>>> x.add(1)
>>> x.add(1)
>>> x.add(5)
>>> x.add(3)
>>> x.add(-7.5)
>>> x.add(4)
>>> x.add(9.78)
>>> x.add(4)
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> 8.76 -> 9.78
>>> x.replicate()
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> -7.5 -> 1 -> 1 -> 1 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 4 ->
4 -> 4 -> 4 -> 5 -> 5 -> 5 -> 5 -> 5 -> 8.76 -> 8.76 -> 9.78 -> 9.78
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> 8.76 -> 9.78
>>> x.removeDuplicates()
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> 1 -> 3 -> 4 -> 5 -> 8.76 -> 9.78
'''
def __init__(self): # You are not allowed to modify the constructor
self.head=None
self.tail=None
def __str__(self): # You are not allowed to modify this method
temp=self.head
out=[]
while temp:
temp=temp.next
out=' -> '.join(out)
return f'Head:{self.head}\nTail:{self.tail}\nList:{out}'
__repr__=__str__
def isEmpty(self):
return self.head == None
def __len__(self):
count=0
current=self.head
while current:
current=current.next
count+=1
return count
def add(self, value):
# --- YOUR CODE STARTS HERE
pass
def replicate(self):
# --- YOUR CODE STARTS HERE
pass
def removeDuplicates(self):
# --- YOUR CODE STARTS HERE
pass
Trending now
This is a popular solution!
Step by step
Solved in 2 steps