Question
two Python question about Cirlist class 1.Extend the Circular LinkedList class CirList by adding the __str__(self) method that returns a reasonable string representation of an
two Python question about Cirlist class
1.Extend the Circular LinkedList class CirList by adding the __str__(self) method that returns a reasonable string representation of an ordered list using space and square brackets.
The implementations of the NodeDLL is provided to you as part of this exercise. You can simply use: NodeDLL(), get_prev(), get_next(), set_prev(), set_next(), as well as get_data() and set_data() as necessary in your function definition. Note: You should include the entire Circular LinkedList class definition in your answer to this question.
For example:
Test | Result |
---|---|
listA = CirList() listA.append(5) listA.append(3) listA.append(3) print(listA) | [5 3 3] |
2.Extend the Circular LinkedList class CirList by adding the __str__(self) method that returns a reasonable string representation of an ordered list using space and square brackets. You are also asked to re-write the append(self, item) and add_before(self, item, old_item) methods such that no duplicate items will be appeared on the ordered list.
The implementations of the NodeDLL is provided to you as part of this exercise. You can simply use: NodeDLL(), get_prev(), get_next(), set_prev(), set_next(), as well as get_data() and set_data() as necessary in your function definition. Note: You should include the entire Circular LinkedList class definition in your answer to this question.
For example:
Test | Result |
---|---|
listA = CirList() listA.append(5) listA.append(3) listA.append(3) print(listA) | [5 3]
|
and here are my codes of node and cirlist :
class NodeDLL:
def __init__(self, init_data, next_node=None, prev_node=None):
self.data = init_data
self.next = next_node
self.prev = prev_node
def get_data(self):
return self.data
def get_prev(self):
return self.prev
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 set_prev(self, new_prev):
self.prev = new_prev
class CirList:
def __init__(self):
head_node = NodeDLL(None)
head_node.next = head_node
head_node.prev = head_node
self.__head = head_node
def append(self, item):
curr = self.__head
new_node = NodeDLL(item, curr, curr.get_prev())
curr.set_prev(new_node)
new_node.get_prev().set_next(new_node)
def add_before(self, item, old_item):
curr = self.__head.next
found = False
while curr.get_data() != None and not found:
if curr.get_data() == old_item:
found = True
else:
curr = curr.get_next()
if found:
new_node = NodeDLL(item, curr, curr.get_prev())
curr.set_prev(new_node)
new_node.get_prev().set_next(new_node)
return found
def remove(self, item):
curr = self.__head.next
found = False
while curr.get_data() != None and not found:
if curr.get_data() == item:
found = True
else:
curr = curr.get_next()
if found:
curr.get_prev().set_next(curr.get_next())
curr.get_next().set_prev(curr.get_prev())
def printall(self):
curr = self.__head.next
while curr.get_data() != None:
print(curr.get_data(), end=" ")
curr = curr.get_next()
print()
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