Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on a linked list set project with an iterator. This is the first ti

ID: 3754530 • Letter: I

Question

I am working on a linked list set project with an iterator.

This is the first time I have to deal with stuff like generic and iterator. It is really confusing to me. Wish someone could help.

I have two java docs (LinkedNodeIterator.java and LinkedSet.java) that I have to work on and another two supportive java docs (LinkedNode.java and set.java).

I would rate your answers if they are helpful. Thanks.

Here are the java docs:

LinkedNodeIterator.java:

package sets;

import java.util.Iterator;

import java.util.NoSuchElementException;

class LinkedNodeIterator implements Iterator {

// TODO (1) define data variables

private LinkedNode Node;

  

// Constructors

public LinkedNodeIterator(LinkedNode head) {

// TODO (2) choose appropriate parameters and do the initialization

this.Node = head;

}

@Override

public boolean hasNext() {

// TODO (3)

}

@Override

public E next() {

// TODO (4)

}

@Override

public void remove() {

// Nothing to change for this method

throw new UnsupportedOperationException();

}

}

LinkedSet.java:

package sets;

import java.util.Iterator;

/**

* A LinkedList-based implementation of Set

*/

/********************************************************

* NOTE: Before you start, check the Set interface in

* Set.java for detailed description of each method.

*******************************************************/

  

/********************************************************

* NOTE: for this project you must use linked lists

* implemented by yourself. You are NOT ALLOWED to use

* Java arrays of any type, or any Collection-based class

* such as ArrayList, Vector etc. You will receive a 0

* if you use any of them.

*******************************************************/

/********************************************************

* NOTE: you are allowed to add new methods if necessary,

* but do NOT add new files (as they will be ignored).

*******************************************************/

public class LinkedSet implements Set {

private LinkedNode head = null;

// Constructors

public LinkedSet() {

}

public LinkedSet(E e) {

this.head = new LinkedNode(e, null);

}

private LinkedSet(LinkedNode head) {

this.head = head;

}

@Override

public int size() {

// TODO (1)

return 0;

}

@Override

public boolean isEmpty() {

// TODO (2)

return false;

}

@Override

public Iterator iterator() {

return new LinkedNodeIterator(this.head);

}

@Override

public boolean contains(Object o) {

// TODO (3)

return false;

}

@Override

public boolean isSubset(Set that) {

// TODO (4)

return false;

}

@Override

public boolean isSuperset(Set that) {

// TODO (5)

return false;

}

@Override

public Set adjoin(E e) {

// TODO (6)

return null;

}

@Override

public Set union(Set that) {

// TODO (7)

return null;

}

@Override

public Set intersect(Set that) {

// TODO (8)

return null;

}

@Override

public Set subtract(Set that) {

// TODO (9)

return null;

}

@Override

public Set remove(E e) {

// TODO (10)

return null;

}

@Override

@SuppressWarnings("unchecked")

public boolean equals(Object o) {

if (! (o instanceof Set)) {

return false;

}

Set that = (Set)o;

return this.isSubset(that) && that.isSubset(this);

}

@Override

public int hashCode() {

int result = 0;

for (E e : this) {

result += e.hashCode();

}

return result;

}

}

LinkedNode.java:

package sets;

public class LinkedNode {

E data;

LinkedNode next;

public LinkedNode(E data, LinkedNode next) {

this.data = data;

this.next = next;

}

public E getData() {

return data;

}

public LinkedNode getNext() {

return next;

}

}

Set.java(Basically contains describtions about how to write every method in LinkedSet.java):

package sets;

import java.util.Iterator;

