''' 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 solution to P4 ''' class DLinkedList: """ A user defined doubly linked list""" def __init__(self): self.head = None self.tail = None def __str__(self): """ String function """ s = '[' h = self.head while h != None: s += str(h.data) h = h.next if h != None: s += ', ' s += ']' return s def __len__(self): """Count and return the length""" return self.count_length(self.head) def count_length(self, node): """Count the nodes recursively""" if node == None: return 0 else: return 1 + self.count_length(node.next) def insert(self, value): """ Insert a value 'data'""" node = DListNode(value) if self.head == None: # empty list self.head = node self.tail = node else: # insert after tail self.tail.next = node node.prev = self.tail self.tail = node class DListNode: """ The node class for list notes""" def __init__(self, data): self.data = data self.next = None self.prev = None