gpt4 book ai didi

java - 堆栈链表故障排除

转载 作者:行者123 更新时间:2023-11-30 10:37:01 24 4
gpt4 key购买 nike

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

这就是它应该看起来的样子
示例预期输出:

确定:堆栈为空
推送 3 条数据:10、30、50
打印堆栈 [50,30,10,]
OK:堆栈大小为 3
OK: 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

最佳答案

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

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

永远不会结束。现在您知道 + s 将在 s 上调用 toString()。所以仔细看看toString是干什么的,看看是不是死循环了。 (我觉得是的……)

提示 2:如果您无法通过“目测”代码找到错误,请尝试使用 Java 调试器并单步执行它。 (还是要发挥自己的观察力和推理力……)

关于java - 堆栈链表故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com