创建不可变队列的问题

时间:2014-09-19 14:03:42

标签: java stack queue immutability

我使用以下代码创建一个不可变队列。

import java.util.NoSuchElementException;
import java.util.Queue;

public class ImmutableQueue<E> {

    //Two stacks are used. One is to add items to the queue(enqueue) and 
    //other is to remove them(dequeue)


    private ImmutableQueue(ReversableStack<E> order, ReversableStack<E> reverse) {
        this.order = order;
        this.reverse = reverse;
    }

    //initially both stacks are empty
    public ImmutableQueue() {
        this.order = ReversableStack.emptyStack();
        this.reverse = ReversableStack.emptyStack();
    }

    public ImmutableQueue<E> enqueue(E e) {
        if (null == e)
            throw new IllegalArgumentException();
        return new ImmutableQueue<E>(this.order.push(e), this.reverse);
    }

    public ImmutableQueue<E> dequeue() {
        if (this.isEmpty())
            throw new NoSuchElementException();
        if (!this.reverse.isEmpty()) {
            return new ImmutableQueue<E>(this.order, this.reverse.tail);
        } else {

            return new ImmutableQueue<E>(ReversableStack.emptyStack(),
                    this.order.getReverseStack().tail);
        }
    }




    private static class ReversableStack<E> {
        private E head; //top of original stack
        private ReversableStack<E> tail; //top of reversed stack
        private int size;

        //initializing stack parameters
        private ReversableStack(E obj, ReversableStack<E> tail) {
            this.head = obj;
            this.tail = tail;
            this.size = tail.size + 1;
        }

        //returns a new empty stack
        public static ReversableStack emptyStack() {
            return new ReversableStack();
        }

        private ReversableStack() {
            this.head = null;
            this.tail = null;
            this.size = 0;
        }

        //Reverses the original stack
        public ReversableStack<E> getReverseStack() {
            ReversableStack<E> stack = new ReversableStack<E>();
            ReversableStack<E> tail = this;
            while (!tail.isEmpty()) {
                stack = stack.push(tail.head);
                tail = tail.tail;
            }
            return stack;
        }

        public boolean isEmpty() {
            return this.size == 0;
        }

        public ReversableStack<E> push(E obj) {
            return new ReversableStack<E>(obj, this);
        }
    }

    private ReversableStack<E> order;

    private ReversableStack<E> reverse;



    private void normaliseQueue() {
        this.reverse = this.order.getReverseStack();
        this.order = ReversableStack.emptyStack();
    }

    public E peek() {
        if (this.isEmpty())
            throw new NoSuchElementException();
        if (this.reverse.isEmpty())
            normaliseQueue();
        return this.reverse.head;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    //returns the number of items currently in the queue
    public int size() {
        return this.order.size + this.reverse.size;
    }

    public static void main(String[] args)
    {
        ImmutableQueue<Integer> newQueue = new ImmutableQueue<Integer>();
        newQueue.enqueue(5);
        newQueue.enqueue(10);
        newQueue.enqueue(15);
        int x = newQueue.size();
        //ImmutableQueue<Integer> x = newQueue.dequeue();
        System.out.println(x);

    }

}

但每当我尝试出队时,我都会收到NoSuchElementException。此外,newQueue.size函数也返回0。 我究竟做错了什么 ? 感谢

1 个答案:

答案 0 :(得分:2)

您缺少新的ImmutableQueue参考..

因为enqueue()方法返回ImmutableQueue

的新实例
public ImmutableQueue<E> enqueue(E e) {
    if (null == e)
        throw new IllegalArgumentException();
    return new ImmutableQueue<E>(this.order.push(e), this.reverse);
}

但是在你的主要方法上你丢弃了那个对象

public static void main(String[] args)
{
    ImmutableQueue<Integer> newQueue = new ImmutableQueue<Integer>();
    newQueue.enqueue(5);
    newQueue.enqueue(10);
    newQueue.enqueue(15);
    int x = newQueue.size();
    //ImmutableQueue<Integer> x = newQueue.dequeue();
    System.out.println(x);

}

将您的电话改为:

newQueue = newQueue.enqueue(5);
int x = newQueue.size();
System.out.println(x);

您会看到尺寸会发生变化