Question

I have an application that is supposed to be an example of displaying students waiting to enroll in a course (using a single linked list). However, I'm unsure of how to test it. More specifically, how to print the data values of all nodes to test my add/remove methods. My code so far is below, and please let me know if I haven't given enough information. Thanks in advance for any help!

public class StudentRegistration<E>
{
    private static class Node<E> 
    {

        /** The data value. */
        private E data;
        /** The link */
        private Node<E> next = null;

        /**
         * Construct a node with the given data value and link
         * @param data - The data value 
         * @param next - The link
         */
        public Node(E data, Node<E> next) 
        {
            this.data = data;
            this.next = next;
        }

        /**
         * Construct a node with the given data value
         * @param data - The data value 
         */
        public Node(E data) 
        {
            this(data, null);
        }

        public E getData()
        {
          return data;
        }
    }
    /** A reference to the head of the list */
    private Node<E> head = null;
    /** The size of the list */
    private int size = 0;

    /** Helper methods */
    /** Remove the first occurance of element item.
    @param item the item to be removed
    @return true if item is found and removed; otherwise, return false.
*/
  public boolean remove(E item)
  {
    if(item != null)
    {
      remove(item);
      return true;
    }

    else
      return false; 
  } 

 /** Insert an item as the first item of the list.
   * @param item The item to be inserted
   */
  public void addFirst(E item) 
  {
      head = new Node<E>(item, head);
      size++;
  }

   /**
     * Remove the first node from the list
     * @returns The removed node's data or null if the list is empty
     */
  private E removeFirst() 
  {
      Node<E> temp = head;
      if (head != null) 
      {
          head = head.next;
      }
      if (temp != null) 
      {
          size--;
          return temp.data;
      } else 
      {
          return null;
      }
  }
  /** Add a node to the end of the list
    *@param value The data for the new node
    */
  public void addLast(E value)
    {
      // location for new value
      Node<E> temp = new Node<E>(value,null);
      if (head != null)
      {
          // pointer to possible tail
          Node<E> finger = head;
          while (finger.next != null)
          {
              finger = finger.next;
          }
          temp = finger.next;
      } else head = temp;
  }
}  
Was it helpful?

Solution

Override toString() method in your custom class StudentRegistration.

For example:

@Override
public String toString(){
   StringBuilder sb = new StringBuilder();
   sb.append("[");
   Node<T> aux = this.head;
   boolean isFirst = true;
   while(aux != null){
       if(!isFirst){
          sb.append(",");
       }
       isFirst = false;
       sb.append(aux.data.toString());
       aux=aux.next;
   }
  return sb.append("]").toString();

}

Then in your main you just have to call

System.out.println(studentRegistrationObject);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top