The Node class and LinkedList class are defined as below: class Node: def __init__(self, data, next = None): self.data =

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

The Node class and LinkedList class are defined as below: class Node: def __init__(self, data, next = None): self.data =

Post by answerhappygod »

The Node Class And Linkedlist Class Are Defined As Below Class Node Def Init Self Data Next None Self Data 1
The Node Class And Linkedlist Class Are Defined As Below Class Node Def Init Self Data Next None Self Data 1 (29.91 KiB) Viewed 53 times
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 remove_from_head (self) which removes the FIRST node from the linked list. This method returns the removed node after the removal. Note: Submit the entire LinkedList class definition in the answer box 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 banana -> cherry -> None apple 2 <class '__main__.LinkedList'> fruit = LinkedList fruit.add('cherry') fruit.add('banana') fruit.add('apple') value = fruit.remove_from_head() print(fruit) print(value) print(fruit.size() print (type (fruit)) 4 5 -> 3 -> 2 -> 1 -> None my_list = LinkedList() my_list.add(1) my_list.add(2) my_list.add(3) my_list.add(4) print(my_list.remove_from_head() my_list.add(5) print(my_list)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply