Question
I need help with this code; I will start with the instructions: Write a LinkedList class that has recursive implementations of the add and remove
I need help with this code; I will start with the instructions:
Write a LinkedList class that has recursive implementations of the add and remove methods described in the Exploration. It should also have recursive implementations of the contains, insert, and reverse methods described in the exercises. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds).
It should have a recursive method named to_plain_list that takes no parameters and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list.
It should have a method named get_head that takes no parameters and returns the Node object (not the value inside it) that is at the _head of the linked list.
The head data member of the LinkedList class, as well as the data and next members for the Node class must be private and have getters and setters defined.
All the methods should have the arguments in the same order as you saw in the Lesson. You may use default arguments and/or helper functions.
Your recursive functions must not:
- use any loops
- use any variables declared outside of the function
- use any mutable default arguments
Here's an example of how a recursive version of the display() method from the lesson could be written:
def rec_display(self, a_node): """recursive display method""" if a_node is None: return print(a_node.get_data(), end=" ") self.rec_display(a_node.get_next()) def display(self): """recursive display helper method""" self.rec_display(self.get_head())
All your classes must be in a single file named: LinkedList.py
Here is what I have so far:
class Node: """ Represents a node in a linked list """ def __init__(self, data): self._data = data self._next = None def get_data(self): return self._data def set_data(self, newData): self._data = newData def get_next(self): return self._next def set_next(self, newNode): self._next = newNode class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self._head = None def get_head(self): """ Returns head of node """ return self._head def rec_add(self, val, a_node): """ A recursive method that adds a value to the linked list """ # works for an empty list OR because it is recursive; will add the value when it reaches end of the list if a_node is None: current = self._head current._set_data(a_node, val) return self._head else: self.rec_add(val, a_node.get_next()) return self._head def add(self, val): """ Helper method for recursive add method """ self._head = self.rec_add(val, self._head)
I am not close to being done based on what the instructions say, but so far I cannot get the add method to work. I keep getting this popping up: AttributeError: 'NoneType' object has no attribute '_set_data'
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