2016-10-28 4 views
2

링크 된 목록 스택을 구현하는 데이 할당이 있으며 테스트 출력에 데이터가 표시되지 않는 이유를 알 수 없습니다. 지금까지 프로그램을 컴파일하고 실행하면 출력은 다음과 같습니다스택 연결된 목록 문제 해결

이이
샘플 예상 출력과 같이 할 생각되는 것입니다 :

OK : 10, 30 : 스택
푸시 3 데이터 비어 50
인쇄 스택 [50,30,10,]
OK : 스택 사이즈가 3
OK이다 PEEK 스택 최고 50
OK : 스택
팝 2 데이터 비어 있지 : 50 30
인쇄 스택 [30,10,]
인쇄 스택 [10]
OK : 스택 팝 데이터가 30
지우기 스택
인쇄 스택 [] 이건 내가 대신 얻고 무엇을


테스트 출력 :

OK : 스택이 비어
푸시 3 데이터이다 : 10, 30, 50

,617,

하지만 여기서는 아무 것도 실행하지 않으며 오류가 발생하지 않습니다. 나는 이것이 무엇이 잘못된지 이해하지 못한다. 내 코드는 다음과 같습니다 :

/** 
    A class of stacks whose entries are stored in a chain of nodes. 
    Implement all methods in SimpleLinkedStack class using 
    the inner Node class. 

    Do not change or add data fields 
    Do not add new methods 
    You may access Node object fields directly, i.e. data and next 
*/ 

package PJ2; 

public class SimpleLinkedStack<T> implements StackInterface<T> 
{ 

    // Data fields 
    private Node topNode; // references the first node in the chain 
    private int count;  // number of data in this stack 

    public SimpleLinkedStack() 
    { 
     topNode = null; 
     count = 0; 
     // add stataments 
    } // end default constructor 

    public void push(T newData) 
    { 
     Node newNode = new Node (newData, topNode); 
     topNode = newNode; 
     count++; 

     // add stataments 
    } // end push 

    public T peek() 
    { 
     T top = null; 
     if (topNode != null) 
      top = topNode.data; 

     return top; 
     // add stataments 
    } // end peek 

    public T pop() 
    { 
     T top = peek(); 
     if (topNode != null) { 
      topNode = topNode.next; 
      count--; 
     } 

     return top; 
     // add stataments 
    } // end pop 

    public boolean empty() 
    { 
     return (count == 0) && (topNode == null); 
     // add stataments 
    } // end empty 

    public int size() 
    { 
     return count; 
     // add stataments 
    } // end isEmpty 

    public void clear() 
    { 
     topNode = null; 
     count = 0; 
     // add stataments 
    } // end clear 

    @Override 
    public String toString() 
    { 
     String result = "["; 
     Node currentNode = topNode; 
     while (currentNode != null) { 
      result = result + topNode.data + ", "; 
      currentNode = topNode.next; 
     } 
     result = result + "]"; 
     return result; 
     // add stataments 
     // note: data class in stack must implement toString() method 
     //  return a list of data in Stack, separate them with ',' 
    } 


    /**************************************************** 
    private inner node class 
     Do not modify this class!! 
     you may access data and next directly 
    ***************************************************/ 

    private class Node 
    { 
     private T data; // entry in list 
     private Node next; // link to next node 
     private Node (T dataPortion) 
     { 
     data = dataPortion; 
     next = null; // set next to NULL 
     } // end constructor 

     private Node (T dataPortion, Node nextNode) 
     { 
     data = dataPortion; 
     next = nextNode; // set next to refer to nextNode 
     } // end constructor 
    } // end Node 


    /**************************************************** 
     Do not modify: Stack test 
    ****************************************************/ 
    public static void main (String args[]) 
    { 

     System.out.println("\n"+ 
    "*******************************************************\n"+ 
     "Sample Expected output:\n"+ 
    "\n"+ 
     "OK: stack is empty\n"+ 
     "Push 3 data: 10, 30, 50\n"+ 
     "Print stack [50,30,10,]\n"+ 
     "OK: stack size is 3\n"+ 
     "OK: peek stack top is 50\n"+ 
     "OK: stack is not empty\n"+ 
     "Pop 2 data: 50, 30\n"+ 
     "Print stack [30,10,]\n"+ 
     "Print stack [10,]\n"+ 
     "OK: stack pop data is 30\n"+ 
     "Clear stack\n"+ 
     "Print stack []\n"+ 
    "\n"+ 
    "*******************************************************"); 

     System.out.println("\nYour Test output:\n"); 
    StackInterface<Integer> s = new SimpleLinkedStack<Integer>(); 
    if (s.empty()) 
      System.out.println("OK: stack is empty"); 
    else 
      System.out.println("Error: stack is not empty"); 

    s.push(10); 
    s.push(30); 
    s.push(50); 
     System.out.println("Push 3 data: 10, 30, 50"); 
     System.out.println("Print stack " + s); 

    if (s.size() == 3) 
      System.out.println("OK: stack size is 3"); 
    else 
      System.out.println("Error: stack size is " + s.size()); 

    if (s.peek() == 50) 
      System.out.println("OK: peek stack top is 50"); 
    else 
      System.out.println("Error: peek stack top is " + s.size()); 

    if (!s.empty()) 
      System.out.println("OK: stack is not empty"); 
    else 
      System.out.println("Error: stack is empty"); 

     System.out.println("Pop 2 data: 50, 30"); 
     s.pop(); 
     System.out.println("Print stack " + s); 
    int data=s.pop(); 
     System.out.println("Print stack " + s); 
    if (data == 30) 
      System.out.println("OK: stack pop data is 30"); 
    else 
      System.out.println("Error: stack pop data is " + data); 

     System.out.println("Clear stack"); 
     s.clear(); 
     System.out.println("Print stack " + s); 
    } 

} // end Stack 
+1

자바 디버거를 사용해 보았습니까? 그게 도움을 요청하기 전에 당신이 시도한 첫 번째 것이어야합니다. –

답변

1

힌트 :

System.out.println("Print stack " + s); 

결코 완료되지 : 당신이보고있는 행동은 제안합니다. 이제 + sstoString()으로 전화 할 것입니다. 따라서 무한 루프가 있는지 확인하려면 toString을주의 깊게 살펴보십시오. (나는 생각합니다 ...)

힌트 2 : 코드를 "눈으로 볼링"하여 버그를 찾을 수없는 경우 Java 디버거를 사용하여 단일 단계로 시도하십시오. (당신은 여전히 ​​관측과 공제의 힘을 사용해야합니다 ...)

+0

힌트를 가져 주셔서 감사합니다. 지금 제 코드를 수정했습니다. – Wes