The Node class and LinkedList class are defined as below: class Node: def __init__(self, data, next = None): self.data =
Posted: Sat May 14, 2022 2:45 pm
The Node class and LinkedList class are defined as below: class Node: def __init__(self, data, next = None): self.data = data self.next = next def get_data(self): return self.data def get_next(self): return self.next def set_data(self, new_data): self data = new_data def set_next(self, new_next): self.next = new_next class LinkedList: def __init__(self): self. head = None self.count = 0 def add(self, item): new_node - Node(item, self.head) self.head = new_node self.count += 1 def is_empty(self): return self.count == 0 def size(self): return self.count def search(self, item): current - self.head while current: if current. data == item: return True current = current.next return false def remove(self, item): #change this method to update self.count found = False current = self.head previous = None while current is not None and not found: if current.data == item: found - True else: previous = current current = current.next
if found: if previous None: self.head= current.next else: previous.set_next(current.next) self.count -- 1
Continuing on with your LinkedList class implementation, extend the LinkedList class by adding the method __str_(self) which returns a string representation of the object, formatted as in the examples below. The method should return a comma-separated string of the values stored in each node, as shown in the examples below. IMPORTANT: A Node implementation is provided to you as part of this exercise - you should not define your own Node class. Instead, your code can make use of any of the Node ADT data fields and methods. For example: Test Result values = LinkedList) apple -> banana -> cherry -> None values.add('cherry') <class '__main__.LinkedList'> values.add('banana') values.add('apple') print(values) print (type (values)) 3 -> 2 - > 1 -> None values = LinkedList() values.add(1) values.add(2) values.add(3) print(values) values = LinkedList() print(values)
if found: if previous None: self.head= current.next else: previous.set_next(current.next) self.count -- 1
Continuing on with your LinkedList class implementation, extend the LinkedList class by adding the method __str_(self) which returns a string representation of the object, formatted as in the examples below. The method should return a comma-separated string of the values stored in each node, as shown in the examples below. IMPORTANT: A Node implementation is provided to you as part of this exercise - you should not define your own Node class. Instead, your code can make use of any of the Node ADT data fields and methods. For example: Test Result values = LinkedList) apple -> banana -> cherry -> None values.add('cherry') <class '__main__.LinkedList'> values.add('banana') values.add('apple') print(values) print (type (values)) 3 -> 2 - > 1 -> None values = LinkedList() values.add(1) values.add(2) values.add(3) print(values) values = LinkedList() print(values)