链接列表堆栈/队列实现没有明显原因清空?

时间:2017-07-13 16:26:05

标签: java linked-list stack queue empty-list

我一直在尝试编写Stack和Queue的LinkedList实现。然而,每当我运行它们时,我都会收到错误,当我尝试弹出/取出任何内容时,我会发现该列表为空。问题是,如果我在列表上调用.toString(),甚至在pop / dequeue命令之前调用RIGHT,我可以看到它们不是。我不能看到我的pop / dequeue实现有什么问题,但也许你们好人可以帮助我。

LinkedList.java

package jsjf;

public class LinkedListQueue<T> implements QueueADT<T> {

    LinkedList queue = new LinkedList();


    /**
     * Adds the specified element to the back of this queue.
     * 
     * @param element generic element to be pushed onto queue.
     */
    public void enqueue(T element) {
        queue.add(element);
    }


    /**
     * Removes the element from the front of the queue and returns it.
     * 
     * @return the element from the front of the queue.
     */
    public T dequeue() {
        return (T) queue.removeFirst();
    }


    /**
     * Returns the front element, without removing it.
     * 
     * @return the element at the front of the queue.
     */
    public T first() {
        return (T) queue.first();
    }


    /**
     * Returns true if the queue is empty.
     * 
     * @return the boolean value of this queue being empty.
     */
    public boolean isEmpty() {
        if(queue.isEmpty())
            return true;
        return false;
    }


    /**
     * Returns the number of elements contained in the queue.
     * 
     * @return the number of elements contained in the queue.
     */
    public int size() {
        return queue.size();
    }


    /**
     * Returns the queue as a string.
     * 
     * @return the queue as a string.
     */
    public String toString() {
        return queue.toString();
    }
}

LinkedListQueue.java

package jsjf;

public class LinkedListStack<T> implements StackADT<T> {

    LinkedList stack = new LinkedList();


    /**
     * Adds the specified element to the top of this stack.
     * 
     * @param element generic element to be pushed onto stack.
     */
    public void push(T element) {
        stack.add(element);
    }


    /**
     * Removes the element from the top of the stack and returns it.
     * 
     * @return the element from the top of the stack.
     */
    public T pop() {
        return (T) stack.removeLast();
    }


    /**
     * Returns the top element, without removing it.
     * 
     * @return the element at the top of the stack.
     */
    public T peek() {
        return (T) stack.last();
    }


    /**
     * Returns true if the stack is empty.
     * 
     * @return the boolean value of this stack being empty.
     */
    public boolean isEmpty() {
        if(stack.isEmpty())
            return true;
        return false;
    }


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


    /**
     * Returns the stack as a string.
     * 
     * @return the stack as a string.
     */
    public String toString() {
        return stack.toString();
    }

}

LinkedListStack.java

package jsjf;

public class Driver {

    public static void main(String[] args) {

        // Instantiate the array based structures.
        ArrayListQueue arrayQueue = new ArrayListQueue();
        ArrayListStack arrayStack = new ArrayListStack();

        // Instantiate the link based structures.
        LinkedListQueue linkedQueue = new LinkedListQueue();
        LinkedListStack linkedStack = new LinkedListStack();

        // An integer to hold reference to a data piece to be pushed to the structures.
        int data;

        // Randomly generate a piece of data, and pass it to the structures.
        for(int i = 0; i < 25; i++){
            data = (int)(Math.random() * 50);
            arrayQueue.enqueue(data);
            arrayStack.push(data);
            linkedQueue.enqueue(data);
            linkedStack.push(data);
        }


        System.out.print("Array Queue: ");
        for(int i = 0; i < 25; i++){
            System.out.print(arrayQueue.dequeue() + " ");
        }
        System.out.println();


        System.out.print("Array Stack: ");
        for(int i = 0; i < 25; i++){
            System.out.print(arrayStack.pop() + " ");
        }
        System.out.println();

        System.out.print("\n\nLinked Stack as string: " + linkedStack.toString() + "\n\n");


        System.out.print("Linked Stack: ");
        for(int i = 0; i < 25; i++){
            System.out.print(linkedStack.pop() + " ");
        }
        System.out.println();


        System.out.print("Linked Queue: " + linkedQueue.toString());


        System.out.print("Linked Queue: ");
        for(int i = 0; i < 25; i++){
            System.out.print(linkedQueue.dequeue() + " ");
        }
        System.out.println();


    }

}

Driver.java

Array Queue: 45 12 25 40 31 32 14 16 14 26 3 25 22 26 29 6 13 12 30 10 46 10 11 3 11 
Array Stack: 11 3 11 10 46 10 30 12 13 6 29 26 22 25 3 26 14 16 14 32 31 40 25 12 45


Linked Stack as string: 45
12
25
40
31
32
14
16
14
26
3
25
22
26
29
6
13
12
30
10
46
10
11
3
11


Linked Stack: Exception in thread "main" jsjf.exceptions.EmptyCollectionException: The list is empty.
    at jsjf.LinkedList.removeLast(LinkedList.java:135)
    at jsjf.LinkedListStack.pop(LinkedListStack.java:24)
    at jsjf.Driver.main(Driver.java:46)

当我运行Driver.java时,我得到以下输出。

var days = {'Monday': 0, 'Tuesday': 1, 'Wednesday': 2, 'Thursday': 3, 'Friday': 4, 'Saturday': 5, 'Sunday': 6};
function getDayPosition(day) {
    return days[day];
}
console.log(getDayPosition('Monday'));

1 个答案:

答案 0 :(得分:2)

看起来你没有在链接列表的添加方法中增加计数,但是使用count来确定它是否为空。