Question: Ilfrom Node import Node #This class implements the following methods: # add(item) # search(item) # remove(item) # isEmpty() # size() # not in book #
Ilfrom Node import Node #This class implements the following methods: # add(item) # search(item) # remove(item) # isEmpty() # size() # not in book # index(self, item) not in book # insert(pos, item) not in book #Methods implemented for this assignment: append(item) in this solution pop(pos) in this solution #Other methods must be implemented. These include: %23 pop() str_- ### class UnorderedList: def _init__(self): self.head = None #Purpose: add item to front of list #Assumptions: none #Inputs: self: the Unorderedlist object item: object to be added to list #Outputs: updates self.head det add(self, item): temp = Node (item) temp.setNext(self.head) self.head = temp #Purpose: search for item in list #Assumptions: none #Inputs: self: the Unordered List object # item: object being searched for #Outputs: returns True when item found, # otherwise returns False def search(self, item): current = self.head found = Fals while current != None ind in found: if current.getData() == item: found =. True else: current = current.getNext(). return found #Purpose: remove item from list #Assumptions: item is found in the list #Inputs: self: the Unorderedlist object item: object to be removed #Outputs: updates self.head when necessary def remove(self, item): current self.head previous = None found False while net 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()) ## def is Empty(self): return self.head == None #Purpose: determine size of list #Assumptions: none #Inputs: self: the Unorderedlist object #Outputs: returns integer representing size of list def size(self): current = self.head count = 0 while current != None: count = count + 1 current = current.getNext() return count def str__(self): result = current = self.head while eurrent != None: if len(result) > O: result = result + result result + str(current.getData() current = current.getNext() 11 return "Unordered List:" + result #Purpose: determine position of item in list #Assumptions: none #Inputs: self: the Unordered List object # item: object to be located within list #Outputs: When item is found, returns integer representing position of item in list. # When item is not found, returns -1. def index(self, item): current = self.head index = 0 found = False while current != None and not found: if current.getData() == item: found = True else: current = current.getNext() index = index + 1 if not found: index = -1 return index #Purpose: add item to list at pos index position #Assumptions: 0 =>= 0 and pos =>=>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
