双向链接丢弃堆栈的麻烦

时间:2015-10-15 20:36:17

标签: java reference stack doubly-linked-list

我正在尝试使用双向链表创建一个drop-out堆栈。一切运作良好除了退出功能。当我测试它时,堆栈的底部仍然存在。问题可能出在我的推送方法中。我在想有关于我在某处遗漏的参考文献的一些细节。

我正在使用节点类来保存元素。 Node类包含上一节点和下一节点的自引用,并具有访问和设置这些节点的方法。 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();

    }
}

0 个答案:

没有答案