Question: Continuing with the LinkedList class implementation, add the following method to the class: class LinkedList: def __init__(self, head = None): self.head = head def add(self,

Continuing with the LinkedList class implementation, add the following method to the class:

class LinkedList:

def __init__(self, head = None): self.head = head

def add(self, item): new_node = Node(item) new_node.set_next(self.head) self.head = new_node

def is_empty(self): if self.head == None: return True else: return False def size(self): count = 0 node = self.head while node != None: count += 1 node = node.get_next() return count def search(self, value): node = self.head while node != None: if node.get_data() == value: return True else: node = node.get_next() return False

  1. remove(): this method is passed a data value as a parameter. The method removes the first Node object in the linked list which contains the specified data value. If the data value does not exist in the linked list, then this method has no effect.

NOTE: You can assume the Node class is provided and you do not need to include the Node class in your answer. Submit your entire LinkedList class.

For example:

Test Result
values = LinkedList() values.add('hello') print(values.size()) values.remove('hello') print(values.size())
1 0 
values = LinkedList() values.add('hello') values.add('world') values.remove('there') print(values.size())
2

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!