Question: If someone can help me write code to implement insert(pos, item) method that would be great thank you. part b of question 3 this is

 If someone can help me write code to implement insert(pos, item)

If someone can help me write code to implement insert(pos, item) method that would be great thank you. part b of question 3

this is my code so far

from Node import Node #This class implements the following methods: # add(item) # search(item) # remove(item) # isEmpty() # size() # The other methods in this class must be implemented. 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 def add(self,item): temp = Node(item) temp.setNext(self.head) self.head = temp # Purpose: search for item in list # Assumptions: none # Inputs: self: the UnorderedList object # item: object being searched for # Outputs: returns True when item found; # otherwise returns False. 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 # 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 if a.isEmpty() == True: return found else: 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()) # Purpose: # Assumptions: # Inputs: self: # Outputs: def index(self, item): current = self.head orientation = 0 while current != None: if current.getData() == item: return orientation else: current = current.getNext() orientation = orientation + 1 def isEmpty(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 current != None: if len(result) > 0: result = result + "," result = result + str(current.getData()) current = current.getNext() return "UnorderedList:" + result def test_remove(): a = UnorderedList a.add('b') print(a) a.remove('b') a.remove('x') print(a)

3. Modify the Unorderedlist class as follows. a. Implement the index(self, item) method. i. When the item is found, this method should return the position of the item in the Unorderedlist. Note: the item at the head of the list is at position zero, the next item is at position one, and so on. ii. When the item is NOT found, this method should return -1. b. Implement the insert(pos, item) method. i. When pos is not valid, return false. Note: the pos value is invalid if it is less than zero or greater than size(). ii. Otherwise, insert the item at the pos position so the item just inserted is at this index position after the insert has completed. Note: the item at the head of the list is at position zero, the next item is at position one, and so on

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!