在java中使用链接列表创建一个dropout堆栈的麻烦

时间:2017-05-08 21:19:41

标签: java linked-list stack

所以在我的任务中,我必须在Java中实现一个dropout堆栈。除非如果堆栈大小为n,否则丢弃堆栈在各方面的行为类似于堆栈,然后当按下n + 1个元素时,第一个元素将丢失。就我而言,我设置了n = 5。我的代码运行正常,但是当我在第5个元素后添加更多元素时,底部的元素不会被删除。它只是像普通的堆栈一样将新的堆叠在顶部。请帮我理解如何解决这个问题。这是我的堆栈实现代码:

/**
 * Represents a linked implementation of a stack.
 *
 * @author Java Foundations 
 * @version 4.0
 */
    public class DropOutStack<T> implements StackADT<T>
    {
    private int count; //number of elements in the stack
    private LinearNode<T> top; 

    /*Declares the maximum number of elements in the stack*/
    private final int n = 5;//max size
    private LinearNode<T> prev;
    private LinearNode<T> curr;

    /**
     * Creates an empty stack.
     */
    public DropOutStack()
    {
        count = 0;
        top = null;
    }

    /**
     * Adds the specified element to the top of this stack.
     * @param element element to be pushed on stack
     */
    public void push(T element)
    {
        LinearNode<T> temp = new LinearNode<T>(element);

        /*Verifies that the number of elements in the stack is
         * less than n. If yes, adds the new element to the stack*/
        if (count < n) {
            temp.setNext(top);
            top = temp;
            count++;
        }
        /*Verifies if the number of elements in the stack is greater
         * than or equal to n or not, and that the n is not equal to one.
         * If yes, removes the first element from the stack and adds
         * the new element to the stack*/
        else if(count>=n && n!=1) {
            prev = top;
            curr = top.getNext();

            while(curr != null) {
                prev = prev.getNext();
                curr = curr.getNext();
            }
            prev.setNext(null);
            count--;

            push(element);
        }
        else //if n=1
        {
            top.setElement(element);
        }
    }

    /**
     * Removes the element at the top of this stack and returns a
     * reference to it. 
     * @return element from top of stack
     * @throws EmptyCollectionException if the stack is empty
     */
    public T pop() throws EmptyCollectionException
    {
        if (isEmpty())
            throw new EmptyCollectionException("stack");

        T result = top.getElement();
        top = top.getNext();
        count--;

        return result;
    }

    /**
     * Returns a reference to the element at the top of this stack.
     * The element is not removed from the stack.  
     * @return element on top of stack
     * @throws EmptyCollectionException if the stack is empty  
     */
    public T peek() throws EmptyCollectionException
    {

        if (isEmpty())
            throw new EmptyCollectionException("stack");
        T result = top.getElement();

        return result;

    }

    /**
     * Returns true if this stack is empty and false otherwise. 
     * @return true if stack is empty
     */
    public boolean isEmpty()
    {
        return (count ==0);

    }

    /**
     * Returns the number of elements in this stack.
     * @return number of elements in the stack
     */
    public int size()
    {
        return count;
    }

    /**
     * Returns a string representation of this stack. 
     * @return string representation of the stack
     */
    public String toString()
    {
        String result = "";
        LinearNode<T> current = top;
        while (current != null) {
            result = current.getElement() + "\n" + result;
            current = current.getNext();
        }
        return result;
    }
    }

1 个答案:

答案 0 :(得分:1)

问题是您在else if (count >= n && n != 1)块中的链接列表上进行了过多的迭代。

目前您在curr == null时终止,但如果currnull,则prev是列表中的最后一个节点,其下一个值已经是nullcurr)。

要解决此问题,请将循环条件更改为curr.getNext() != null。因为你总是想要推送元素,所以只需在最后执行此操作并简化逻辑,前提是n(最大大小[可怜的名字])应该至少为1。

/**
 * @pre. n >= 1
 */
public void push(T element) {
    if (count >= n) {
        assert top != null;
        prev = null;
        curr = top;
        while (curr.getNext() != null) {
            prev = curr;
            curr = curr.getNext();
        }
        if (prev != null) {
            prev.setNext(null);
        } else {
            top = null;
        }
        count--;
    }
    LinearNode<T> temp = new LinearNode<>(element);
    temp.setNext(top);
    top = temp;
    count++;
}