0

我是 java 新手,我正在使用链表进行作业。我得到了一个测试器类,我只是将我的代码插入到链表类的特定位置。首先,我面临的问题是我无法打印我的列表并查看我的代码是否正常工作或者我是否取得了任何进展。测试器文件使用“printList(nameOftheList)”,但不打印列表的任何元素。我尝试使用 System.outprintln(nameOftheList) 进行测试,但我得到了我认为是列表的位置而不是列表中的元素。我已经在这个程序上工作了几天,我了解链表,但我的书只涵盖了这么多,我无法应用我在网上找到的任何东西。

如果有人能指出我正确的方向,我将不胜感激。

这里给定的测试人员:

测试者:

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");
      ....

这是我正在学习的课程:

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;
    }

}

}

这是迭代器:

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);
}
4

2 回答 2

1

使用 Iterator 或您的 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();

    }
于 2013-05-06T16:22:50.767 回答
0

在您的构造函数中LinkedListIterator,您将字段设置positionnull并且(除非我遗漏了什么)这永远不会改变。

然后,在 中hasNext(),您检查是否position == null,如果是,则返回 false。

这意味着,如果printList正在使用您的LinkedListIterator,它可能正在检查hasNext()以确定何时停止打印。由于您hasNext()总是返回 false,printList因此只能假设它正在查看一个空列表。

于 2013-05-06T16:23:52.723 回答