Using the classes LinearNode.java, LinkedSet.java and SetADT.java implement a simple (console or graphical) program that creates a linked list with integers and performs the following operations: 1. Can add an integer (using the add method) 2. Can remove a specific integer (using the removeRandom method) 3. Can remove a random integer (using the removeRandom method) 4. The program can display the resulting set of Integers (by implementing the toString method) B
Code 1: LinearNode.java //************************************************************ // LinearNode.java Authors: Lewis/Chase // // Represents a node in a linked list. //************************************************************
public class LinearNode<E> { private LinearNode<E> next; private E element;
//--------------------------------------------------------- // Creates an empty node. //--------------------------------------------------------- public LinearNode() { next = null; element = null; }
//--------------------------------------------------------- // Creates a node storing the specified element. //--------------------------------------------------------- public LinearNode (E elem) { next = null; element = elem; }
//--------------------------------------------------------- // Returns the node that follows this one. //--------------------------------------------------------- public LinearNode<E> getNext() { return next; }
//--------------------------------------------------------- // Sets the node that follows this one. //--------------------------------------------------------- public void setNext (LinearNode<E> node) { next = node; }
//--------------------------------------------------------- // Returns the element stored in this node. //--------------------------------------------------------- public E getElement() { return element; }
//--------------------------------------------------------- // Sets the element stored in this node. //--------------------------------------------------------- public void setElement (E elem) { element = elem; } }
LinkedSet.java
//******************************************************************** // LinkedSet.java Authors: Lewis/Chase // // Represents a linked implementation of a set. //********************************************************************
pa ckage jss2;
import jss2.exceptions.*; import java.util.*;
public class LinkedSet<T> implements SetADT<T> { private static Random rand = new Random();
private int count; // the current number of elements in the set
private LinearNode<T> contents;
//----------------------------------------------------------------- // Creates an empty set. //----------------------------------------------------------------- public LinkedSet() { count = 0; contents = null; }
//----------------------------------------------------------------- // Adds the specified element to the set if it's not already // present. //----------------------------------------------------------------- public void add (T element) { if (!(contains(element))) { LinearNode<T> node = new LinearNode<T> (element); node.setNext(contents); contents = node; count++; } }
//----------------------------------------------------------------- // Adds the contents of the parameter to this set. //----------------------------------------------------------------- public void addAll (SetADT<T> set) { Iterator<T> scan = set.iterator();
while (scan.hasNext()) add (scan.next()); }
//----------------------------------------------------------------- // Removes a random element from the set and returns it. Throws // an EmptySetException if the set is empty. //----------------------------------------------------------------- public T removeRandom() throws EmptySetException { LinearNode<T> previous, current; T result = null;
if (isEmpty()) throw new EmptySetException();
int choice = rand.nextInt(count) + 1;
if (choice == 1) { result = contents.getElement(); contents = contents.getNext(); } else { previous = contents; for (int skip=2; skip < choice; skip++) previous = previous.getNext(); current = previous.getNext(); result = current.getElement(); previous.setNext(current.getNext()); }
count--;
return result; }
//----------------------------------------------------------------- // Removes the specified element from the set and returns it. // Throws an EmptySetException if the set is empty and a // NoSuchElemetnException if the target is not in the set. //----------------------------------------------------------------- public T remove (T target) throws EmptySetException, NoSuchElementException { boolean found = false; LinearNode<T> previous, current; T result = null;
if (isEmpty()) throw new EmptySetException();
if (contents.getElement().equals(target)) { result = contents.getElement(); contents = contents.getNext(); } else { previous = contents; current = contents.getNext(); for (int look=0; look < count && !found; look++) if (current.getElement().equals(target)) found = true; else { previous = current; current = current.getNext(); }
if (!found) throw new NoSuchElementException();
result = current.getElement(); previous.setNext(current.getNext()); }
count--;
return result; }
//----------------------------------------------------------------- // Returns a new set that is the union of this set and the // parameter. //----------------------------------------------------------------- public SetADT<T> union (SetADT<T> set) { LinkedSet<T> both = new LinkedSet<T>();
LinearNode<T> current = contents;
while (current != null) { both.add (current.getElement()); current = current.getNext(); }
Iterator<T> scan = set.iterator(); while (scan.hasNext()) both.add (scan.next());
return both; }
//----------------------------------------------------------------- // Returns true if this set contains the specified target // element. //----------------------------------------------------------------- public boolean contains (T target) { boolean found = false;
LinearNode<T> current = contents;
for (int look=0; look < count && !found; look++) if (current.getElement().equals(target)) found = true; else current = current.getNext();
return found; }
//----------------------------------------------------------------- // Returns true if this set contains exactly the same elements // as the parameter. //----------------------------------------------------------------- public boolean equals (SetADT<T> set) { boolean result = false; LinkedSet<T> temp1 = new LinkedSet<T>(); LinkedSet<T> temp2 = new LinkedSet<T>(); T obj;
if (size() == set.size()) { temp1.addAll(this); temp2.addAll(set);
Iterator<T> scan = set.iterator();
while (scan.hasNext()) { obj = scan.next(); if (temp1.contains(obj)) { temp1.remove(obj); temp2.remove(obj); }
} result = (temp1.isEmpty() && temp2.isEmpty()); }
return result; }
//----------------------------------------------------------------- // Returns true if this set is empty and false otherwise. //----------------------------------------------------------------- public boolean isEmpty() { return (size() == 0); }
//----------------------------------------------------------------- // Returns the number of elements currently in this set. //----------------------------------------------------------------- public int size() { return count; }
//----------------------------------------------------------------- // Returns an iterator for the elements currently in this set. //----------------------------------------------------------------- public Iterator<T> iterator() { return new LinkedIterator<T> (contents, count); }
//----------------------------------------------------------------- // Returns a string representation of this set. //----------------------------------------------------------------- public String toString() { String result = "";
LinearNode<T> current = contents;
while (current != null) { result += current.getElement().toString() + "\n"; current = current.getNext(); }
return result; } }
Class 3 setADt.java
/***************************************************************** *** // SetADT.java Authors: Lewis/Chase // // Defines the interface to a set collection. //***************************************************************** ***
package jss2;
import java.util.Iterator;
public interface SetADT<T> { // Adds one element to this set, ignoring duplicates public void add (T element);
// Removes and returns a random element from this set public T removeRandom ();
// Removes and returns the specified element from this set public T remove (T element);
// Returns the union of this set and the parameter public SetADT<T> union (SetADT<T> set);
// Returns true if this set contains the parameter public boolean contains (T target);
// Returns true if this set and the parameter contain exactly // the same elements public boolean equals (SetADT<T> set);
// Returns true if this set contains no elements public boolean isEmpty();
// Returns the number of elements in this set public int size();
// Returns an iterator for the elements in this set public Iterator<T> iterator();
// Returns a string representation of this set public String toString(); }
I'm just responding to let you know that the odds of you getting an answer are next to 0. I can guarantee you that there are several users of this forum who are capable of giving you an answer on Questions about LinkedLists/LinkedSets (I can think of a few off the top of my head) but the pain of going through unformatted code will probably outweigh the urge to assist you.
If you're going to cheat on a homework assignment, wouldn't it be better to just quietly ask a classmate for the answer, rather than broadcast the act over the internet? Seriously. Or Google? Did you even try Google?