Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Remove method def remove(self, item): current = self.head if current == None: # empty list, nothing to do return previous = None found = False
Remove method
def remove(self, item): current = self.head if current == None: # empty list, nothing to do return previous = None found = False while not found: #Find the element if current == None: return if current.getData() == item: found = True else: previous = current current = current.getNext() # Case 1: remove 1st element if found == True and previous == None: self.head = current.getNext() # Case 2: remove not 1st element if found == True and previous != None: previous.setNext(current.getNext())In the space below, write a Python method in the OrderedLinkedList class implementation with a head reference discussed in lecture and the textbook called (removeAllItems (self, item)). This method will remove all nodes in the Ordered LinkedList that contain item. To simplify the problem, you may assume the data in the Ordered Linked List nodes are integer values. Hints: . Since this is an OrderedLinkedList, duplicate items will be located right next to each other. Consider the remove method in the LinkedList class discussed in lecture and the textbook. This method can be modified in order to account for removing all duplicate items in the Ordered LinkedList. For reference, the relevant pieces used in the test cases below (including a helper method constructing a string containing the elements in the Ordered LinkedList from front to back for illustration purposes) are: 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, newData): self.data = newData def setNext(self, newNext): self.next = newNext class OrderedLinkedList: def init (self): self.head = None def add(self, item): current = self.head previous = None stop = False while current != None and not stop: if current.getData() > item: stop = True else: previous = current current = current.getNext() temp = Node (item) if previous == None: temp.setNext (self.head) self.head = temp else: temp.setNext(current) previous.setNext(temp) # Helper Method returns string containing # elements in Linked List from front to back def getList(self): current = self.head output = " while current != None: output += str(current.getData()) + current = current.getNext(). # remove end space output = output[:len (output)-1] return output # Other OrderedLinkedList methods ... Your solution should be a complete method definition with appropriate syntax and indentation. If your method is implemented correctly, then the following assert statements should pass: oll = OrderedLinkedList() assert oll.getList() == oll.add(3) oll.add(2) oll.add(1) assert oll.getList() == "1 2 3" oll.add(1) oll.add(2) oll.add(3) assert oll.getList() == "1 1 2 2 3 3" oll.removeAllItems (4) assert oll.getList() "1 1 2 2 3 3" oll.removeAllItems (2) assert oll.getList() "1 1 3 3" oll.removeAllItems (1) assert oll.getList() == "33" oll.removeAllItems (3) assert oll.getList() == == Enter your answer here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started