Question: Python Issue 1.Extend the UnorderedList class by adding the pop(self, index) method that that takes an integer index as a parameter and removes the item
Python Issue
1.Extend the UnorderedList class by adding the pop(self, index) method that that takes an integer index as a parameter and removes the item at the index position in the unordered linked list. The index should be in the range [0..length-1]. If the method is called without any parameter, the last element will be removed from the unordered linked list.
The implementations of the Node is provided to you as part of this exercise. You can simply use: Node(), get_next(), set_next(), as well as get_data() and set_data() as necessary in your function definition.
For example:
| Test | Result |
|---|---|
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) print(my_list.pop()) | 3 |
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) print(my_list.pop(0)) | 8 |
2.**Extend the UnorderedList class by adding the insert(self, index, item) method that that takes an integer index as a parameter and inserts the new item at the index position in the unordered linked list. The index should be in the range [0..length-1].
The implementations of the Node is provided to you as part of this exercise. You can simply use: Node(), get_next(), set_next(), as well as get_data() and set_data() as necessary in your function definition.
For example:
| Test | Result |
|---|---|
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) my_list.insert(0, 9) print(my_list) | 9 8 7 6 4 5 3 |
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) my_list.insert(6, 2) print(my_list) | 8 7 6 4 5 3 2 |
class Node: def __init__(self, init_data): self.data = init_data self.next = None 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 def __str__(self): return self.data
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
