Complete the implementation of the link-based set defined in the new class named LinkedSet that implements a set type using linked nodes. Hint: Much of the code in the LinkedBag class from your solution of the linkedbag.py file in Programming Exercise 5.5 can be reused. In the linkedset.py file, complete the following: Complete the accessor methods: isEmpty(), returns true if len(self) == 0, or false otherwise. __len__, returns the number of items in self. __str__, returns the string representation of self. __iter__, supports iteration over a view of self. __add__, returns anew set containing the contents of self and other clone, return a copy of self. __eq__, returns true if self equals other, or false otherwise count, return the numer of instance of item in self. Complete the mutator methods: clear, makes self become empty. add, adds item to self and ignores the item if it’s already in the set. Check array memory and increase it if necessary remove, removes item from self if it's in self. Check precondition and raise KeyError if necessary Search for the node containing the target item, probe will point to the target node, and trailer will point to the one before it, if it exists. Unhook the node to be deleted, either the first one or one thereafter. Decrement logical size To test your program run the test method in the testset.py file. from node import Node class LinkedSet(object):     """A link-based set implementation."""

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
icon
Concept explainers
Question

Complete the implementation of the link-based set defined in the new class named LinkedSet that implements a set type using linked nodes.

Hint: Much of the code in the LinkedBag class from your solution of the linkedbag.py file in Programming Exercise 5.5 can be reused.

In the linkedset.py file, complete the following:

  1. Complete the accessor methods:
    • isEmpty(), returns true if len(self) == 0, or false otherwise.
    • __len__, returns the number of items in self.
    • __str__, returns the string representation of self.
    • __iter__, supports iteration over a view of self.
    • __add__, returns anew set containing the contents of self and other
    • clone, return a copy of self.
    • __eq__, returns true if self equals other, or false otherwise
    • count, return the numer of instance of item in self.
  2. Complete the mutator methods:
    • clear, makes self become empty.
    • add, adds item to self and ignores the item if it’s already in the set.
      • Check array memory and increase it if necessary
    • remove, removes item from self if it's in self.
      • Check precondition and raise KeyError if necessary
      • Search for the node containing the target item, probe will point to the target node, and trailer will point to the one before it, if it exists.
      • Unhook the node to be deleted, either the first one or one thereafter.
      • Decrement logical size

To test your program run the test method in the testset.py file.

from node import Node

class LinkedSet(object):

    """A link-based set implementation."""

 

    # Constructor

    def __init__(self, sourceCollection = None):

        """Sets the initial state of self, which includes the

        contents of sourceCollection, if it's present."""

         # Accessor methods

    def isEmpty(self):

        """Returns True if len(self) == 0, or False otherwise."""

     def __len__(self):

        """-Returns the number of items in self."""

       

 

    def __str__(self):

        """Returns the string representation of self."""

        

 

    def __iter__(self):

        """Supports iteration over a view of self."""

        

 

    def __add__(self, other):

        """Returns a new set containing the contents

        of self and other."""

        

 

    def clone(self):

        """Returns a copy of self."""

        

 

    def __eq__(self, other):

        """Returns True if self equals other,

        or False otherwise."""

        

 

    # Mutator methods

    def clear(self):

        """Makes self become empty."""

        

 

    def add(self, item):

        """Adds item to self."""

        

 

    def remove(self, item):

        """Precondition: item is in self.

        Raises: KeyError if item in not in self.

        Postcondition: item is removed from self."""

        # Check precondition and raise if necessary

        

        # Search for the node containing the target item

        # probe will point to the target node, and trailer

        # will point to the one before it, if it exists

        

        # Unhook the node to be deleted, either the first one or one

        # thereafter

        # Decrement logical size

from arrayset import ArraySet
from linkedset import LinkedSet

def test(setType):
    """Expects a bag type as an argument and runs some tests
    on objects of that type."""
    print("Testing", setType)
    lyst = list(range(1, 11))
    print("The list of items added is:", lyst)
    s = setType(lyst)
    print("Expect the set's string:", s)
    print("Add same items to test for uniqueness:")
    for i in range(1, 11):
        s.add(i)
    print("Expect the set's string:", s)

test(LinkedSet)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Types 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