发布
社区首页 >问答首页 >堆栈链接列表故障排除

堆栈链接列表故障排除
EN

Stack Overflow用户
提问于 2016-10-28 01:31:11
回答 1查看 90关注 0票数 1

我有这个任务来实现链表堆栈,我不明白为什么测试输出没有显示任何数据。当编译和运行程序时,输出是:

,这就是它应该看起来像的样子

抽样预期产出:

好:堆栈是空的

推送数据: 10,30,50

打印堆栈50,30,10,

好的:堆栈大小是3

好的:窥视堆栈顶部是50

好的:堆栈不是空的

Pop 2数据: 50,30

打印堆栈30,10,

打印栈10,

好的:堆栈pop数据是30

清晰堆栈

打印堆栈[]

,这是我要得到的,

您的测试输出:

好:堆栈是空的

推送数据: 10,30,50

但在这里,它没有运行任何东西,也没有为我提供任何错误。我不明白这是怎么回事。我的代码如下:

代码语言:javascript
代码运行次数:0
复制
/**
    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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-28 01:45:28

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

代码语言:javascript
代码运行次数:0
复制
    System.out.println("Print stack " + s);

永远不会结束。现在您知道了,+ s将在s上调用toString()。所以仔细看看toString在做什么,看看它是否有一个无限循环。(我想是的.)

提示2:如果您无法通过对代码进行“眼球化”来找到bug,那么就尝试使用Java调试器并单步执行它。(你仍然需要运用你的观察力和推理能力.)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40296614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档