''' CSCI 204.01 Exam 1 review exercise Divide the students into groups and each group solves one proble, then exchange their ideas or solution. 1. Write a Python function that can remove all nodes with a specific value in a singly linked list. 2. Write a Python function that can count the number of nodes in a circular linked list with a specific data value. 3. Write an operator overloading function __sub__ that removes a node with specified value in a singly linked list. 4. Write a recursive function to compute the length of a doubly linked list. 5. Write a child class with a constructor and string function that inherits information from a parent class. ''' ''' This file contains solutions to P2 ''' class CircularListSingly: """A user defined singly linked circular list""" def __init__(self): self.ref = None # self.ref always points to the end of the list # alternatively we could have it point to the first def __str__(self): """ String function """ s = '[' h = self.ref if h == None: return '[]' else: temp_ref = h h = h.next s += '(ref) ' + str(h.data) while h != temp_ref: h = h.next if h != temp_ref: s += ', ' s += str(h.data) s += ', ' s += str(temp_ref.data) s += ']' return s def insert(self, node): """ Insert a node with value""" if self.ref == None: # empty list self.ref = node node.next = self.ref else: # insert after ref node.next = self.ref.next self.ref.next = node def count_values(self, value): """Count all nodes containing a specific value""" count = 0 node = self.ref if node == None: return 0 while node.next != self.ref: if node.data == value: count += 1 node = node.next return count class ListNode: """ The node class for list notes""" def __init__(self, data): self.data = data self.next = None