2013-05-06 113 views
0

我是java的新手,我正在處理一個帶鏈接列表的任務。我被給了一個測試者類,我只是將我的代碼插入鏈表類中的特定位置。首先,我面對的問題是我無法打印我的列表,看看我的代碼是否正在工作,或者我是否正在取得進展。測試程序文件使用「printList(nameOftheList)」,但不打印列表中的任何元素。我嘗試使用System.outprintln(nameOftheList)來測試,但我得到我認爲是列表的位置而不是列表中的元素。我一直在研究這個程序幾天,並且我理解鏈表,但是我的書只涵蓋了很多內容,而且我還沒有能夠應用任何我在網上找到的東西。Java中的循環鏈接列表

如果有人能指出我正確的方向,我將不勝感激。

這裏給定的測試儀:

測試儀:

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

} 

}

這是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); 
} 
+0

閱讀所有類繼承的Object類的'toString()'方法。 – 2013-05-06 16:16:57

+0

使用迭代器打印每個節點的數據。 – Aubin 2013-05-06 16:19:27

回答

1

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

    } 
0

在你的構造爲LinkedListIterator,你外地position設置爲null和(除非我失去了一些東西),這永遠不會改變。

然後,在hasNext()中,檢查是否爲position == null,如果情況如此,則返回false。

這意味着,如果printList正在使用你的LinkedListIterator,它可能檢查hasNext()弄清楚何時停止打印。由於您的hasNext()始終返回false,因此printList只能假定它正在查看空白列表。