Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use python: 1. Re-implement the remove method so that it works correctly in the

ID: 3805178 • Letter: U

Question

use python:

1. Re-implement the remove method so that it works correctly in the case where the item is not in the list

2. Modify the list class to allow duplicates. Modify all the functions that will be impacted by allowing duplicates

The search() function should return the number of occurrences of the item in the list

The remove() function should remove all the nodes that contain the duplicate item

code you may need

class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None

def getData(self):
return self.data

def getNext(self):
return self.next

def setData(self,newdata):
self.data = newdata

def setNext(self,newnext):
self.next = newnext


class UnorderedList:

def __init__(self):
self.head = None

def isEmpty(self):
return self.head == None

def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp

def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()

return count

def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()

return found

def remove(self,item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()

if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())

Explanation / Answer

For Removing duplicates :-

def remove(self,item):
current = self.head
previous = None
found = False

duplicate = false
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()

if previous.getData == current.getData:

found = true

duplicate = true

if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())