Two Python Question Cirlist Class 1extend Circular Linkedlist Class Cirlist Adding Str Sel Q29168816

two Python question about Cirlist class

1.Extend the Circular LinkedList classCirList by adding the__str__(self) method that returns a reasonablestring representation of an ordered list using space and squarebrackets.

The implementations of the NodeDLL is provided toyou 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 functiondefinition.     

Note: You should include the entire CircularLinkedList class definition in your answer to thisquestion.   

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 classCirList by adding the__str__(self) method that returns a reasonablestring representation of an ordered list using space and squarebrackets. 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 beappeared on the ordered list.

The implementations of the NodeDLL is provided toyou 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 functiondefinition.     

Note: You should include the entire CircularLinkedList class definition in your answer to thisquestion.   

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

“We Offer Paper Writing Services on all Disciplines, Make an Order Now and we will be Glad to Help”

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.