堆栈链接列表故障排除

时间:2016-10-28 01:31:12

标签: java linked-list stack

我有这个分配来实现一个链接列表堆栈,我无法理解为什么测试输出没有显示任何数据。编译并运行程序时,输出为:

这就是假设的样子
样本预期输出:

好的:堆栈是空的 推送3个数据:10,30,50 打印堆栈[50,30,10,]
OK:堆栈大小为3
好的:peek stack top是50
好的:堆栈不是空的 Pop 2数据:50,30 打印堆栈[30,10,]
打印堆栈[10,]
OK:堆栈弹出数据为30
清除堆栈
打印堆栈[]

这就是我所得到的
你的测试输出:

好的:堆栈是空的 推送3个数据:10,30,50

但就在这里,它没有运行任何东西,也没有给我任何错误。我不明白这有什么问题。我的代码如下:

/**
    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 个答案:

答案 0 :(得分:1)

提示:您所看到的行为表明:

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

永远不会结束。现在您知道+ s会在toString()上致电s。所以仔细看看toString正在做什么,看看它是否有无限循环。 (我认为确实......)

提示2:如果你无法通过&#34;引人注目的&#34;代码,然后尝试使用Java调试器并单步执行。 (你仍然需要使用你的观察和演绎的力量......)