So I've printed the first node in the linkedset. What I'm trying to do now is to figure out how to print the entire linkedSet1, in this case, since only one node has been added, it should only print the first node, but once more nodes are added it will print the entire linkedSet. Do you have any suggestions on how I could set up so to print the entire linkedSet1? Perhaps I have to iterate through the set using a LinkedIterator (class below) somehow? Hope question is clear, if not let me know and i'll reword.

public class Hw9p1 {

  public static void main (String[] args) {
  
  String s1 = "string1";
  
  LinearNode<String> linearNode1 = new LinearNode<String>(s1);
  
  System.out.println(linearNode1.getElement());  //print node 1
  
  LinkedSet<LinearNode> linkedSet1 = new LinkedSet<LinearNode>();
 
  linkedSet1.add(linearNode1);  //add node 1 to linkedSet
  
  // print linkedSet1... 
  }
}

_________________

public class LinkedIterator<T> implements Iterator<T>
{
   private int count;  // the number of elements in the collection
   private LinearNode<T> current;  // the current position
    
    /*************************************************************
      Sets up this iterator using the specified items.
    *************************************************************/
    public LinkedIterator (LinearNode<T> collection, int size)
    {
       current = collection;
       count = size;
    }
 
    /*************************************************************
      Returns true if this iterator has at least one more element
      to deliver in the iteration.
    *************************************************************/
    public boolean hasNext()
    {
       return (current!= null);
    }
 
    /*************************************************************
      Returns the next element in this iteration. Throws a 
      NoSuchElementException if there are no more elements in 
      the iteration.
    *************************************************************/
    public T next() throws NoSuchElementException
    {
       if (! hasNext())
          throw new NoSuchElementException();
 
       T result = current.getElement();
       current = current.getNext();
       return result;
    }
 
    /*************************************************************
      The remove operation is not supported.
    *************************************************************/
    public void remove() throws UnsupportedOperationException
    {
       throw new UnsupportedOperationException();
    }
}
public class LinkedSet<T> implements SetADT<T>, Iterable<T>
{
  private static Random rand = new Random();
  /*private*/ public int count;  // the current number of elements in the set 
  /*private*/ public LinearNode<T> contents; 

  /******************************************************************
    Creates an empty set.
  ******************************************************************/
  public LinkedSet()
  {
    count = 0;
    contents = null;
  }

  /******************************************************************
    Adds the specified element to this set if it is not already
    present.
  ******************************************************************/
  public void add (T element)
  {
    if (!(contains(element)))
    {
      LinearNode<T> node = new LinearNode<T> (element);
      node.setNext(contents);
      contents = node;
      count++;
    }
  }
  
  /******************************************************************
    Returns true if this set contains the specified target
    element.
  ******************************************************************/
    public boolean contains (T target)
    {
       boolean located = false;
		 LinearNode<T> current = contents;
		 for (int i=0; i<count && !located; i++) {
		   if (current.getElement().equals(target))
			  located = true;
			else
			  current = current.getNext();
		 }
		 return located;
    }
  
 }

files added

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.