/**
* A collection that contains no duplicate elements. More formally,
* a set contains no pair of elements e1 and
* e2 such that e1.equals(e2) is true, and
* and at most one null element (for the simple reason that if it had
* contained two null elements, they would have been duplicates).
*/
public interface Set extends Iterable {

/**
* Returns the number of elements in this set (its cardinality).
*
* @return the number of elements in this set (its cardinality).
*/
int size();

/**
* Returns true if this set contains no elements.
*
* @return true if this set contains no elements.
*/
boolean isEmpty();

/**
* Returns an iterator over the elements in this set.
*
* @return an iterator over the elements in this set
*/
Iterator iterator();

/**
* 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)).
*
* @param o the element whose presence in this set is to be tested
* @return true if this set contains the specified element
*/
boolean contains(Object o);

/**
* Returns true if this set is a subset (or equal to) the given
* set. More formally, returns true if and only if every member of
* 'this' set is also contained in that set.
*
* @param that the set to check whether this is a subset of
* @return true if every member of this set is also a
* member of that
*/
boolean isSubset(Set that);

/**
* Returns true if this set is a superset (or equal to) the given
* set. This is equivalent to asking whether that set is a subset
* of this set.
*
* @param that the set to check whether this is a superset of
* @return true if every member of that is also a member of
* this set
*/
boolean isSuperset(Set that);

/**
* Adjoin a set with a new element. If the specified element is
* already in the set then returns the original Set. Otherwise,
* returns a new Set containing all of the elements of this set and
* also the specified new element.  
*
* @param e the element to be added
* @return a Set containing all of the elements of this set that
* also contains e
*/
Set adjoin(E e);

/**
* Returns a new Set that is the union of this
* Set and that Set. This is intended to be the
* mathematical operation of union on sets.
*
* @param other the other Set to take the union with
* @return a Set containing the union of the two
* input Sets
*/
Set union(Set other);

/**
* Returns a new Set that is the intersection of
* this Set and that Set. This is
* intended to be the mathematical operation of intersection on
* sets.
*
* @param other the other Set to take the
* intersection with
* @return a Set containing the intersection of
* the two input Sets
*/
Set intersect(Set other);

/**
* Returns a new Set that is the difference of
* this Set and that Set, i.e.,
* this - that. This is intended to be the mathematical
* operation of difference on sets. Formally, the returned set
* contains every element e of this that
* is NOT an element of that.
*
* @param that the Set to subtract from this one
* @return a Set containing the difference of
* the two input Sets
*/
Set subtract(Set other);

/**
* Remove an element from a set. If this set does not contain the
* specified element then return the original set. Otherwise,
* return a new Set that is the same as this
* except that it does not contain the specified element.
*
* @param e the element to be removed
* @return a set similar to this but without
* e
*/
Set remove(E e);

/**
* Compares the specified object with this set for equality. Returns
* true if the specified object is also a set, the two sets have the
* same size, and every member of the specified set is contained in
* this set (or equivalently, every member of this set is contained
* in the specified set).
*
* @param o object to be compared for equality with this set
* @return true if the specified object is equal to this set
* @see java.lang.Object#hashCode()
*/
boolean equals(Object o);

/**
* Returns the hash code value for this set. The hash code of a set
* is defined to be the sum of the hash codes of the elements in the
* set, where the hash code of a null element is defined to be
* zero. This ensures that s1.equals(s2) implies that
* s1.hashCode() == s2.hashCode() for any two sets
* s1 and s2, as
* required by the general contract of Object.hashCode().
*
* @return the hash code value for this set
* @see java.lang.Object#equals(Object)
* @see equals(Object)
* @see java.lang.Object#hashCode()
*/
int hashCode();
}

Explanation / Answer

I have written the code for this so you can understand the login of how this works. Due to less time I am not considering complexity of the program that is totally based on the logic that you will implement but Now things are working based on the parameters.

I have written the code with title as their file names. Just copy this code and run this on Main Class and then see the functionality how this works.

If there will be any doubt then you can comment and I will help you in that part too.

Due to less time I am not able to complete the remove and substract method of this LinkedSet class.

You only need to implement the logic for like

To remove the element you can either rearrange the List by deleting that element from that Node or

You can make new List without that element if complexity is not the issue.

if there is any issue still with this code . Do let me know in comment I will surely help.

Main.java

Set.java

LinkedSet.java

LinkedNodeIterator.java

Linked Node:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote