2016-10-28 5 views
2

Ich habe diese Aufgabe, um einen Linked List Stack zu implementieren, und ich kann nicht verstehen, warum die Testausgabe keine Daten zeigt. Wann immer das Programm kompilieren und ausführen die Ausgabe lautet:Stapelverknüpfte Liste Fehlerbehebung

Dies ist, wie es
Probe Erwartete Ausgabe aussehen annehmen wird:

OK: Stapel leer
Push-3-Daten: 10, 30 50
drucken Stapel [50,30,10,]
OK: Stapelgröße ist 3
OK: peek Stack-Top 50
OK: Stapel nicht leer
Pop 2 Daten: 50, 30
Drucken Stapel [30,10,]
Drucken Stapel [10,]
OK: Stapel pop Daten 30
löschen Stapel
Druckstapel []

Dies ist, was ich bin immer statt
Ihr Testausgang:

OK: Stapel leer
Push-3-Daten: 10, 30, 50

Aber genau hier läuft nichts und es liefert mir keine Fehler. Ich verstehe nicht, was damit nicht stimmt. Mein Code ist unten:

/** 
    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

Haben Sie versucht, einen Java-Debugger zu verwenden? Das sollte das erste sein, was du versuchst ... bevor du um Hilfe bittest. –

Antwort

1

Hinweis: Das Verhalten, das Sie sehen, sind schlägt vor, dass:

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

nie beendet. Jetzt wissen Sie, dass + stoString() auf s aufrufen wird. Schauen Sie genau hin, was toString tut, um zu sehen, ob es eine Endlosschleife hat. (Ich denke, es tut ...)

Tipp 2: Wenn Sie den Fehler nicht finden können, indem Sie den Code "eye-balling", dann versuchen Sie es mit einem Java-Debugger und Single-Stepping es. (Sie müssen immer noch Ihre Beobachtungs- und Abzugsfähigkeiten nutzen ...)

+0

danke für den Hinweis. Ich habe jetzt meinen Code repariert. – Wes