How to get insert method to accept two values? Right now it accepts one, and ignores the other.  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())

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

How to get insert method to accept two values? Right now it accepts one, and ignores the other. 

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
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY