Question: ***PYTHON*** Previous code for unorderedlist: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setdata(self,newdata):
***PYTHON***
Previous code for unorderedlist:
class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setdata(self,newdata): self.data = newdata def setNext(self, newnext): self.next = newnext class UnorderedList: def __init__(self): self.head = None def isEmpty(self): return self.head == None def add(self, item): temp = Node(item) temp.setNext(self.head) self.head = temp def size(self): cr = self.head count = 0 while cr != None: count = count + 1 cr = cr.getNext() return count def search(self,item): found = False while cr!= None and not found: if cr.getData() == item: found = True else: cr = cr.getNext() return found def __str__(self): result = "[" node = self.head if node != None: result += str(node.data) node = node.next while node: result += " " + str(node.data) node = node.next result += "]" return result
Q1:
Extend the UnorderedList class by adding the get(self, index) method that takes an integer index as a parameter and returns the data value stored in the node at that index position in a 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. Note: You should include the entire UnorderedList class definition in your answer to this question.
For example:
| Test | Result |
|---|---|
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) print(my_list.get(1)) | 7 |
my_list = UnorderedList() for x in [3,5,4,6,7,8]: my_list.add(x) print(my_list.get(0)) | 8 |
Q2:
Extend the UnorderedList class by adding the append(self, item) method that takes an item as a parameter and inserts the item to the end of 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. Note: You should include the entire UnorderedList class definition in your answer to this question.
For example:
| Test | Result |
|---|---|
my_list = UnorderedList() my_list.append(13) print(my_list) | [13] |
my_list = UnorderedList() my_list.append(13) my_list.append(42) my_list.append(5) print(my_list) | [13 42 5] |
Q3:
Extend the OrderedList class by adding the __str__(self) method that returns a reasonable string representation of an ordered list using space and square brackets. You also need to re-write the add(self, item) method such that no duplicate items will be appeared on the ordered 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. Note: You should include the entire OrderedList class definition in your answer to this question.
For example:
| Test | Result |
|---|---|
name_list = ["Gill", "Tom", "Eduardo", "Raffaele", "Serena", "Bella"] my_orderedlist = OrderedList() for name in name_list: my_orderedlist.add(name) print(my_orderedlist) | [Bella Eduardo Gill Raffaele Serena Tom] |
Q4:
Extend the OrderedList class by adding the get(self, index) method that takes an integer index as a parameter and returns the data value stored in the node at that index position in an ordered linked list. You also need to re-write the add(self, item) method such that no duplicate items will be appeared on the ordered 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. Note: You should include the entire OrderedList class definition in your answer to this question.
For example:
| Test | Result |
|---|---|
name_list = ["Gill", "Tom", "Eduardo", "Raffaele", "Serena", "Bella"] my_orderedlist = OrderedList() for name in name_list: my_orderedlist.add(name) print(my_orderedlist) print(my_orderedlist.get(4), my_orderedlist.get(1)) | [Bella Eduardo Gill Raffaele Serena Tom] Serena Eduardo |
You do not need to write all the codes, just extended code please.
Thank you! :)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
