Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedclass 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() def __str__(self): s = "[" cur = self.__head.next; if cur != self.__head or cur != None: s = s + str(cur.get_data()); cur = cur.get_next(); else: return s + "]"; while cur != self.__head: s = s + " " + str(cur.get_data()); cur = cur.get_next(); return s + "]";

2Extend 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 NodeDLL0, get_prev), get_next), set_prev), set_next(), as well as get_data0 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 Result istA CirList0 listA.append(5) listA.append(3) listA.append(3) print(listA) 5 3]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

3. What information do participants need?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago