Java – 链接堆栈实现,无法弄清楚为什么始终打印相同的数字
作者:互联网
所以我一直在努力完成以下大学实验.大部分代码都提供给我们,但我们被告知要填写LinkedStack.java代码的方法.这个概念非常简单,每个Node()对象都包含一个Object类型变量(在本例中为Integer)和另一个Node()对象. LinkedStack()类用于初始化这些节点并设置它们的数量限制.问题是,在执行StackTest.java之后,返回以下内容:
18 18 18 18 18 18 18 18 18 18
6 6 6 6 6 6
当它应该返回以下内容:
18 16 14 12 10 8 6 4 2 0
6 5 4 3 2 1
附:代码被广泛评论,所以我相信你将能够找出我没有提到的任何内容.此外,StackADT只是一个简单的接口,它只是陈述LinkedStack中使用的方法.
当然,非常感谢任何帮助过的人!
这是所有的代码(对不起,我是新来的,不知道如何防止它分组):
// ***************************************************************
// LinkedStack.java
//
// A linked implementation of an Object stack class with operations push,
// pop, and isEmpty and isFull.
//
// ***************************************************************
public class LinkedStack implements StackADT {
private Node top; // reference to top of stack
private int size;
private int counter = 0;
// ---------------------------------------------------
// Constructor -- initializes top
// ---------------------------------------------------
public LinkedStack() {
top = new Node();
size = 10;
}
// ---------------------------------------------------
// Adds element to top of stack if it's not full, else
// does nothing.
// ---------------------------------------------------
public void push(Object val) {
counter++;
if(counter > 0 && counter <= size) {]
top.setNext(top);
top.setElement(val);
}
else {
top.setElement(val);
}
}
// ---------------------------------------------------
// Removes and returns value at top of stack. If stack
// is empty returns null.
// ---------------------------------------------------
public Object pop() {
if (counter > 0) {
Object val = top.getElement();
top = top.getNext();
counter--;
return(val);
}
else {
return(null);
}
}
// ---------------------------------------------------
// Returns true if stack is empty, false otherwise.
// ---------------------------------------------------
public boolean isEmpty() {
if(counter == 0) {
return(true);
}
else {
return(false);
}
}
// ---------------------------------------------------
// Returns true if stack is full, false otherwise.
// ---------------------------------------------------
public boolean isFull() {
if(counter == size) {
return(true);
}
else {
return(false);
}
}
}
Node.java:
//***********************************************************
// Node.java
// A general node for a singly linked list of objects.
//***********************************************************
public class Node {
private Node next;
private Object element;
//----------------------------------------------------
// Creates an empty node
//----------------------------------------------------
public Node() {
next = null;
element = null;
}
//----------------------------------------------------
// Creates a node storing a specified element
//----------------------------------------------------
public Node(Object element) {
next = null;
this.element = element;
}
//----------------------------------------------------
// Returns the node that follows this one
//----------------------------------------------------
public Node getNext() {
return next;
}
//----------------------------------------------------
// Sets the node that follows this one
//----------------------------------------------------
public void setNext(Node node) {
next = node;
}
//----------------------------------------------------
// Returns the element stored in this node
//----------------------------------------------------
public Object getElement() {
return element;
}
//----------------------------------------------------
// Sets the element stored in this node
//----------------------------------------------------
public void setElement(Object element) {
this.element = element;
}
}
StackTest.java:
// *******************************************************
// StackTest.java
//
// A simple driver that exercises push, pop, isFull and isEmpty.
// Thanks to autoboxing, we can push integers onto a stack of Objects.
//
// *******************************************************
public class StackTest {
public static void main(String[] args) {
StackADT stack = new LinkedStack ();
//push some stuff on the stack
for (int i=0; i<10; i++)
stack.push(i*2);
//pop and print
//should print 18 16 14 12 10 8 6 4 2 0
while (!stack.isEmpty())
System.out.print(stack.pop() + " ");
System.out.println();
//push a few more things
for (int i=1; i<=6; i++)
stack.push(i);
//should print 6 5 4 3 2 1
while (!stack.isEmpty())
System.out.print(stack.pop() + " ");
System.out.println();
}
}
解决方法:
堆栈的工作原理是将新节点添加到堆栈顶部,并将其下一个引用设置为先前的顶级节点.
您在构建LinkedStack时创建一个节点,然后在push方法中将引用设置为自己的下一个节点(进行无限循环).
在指示您推送第一个项目之前,不应创建顶级节点.并且最底部的项应该将其下一个引用设置为null,以指示它下面没有更多.
public void push(Object val) {
if(!isFull()) {
counter++;
Node lastTop = top;
top = new Node(val);
top.setNext(lastTop);
}
}
你的isEmpty()方法只需检查top是否为null.
你的pop方法很好,但是你应该使用isEmpty方法来使代码更容易理解:
public Object pop() {
if(!isEmpty()) {
counter--;
Node lastTop = top;
top = top.getNext();
return lastTop.getElement();
} else {
return null;
}
}
标签:java,variable-assignment,static,computer-science,stack 来源: https://codeday.me/bug/20190728/1562996.html