Question
Need help with some python. class UnorderedList: def __init__(self): self.head = None def is_empty(self): return self.head == None def add(self, item): temp = Node(item) temp.set_next(self.head)
Need help with some python.
class UnorderedList: def __init__(self): self.head = None def is_empty(self): return self.head == None def add(self, item): temp = Node(item) temp.set_next(self.head) self.head = temp def size(self): current = self.head count = 0 while current != None: count = count + 1 current = current.get_next() return count def search(self,item): current = self.head found = False while current != None and not found: if current.get_data() == item: found = True else: current = current.get_next() return found def remove(self, item): current = self.head previous = None found = False while current != None and not found: if current.get_data() == item: found = True else: previous = current current = current.get_next() if found: if previous == None: self.head = current.get_next() else: previous.set_next(current.get_next()) def main(): aList = UnorderedList() print("Adding 3, 5, 8, and 11 to the list.") aList.add(3) aList.add(5) aList.add(8) aList.add(11) print("List size:", aList.size()) print("Is 5 in the list? ", aList.search(5)) print("Removing 5 from the list.") aList.remove(5) print("Is 5 in the list? ", aList.search(5)) print("List size:", aList.size()) print("Removing 3 from the list.") aList.remove(3) print("List size:", aList.size()) if __name__ == "__main__": main()
Consider the implementation of an unordered list posted on the course site under UnorderedList.zip Implement the following three list methods as part of the UnorderedList class def insert (self data position) nsert a new node initialized to data at list position indicated by position (where 0 is the position of the first element in the list). If position is outside the list boundaries, the method does nothing. def swap (self posi pos2) Swap the value of the node at position pos1 with that at position pos2. The method should do nothing if either position is invalid def value repeats (self) Returns True if the list contains two or more repeating consecutive values, False otherwise. For instance, calling the function with the list 8,3, 3, 4, 5, 6 would return True since 3 is repeating, while 8, 3, 4, 3, 5, 6 would return False write a test program (as a main function) to demonstrate that the methods work correctly. Be sure to test for invalid inputs. Follow the documentation style in UnorderedList.py by including a comment block at the beginning of each method and comments within each method to explain the code Consider the implementation of an unordered list posted on the course site under UnorderedList.zip Implement the following three list methods as part of the UnorderedList class def insert (self data position) nsert a new node initialized to data at list position indicated by position (where 0 is the position of the first element in the list). If position is outside the list boundaries, the method does nothing. def swap (self posi pos2) Swap the value of the node at position pos1 with that at position pos2. The method should do nothing if either position is invalid def value repeats (self) Returns True if the list contains two or more repeating consecutive values, False otherwise. For instance, calling the function with the list 8,3, 3, 4, 5, 6 would return True since 3 is repeating, while 8, 3, 4, 3, 5, 6 would return False write a test program (as a main function) to demonstrate that the methods work correctly. Be sure to test for invalid inputs. Follow the documentation style in UnorderedList.py by including a comment block at the beginning of each method and comments within each method to explain the codeStep 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