irm
irm

Reputation: 4331

Circular Linked List in Java

I'm new to java and I am working on an assignment with a linked list. I was given a tester class and I am only to insert my code in specific locations in the linked list class. To begin with, the problem I face is I'm not able to print my list and see if my code is working or if I am making any progress. The tester file uses "printList(nameOftheList)" but doesn't print any elements of the list. I tried using System.outprintln(nameOftheList) to test, but I get what I believe is the location of the list and not the elements on the list. I been working on this program for a couple days now and I understand linked lists, but my book only covers so much and I haven't been able to apply anything of what I have found in the web.

If someone can point me in the right direction I would greatly appreciate it.

Here the given tester:

tester:

public class AddTester
{  
   public static void main(String[] args)
   {  
      LinkedList names = new LinkedList();

      names.addFirst("Tom");
      names.addFirst("Harry");
      names.addFirst("Dick");

      names.add("Romeo");
      printList(names);
      System.out.println("Expected: Dick Harry Tom Romeo");
      ....

Here is the class I'm working on:

import java.util.NoSuchElementException;

/**
A circular linked list.
 */
public class LinkedList
{  
    private Node last;
    // Don't add other instance fields

/** 
Constructs an empty linked list.
 */
public LinkedList()
{  
    last = null;
}

/**
Returns the first element in the linked list.
@return the first element in the linked list
 */
public Object getFirst()
{  
    //. . .
    if (last == null) 
        throw new NoSuchElementException();
    return last.data;
}

/**
Removes the first element in the linked list.
@return the removed element
 */
public Object removeFirst()
{  
    //. . .
    if (last == null)
        throw new NoSuchElementException();
    Object element = last.data;
    last = last.next;
    return element;
}

/**
Adds an element to the front of the linked list.
@param element the element to add
 */
public void addFirst(Object element)
{  
    //. . .
    Node newNode = new Node();
    newNode.data = element;
    newNode.next = last;
    last = newNode;
}

/**
Adds an element to the end of the linked list.
@param element the element to add
 */
public void add(Object element)
{  
    //. . .
    if (last == null)
    {
        addFirst(element);
        //position = last;
    }
    else
    {
        Node newNode = new Node();
        newNode.data = element;
        newNode.next = last.next;
        last.next = newNode;
        last = newNode;
    }
    last = last;
}

/**
Returns an iterator for iterating through this list.
@return an iterator for iterating through this list
 */
public ListIterator listIterator()
{  
    return new LinkedListIterator();
}

private class Node
{  
    public Object data;
    public Node next;
}

private class LinkedListIterator implements ListIterator
{              
    private Node position;
    private Node previous;

    /**
    Constructs an iterator that points to the front
    of the linked list.
     */
    public LinkedListIterator()
    {  
        position = null;
        previous = null;
    }

    /**
    Moves the iterator past the next element.
    @return the traversed element
     */
    public Object next()
    {  
        //. . .
        if (!hasNext())
            throw new NoSuchElementException();
        previous = position; //rmbr for remove

        if (position == null)
            position = last;
        else
            position = position.next;

        return position.data;

    }

    /**
    Tests if there is an element after the iterator 
    position.
    @return true if there is an element after the iterator 
    position
     */
    public boolean hasNext()
    {  
        //. . .
        if (position != null)
            return true;
        else 
            return false;
    }

    /**
    Adds an element before the iterator position
    and moves the iterator past the inserted element.
    @param element the element to add
     */
    public void add(Object element)
    {  
        //. . .
        if (position == null)
        {
            addFirst(element);
            position = last;
        }
    }

    /**
    Removes the last traversed element. This method may
    only be called after a call to the next() method.
     */
    public void remove()
    {  
        //. . .
        if (previous == position)
           throw new IllegalStateException();
         if (position == last)
        {
            removeFirst();
        }
         else
         {
             previous.next = position.next;
        }
        position = previous;
    }

    /**
    Sets the last traversed element to a different 
    value. 
    @param element the element to set
     */
    public void set(Object element)
    {
        if (position == null)
            throw new NoSuchElementException();
        position.data = element;
    }

}

}

This is the iterrator:

public interface ListIterator
{  
   /**
      Moves the iterator past the next element.
      @return the traversed element
   */
   Object next();

   /**
      Tests if there is an element after the iterator 
      position.
      @return true if there is an element after the iterator 
      position
   */
   boolean hasNext();

   /**
      Adds an element before the iterator position
      and moves the iterator past the inserted element.
      @param element the element to add
   */
   void add(Object element);

   /**
      Removes the last traversed element. This method may
      only be called after a call to the next() method.
   */
   void remove();

   /**
      Sets the last traversed element to a different 
      value. 
      @param element the element to set
   */
   void set(Object element);
}

Upvotes: 0

Views: 5085

Answers (2)

killscreen
killscreen

Reputation: 1765

In your constructor for LinkedListIterator, you set the field position to null and (unless I'm missing something) this never changes.

Then, in hasNext(), you check if position == null and return false if that is the case.

This means that, if printList is using your LinkedListIterator, it is probably checking hasNext() to figure out when to stop printing. Since your hasNext() always returns false, printList can only assume it is looking at an empty list.

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93842

Use the Iterator or your LinkedList :

    static String printList(LinkedList names){
            StringBuilder sb = new StringBuilder("Expected : ");
            ListIterator st = names.listIterator();

            while(st.hasNext()){
                //Here implements stuff to get the element of your linkedList and add 
               //it to the StringBuilder
            }
            return sb.toString();

    }

Upvotes: 1

Related Questions