How to get the remove method to work in this program? please show output for this   class Node:     """ Represents a node in a linked list """     def __init__(self, data):         self.data = data           self.next = None            class LinkedList:     """ A linked list implementation of the List ADT """     def __init__(self):         self._head = None             def add(self, val):         """ Adds a node containing val to the linked list """         self._head = self.rec_add(self._head, val)              def rec_add(self, a_node, val):         """ Adds a node containing val to the linked list """         if a_node is None:             return Node(val)         else:             a_node.next = self.rec_add(a_node.next, val)             return a_node       def insert(self, val, pos):         """ Adds a node containing val to the linked list """         self._head = self.insert_add(self._head, val, pos)              def insert_add(self, a_node, val, pos):         """ Adds a node containing val to the linked list """         if a_node is None:             return Node(val)         if pos == 0:             n = Node(val)             n.next = a_node             return n         else:             a_node.next = self.insert_add(a_node.next, val, pos-1)             return a_node              def remove(self, val):         """ Removes the node containing val from the linked list """         self._head = self.rec_remove(self._head, val)              def rec_remove(self, a_node, val):         """ Removes a node containing val to the linked list """         if a_node is None:             return a_node         elif a_node.val == val:             return a_node.next         else:             a_node.next = self.rec_remove(a_node.next, val)             return a_node       def contains(self, val):         return self.rec_contains(self._head, val)       def rec_contains(self, a_node, val):         """ Removes a node containing val to the linked list """         if a_node is None:             return False         if a_node.data == val:             return True         return self.rec_contains(a_node.next, val)       def is_empty(self):         """ Returns True if the linked list is empty, returns False otherwise """         return self._head is None             def rec_display(self, a_node):         """recursive display method"""         if a_node is None:             return         print(a_node.data, end=" ")         self.rec_display(a_node.next)              def display(self):         """recursive display helper method"""         self.rec_display(self._head)         print()       def reverse(self):         self._head = self.rec_reverse(self._head)       def rec_reverse(self, node):         if node is None or node.next is None:             return node         nextNode = node.next         remaining = self.rec_reverse(node.next)         nextNode.next = node         node.next = None         return remaining       def to_regular_list (self):         result = []         self.rec_to_regular_list(self._head, result)         return result              def rec_to_regular_list (self, node, result):         if node is None:             return result         result.append(node.data)         return self.rec_to_regular_list(node.next, result)   l = LinkedList() l.add(1) l.add(2) l.add(3) l.add(4) l.add(5)   l.display()   l.insert(7, 2) l.display() l.insert(7, 12) l.display()     print(l.is_empty()) print(l.contains(2)) print(l.contains(20))   l.reverse() l.display()   print(l.to_regular_list())

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

How to get the remove method to work in this program? please show output for this

 

class Node:

    """ Represents a node in a linked list """

    def __init__(self, data):

        self.data = data  

        self.next = None  

        

class LinkedList:

    """ A linked list implementation of the List ADT """

    def __init__(self):

        self._head = None   

    

    def add(self, val):

        """ Adds a node containing val to the linked list """

        self._head = self.rec_add(self._head, val)

        

    def rec_add(self, a_node, val):

        """ Adds a node containing val to the linked list """

        if a_node is None:

            return Node(val)

        else:

            a_node.next = self.rec_add(a_node.next, val)

            return a_node

 

    def insert(self, val, pos):

        """ Adds a node containing val to the linked list """

        self._head = self.insert_add(self._head, val, pos)

        

    def insert_add(self, a_node, val, pos):

        """ Adds a node containing val to the linked list """

        if a_node is None:

            return Node(val)

        if pos == 0:

            n = Node(val)

            n.next = a_node

            return n

        else:

            a_node.next = self.insert_add(a_node.next, val, pos-1)

            return a_node

        

    def remove(self, val):

        """ Removes the node containing val from the linked list """

        self._head = self.rec_remove(self._head, val)

        

    def rec_remove(self, a_node, val):

        """ Removes a node containing val to the linked list """

        if a_node is None:

            return a_node

        elif a_node.val == val:

            return a_node.next

        else:

            a_node.next = self.rec_remove(a_node.next, val)

            return a_node

 

    def contains(self, val):

        return self.rec_contains(self._head, val)

 

    def rec_contains(self, a_node, val):

        """ Removes a node containing val to the linked list """

        if a_node is None:

            return False

        if a_node.data == val:

            return True

        return self.rec_contains(a_node.next, val)

 

    def is_empty(self):

        """ Returns True if the linked list is empty, returns False otherwise """

        return self._head is None   

    

    def rec_display(self, a_node):

        """recursive display method"""

        if a_node is None:

            return

        print(a_node.data, end=" ")

        self.rec_display(a_node.next)

        

    def display(self):

        """recursive display helper method"""

        self.rec_display(self._head)

        print()

 

    def reverse(self):

        self._head = self.rec_reverse(self._head)

 

    def rec_reverse(self, node):

        if node is None or node.next is None:

            return node

        nextNode = node.next

        remaining = self.rec_reverse(node.next)

        nextNode.next = node

        node.next = None

        return remaining

 

    def to_regular_list (self):

        result = []

        self.rec_to_regular_list(self._head, result)

        return result

        

    def rec_to_regular_list (self, node, result):

        if node is None:

            return result

        result.append(node.data)

        return self.rec_to_regular_list(node.next, result)

 

l = LinkedList()

l.add(1)

l.add(2)

l.add(3)

l.add(4)

l.add(5)

 

l.display()

 

l.insert(7, 2)

l.display()

l.insert(7, 12)

l.display()

 

 

print(l.is_empty())

print(l.contains(2))

print(l.contains(20))

 

l.reverse()

l.display()

 

print(l.to_regular_list())

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education