Java 双链接辍学堆栈的问题
我正在尝试使用双链接列表创建一个退出堆栈。除了退出功能外,所有功能都运行良好。当我测试它时,堆栈的底部仍然存在。问题可能出在我的推送方法中。我在想我遗漏了一些关于参考资料的细节 我使用node类来保存元素。节点类包含上一个和下一个节点的自引用,并具有访问和设置这些节点的方法Java 双链接辍学堆栈的问题,java,reference,stack,doubly-linked-list,Java,Reference,Stack,Doubly Linked List,我正在尝试使用双链接列表创建一个退出堆栈。除了退出功能外,所有功能都运行良好。当我测试它时,堆栈的底部仍然存在。问题可能出在我的推送方法中。我在想我遗漏了一些关于参考资料的细节 我使用node类来保存元素。节点类包含上一个和下一个节点的自引用,并具有访问和设置这些节点的方法getNext(),setNext(),getPrevious(),setNext() 任何帮助都将不胜感激 public class lDOStack<T> { private int count;
getNext()
,setNext()
,getPrevious()
,setNext()
任何帮助都将不胜感激
public class lDOStack<T>
{
private int count;
private LinearNode<T> top, bottom;
private static final int LIMIT = 10;
/**
* Creates an empty stack.
*/
public lDOStack()
{
count = 0;
top = bottom = null;
}
/**
* Adds the specified element to the top of this stack.
* If number of elements is greater than limit after push,
* bottom of stack is lost.
* @param element element to be pushed on stack
*/
public void push(T element)
{
LinearNode<T> temp = new LinearNode<T>(element);
temp.setNext(top);
if (isEmpty())
{
bottom = temp; //Reference to first element added.
}
else if (size() == 1)
{
bottom.setPrevious(temp);
}
else if (size() > 0)
{
top.setPrevious(temp);
}
top = temp;
count++;
if (size() > LIMIT) //Bottom should drop out but doesn't.
{
count--;
bottom = bottom.getPrevious();
}
}
公共类lDOStack
{
私人整数计数;
私有LinearNode顶部、底部;
专用静态最终整数限值=10;
/**
*创建一个空堆栈。
*/
公共lDOStack()
{
计数=0;
顶部=底部=空;
}
/**
*将指定的元素添加到此堆栈的顶部。
*如果推送后元件数量大于极限,
*堆栈底部丢失。
*@param元素要推送到堆栈上
*/
公共无效推送(T元素)
{
LinearNode temp=新的LinearNode(元素);
温度设置下一步(顶部);
if(isEmpty())
{
bottom=temp;//添加了对第一个元素的引用。
}
else if(size()==1)
{
底部。设置前一个(温度);
}
else if(size()>0)
{
顶部。设置上一个(温度);
}
顶部=温度;
计数++;
if(size()>LIMIT)//底部应该退出,但不会退出。
{
计数--;
bottom=bottom.getPrevious();
}
}
实现的size()
在哪里?另外,与bottom=bottom.getPrevious();
一起,您应该调用bottom.getNext().setPrevious(null);
和bottom.getNext().setNext(null);和bottom.setNext(null)
。搞定了。这就是问题所在。谢谢!size()实现和其他标准方法,如isEmpty()都存在。我只是没有发布它们,因为它们并不真正相关。我知道它们是有效的。