Question
For the LinkedSet class 1. Implement the contains() method 2. Implement the remove() method public class LinkedSet { private class Node { Node next; Node
For the LinkedSet class
1. Implement the contains() method
2. Implement the remove() method
public class LinkedSet
Node(T e, Node prv, Node nxt) { data = e; prev = prv; next = nxt; } }
private Node head = null; private Node tail = null; private int length = 0;
public LinkedSet() { }
public LinkedSet(T [] initialList) { for (T item : initialList) { add(item); } }
/* * Adds the specified element to this set if it is not already present. * More formally, adds the specified element e to this set if the set * contains no element e2 such that (e==null ? e2==null : e.equals(e2)). * If this set already contains the element, the call leaves the set * unchanged and returns false. In combination with the restriction on * constructors, this ensures that sets never contain duplicate elements. * Parameters: * e - element to be added to this set * Returns: * true if this set did not already contain the specified element */ public boolean add(T e) { if (contains(e)) { return false; }
Node n = new Node(e, tail, null); if (tail == null) { head = n; } else { tail.next = n; } tail = n; length++; return true; }
/* * Removes the specified element from this set if it is present. * More formally, removes an element e such that * (o==null ? e==null : o.equals(e)), if this set contains such * an element. Returns true if this set contained the element (or * equivalently, if this set changed as a result of the call). * (This set will not contain the element once the call returns.) * Parameters: * o - object to be removed from this set, if present * Returns: * true if this set contained the specified element */ public boolean remove(T e) { /* * YOUR CODE HERE */ return false; }
/* * Returns true if this set contains the specified element. More formally, * returns true if and only if this set contains an element e such that * (o==null ? e==null : o.equals(e)). * Parameters: * o - element whose presence in this set is to be tested * Returns: * true if this set contains the specified element */ public boolean contains(T e) { /* * YOUR CODE HERE */ return false; }
/* * Returns the number of elements in this set (its cardinality). * Returns: * the number of elements in this set (its cardinality) */ public int size() { return length; } } please add comments to help me learn, thank ypu so much.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started