2012-11-10 38 views
-2

我有这个链表我的类和idk真的如何测试它,你认为它的工作原理或你能解释我如何测试它?这个链接列表类看起来是否有效?

public class List { 

private int size; 
private Node head; 
private Node current; 
private Node prev; 
private Node temp; 

/** 
* Creates an empty list. 
* @pre none 
* @post and empty list is created 
*/ 

public List(){ 
    head = null;  
} 

/** 
* Delete the current element from this list. The element after the deleted element becomes the new current. 
* If that's not possible, then the element before the deleted element becomes the new current. 
* If that is also not possible, then you need to recognize what state the list is in and define current accordingly. 
* Nothing should be done if a delete is not possible. 
* @pre there is a current element 
* @post the item current pointed to is removed, the element before or after is the new current 
*/ 

public void delete(){ 
    if(current.getNext() != null){ 
     current = current.getNext(); 
     prev.setNext(current); 
     size--; 
    } 
    else 
     if(current.getNext() == null){ 
     current = prev; 
     current.setNext(null); 
     resetPrev(); 
     size--; 
    } 
} 

/** 
* Get the value of the current element. If this is not possible, throw an IllegalArgumentException. 
* @pre the list is not empty 
* @post the current element's data is returned 
* @return value of the current element. 
*/ 

public char get(){ 
    return current.getItem(); 
} 

/** 
* Go to the last element of the list. If this is not possible, don't change the cursor. 
* @pre there is a current element 
* @post current is now the last element in the list 
*/ 

public void goLast(){ 
    while (current.getNext() != null){ 
     current = current.getNext(); 
    } 
} 

/** 
* Advance the cursor to the next element. If this is not possible, don't change the cursor. 
* @pre there is a current element 
* @post current is now the element after what it was 
*/ 

public void goNext(){ 
    if(current.getNext() != null){ 
     current = current.getNext(); 
     } 
    //else do nothing 
} 

/** 
* Retreat the cursor to the previous element. If this is not possible, don't change the cursor. 
* @pre 
* @post 
*/ 

public void goPrev(){ 
    current = prev; 
    resetPrev(); 
} 

/** 
* Go to top of the list. This is the position before the first element. 
* @pre 
* @post 
*/ 

public void goTop(){ 

} 


/** 
* Go to first element of the list. If this is not possible, don't change the cursor. 
* @pre none 
* @post current is now pointing to the first node of the list, the head 
*/ 

public void goFirst(){ 
    current = head; 
} 

/** 
* Insert the given parameter after the current element. The newly inserted element becomes the current element. 
* @pre none 
* @post newVal is inserted into the list and is now the current element 
* @param newVal is the value to insert after the current element. 
*/ 

public void insert(char newVal){ 
    if(head == null){ 
     head = new Node(newVal); 
     size++; 
    } 
    else{ 
     Node current = head; 
     while(current.getNext() != null){ 
      current = current.getNext(); 
     } 
     Node prev = current; 
     current = new Node(newVal); 
     prev.setNext(current); 
     size++; 
    } 
} 

/** 
* Determines if this list is empty. Empty means this list has no elements. 
* @pre none 
* @post a boolean value of the state of the list is returned 
* @return true if the list is empty. 
*/ 

public boolean isEmpty(){ 
    return head == null; 
} 

/** 
* Determines the size of the list. The size of the list is the number of elements in the list. 
* @pre none 
* @post size is accessed and returned 
* @return size which is the number of elements in the list. 
*/ 

public int size(){ 
    return size; 
} 

public void resetPrev(){ 
    //reset prev to prev's previous 
    Node temp = head; 
    while(temp != current){ 
     temp = temp.getNext(); 
    } 
    prev = temp; 
} 

public class Node { 

    private char item; 
    private Node next; 

    public Node(char val) { 
     this.item = val; 
    } 

    public char getItem() { 
     return this.item; 
    } 

    public Node getNext() { 
     return this.next; 
    } 

    public void setNext(Node next) { 
     this.next = next; 
    } 
} 

} 

感谢所有帮助IM仍然很新,所以我不知道如何做这些,即时通讯甚至不知道它的工作原理所有这就是为什么我要测试它的方式!

+1

http://stackoverflow.com/questions/1139136/how-to-best-test-java-code –

回答

2

测试:

我会建议为每种方法编写一些JUnit测试用例。这里是你需要去开始的地方http://www.junit.org

如果你使用Eclipse IDE,Lars Vogel是我最喜欢的资源之一。通过他的tutorial工作,如果您有任何其他具体问题,请更新您的原始帖子。

+0

我们被认为是使用j单位,但对于我的爱这件事迷惑了我-___-会阅读谢谢! – erp

相关问题