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
- 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
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