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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 - 

class Node:
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:
 
 
 
 
 
 
 
out.append(str(temp.value))
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
Section 1: The Sorted Linked List 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 elemet 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
repeated only once immediately after that node. For 0, the node is added, but not repeated.
Method returns None if the list is empty. Hint: Using the add method from above could be very
useful here!
Output
SortedLinkedList A new linked list object that repeats values without disturbing the original
list
None
None keyword is returned if the original list is empty
Transcribed Image Text:Section 1: The Sorted Linked List 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 elemet 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 repeated only once immediately after that node. For 0, the node is added, but not repeated. Method returns None if the list is empty. Hint: Using the add method from above could be very useful here! Output SortedLinkedList A new linked list object that repeats values without disturbing the original list None None keyword is returned if the original list is empty
Section 1: The SortedLinked List class
Examples:
>>> x=SortedLinkedList()
>>> x. add(4)
>>> x. replicate()
He ad: Node (4)
Tail:Node (4)
List:4 -> 4 -> 4 - > 4
>>> x. add(-23)
>>> x. add(2)
>>> x. add(1)
>>> x. add(20.8)
>>> X
He ad: Node (-23)
Tail:Node (20.8)
List:-23 -> 1 -> 2 -> 4 -> 20.8
>>> x. replicate()
He ad : Node (-23)
Tail:Node (20.8)
List:-23 -> -23 -> 1 -> 2 -> 2 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8
>>> x. add(-1)
>>> x. add(0)
>>> x. add(3)
>>> x. replicate()
Head: Node (-23)
Tail:Node ( 20.8)
List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 ->
20.8 -> 20.8
>>> X
Head: Node (-23)
Tail:Node (20.8)
List:-23 - > -1 -> 0 -> 1 -> 2 -> 3 -> 4 -> 20.8
>>> x.add(2)
>>> x.replicate()
He ad: Node (-23)
Tail:Node ( 20.8)
List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 - > 2 -> 2 - > 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4
-> 4 -> 20.8 -> 20.8
removeDuplicates(self)
Removes any duplicate nodes from the list, so it modifies the original list. This method must
traverse the list only once. It modifies the original list.
Output
None
Nothing is returned when the method completes the work
Transcribed Image Text:Section 1: The SortedLinked List class Examples: >>> x=SortedLinkedList() >>> x. add(4) >>> x. replicate() He ad: Node (4) Tail:Node (4) List:4 -> 4 -> 4 - > 4 >>> x. add(-23) >>> x. add(2) >>> x. add(1) >>> x. add(20.8) >>> X He ad: Node (-23) Tail:Node (20.8) List:-23 -> 1 -> 2 -> 4 -> 20.8 >>> x. replicate() He ad : Node (-23) Tail:Node (20.8) List:-23 -> -23 -> 1 -> 2 -> 2 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8 >>> x. add(-1) >>> x. add(0) >>> x. add(3) >>> x. replicate() Head: Node (-23) Tail:Node ( 20.8) List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8 >>> X Head: Node (-23) Tail:Node (20.8) List:-23 - > -1 -> 0 -> 1 -> 2 -> 3 -> 4 -> 20.8 >>> x.add(2) >>> x.replicate() He ad: Node (-23) Tail:Node ( 20.8) List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 - > 2 -> 2 - > 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8 removeDuplicates(self) Removes any duplicate nodes from the list, so it modifies the original list. This method must traverse the list only once. It modifies the original list. Output None Nothing is returned when the method completes the work
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education