【问题标题】:Null pointer Exception while reversing a Queue using stack使用堆栈反转队列时出现空指针异常
【发布时间】:2016-12-05 11:43:11
【问题描述】:

我正在练习如何使用数组实现队列。我已经轻松实现了如何将队列中的元素入队和出队。但是我在使用堆栈实现队列反向时遇到了异常

public class QueueImpl {
private int capacity;
int queueArr[];
int front = 0;
int rear = -1;
int currentSize = 0;
QueueImpl(int queueSize){
    this.capacity=queueSize;
    queueArr=new int[this.capacity];
}

public void enqueue(int data){
    if(isQueueFull()){
        System.out.println("Overflow");
        return;
    }
    else{
        rear=rear+1;
        if(rear==capacity-1)
        {
            rear=0;
        }
        queueArr[rear]=data;
        currentSize++;
        System.out.println("Element " + data+ " is pushed to Queue !");
    }

}


public int dequeue(){
    if(isQueueEmpty()){
        System.out.println("UnderFlow");
    }
    else{
        front=front+1;
        if(front == capacity-1){
            System.out.println("Pop operation done ! removed: "+queueArr[front-1]);
            front = 0;
        } else {
            System.out.println("Pop operation done ! removed: "+queueArr[front-1]);
        }
        currentSize--;
    }
    return queueArr[front-1];

}
private boolean isQueueEmpty() {
    boolean status=false;
    if(currentSize==0){
        status=true;
    }
    return status;
}

private boolean isQueueFull() {
    boolean status=false;
    if(currentSize==capacity){
        status=true;
    }
    return status;
}

public static void main(String arg[]) {
    QueueImpl queueImpl=new QueueImpl(5);
    queueImpl.enqueue(5);
    queueImpl.enqueue(2);
    queueImpl.enqueue(9);
    queueImpl.enqueue(1);
//  queueImpl.dequeue();
    queueImpl.printQueue(queueImpl);
    queueImpl.reverse(queueImpl);
}

private void printQueue(QueueImpl queueImpl) {
System.out.println(queueImpl.toString());       
}
 @Override
    public String toString() {
        return "Queue [front=" + front + ", rear=" + rear + ", size=" + currentSize
                + ", queue=" + Arrays.toString(queueArr) + "]";
    }
private QueueImpl reverse(QueueImpl queueImpl) throws ArrayIndexOutOfBoundsException {
    int i=0;
    Stack<Integer> stack=new Stack<Integer>();  
    while(!queueImpl.isQueueEmpty()){
        stack.push(queueImpl.dequeue());
    }
    while(!stack.isEmpty()){
        stack.get(i);
        i++;
    }
    while(!stack.isEmpty()){
        queueImpl.enqueue(stack.pop());
    }
    return queueImpl;
}

}

错误日志是-

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
 at com.tcs.QueueUsingAraay.QueueImpl.dequeue(QueueImpl.java:51)
 at com.tcs.QueueUsingAraay.QueueImpl.reverse(QueueImpl.java:93)
 at com.tcs.QueueUsingAraay.QueueImpl.main(QueueImpl.java:78)

【问题讨论】:

    标签: data-structures stack queue reverse


    【解决方案1】:

    你的出队方法有问题: 1. 你在做 front=0 并试图访问 front-1 是 -1 结果引发了异常。

    1. 删除了 stack.get,因为它不是必需的。

    更正的工作代码。

    public class QueueImpl {
            private int capacity;
            int queueArr[];
            int front = 0;
            int rear = -1;
            int currentSize = 0;
    
            QueueImpl(int queueSize) {
                this.capacity = queueSize;
                queueArr = new int[this.capacity];
            }
    
            public void enqueue(int data) {
                if (isQueueFull()) {
                    System.out.println("Overflow");
                    return;
                } else {
                    rear = rear + 1;
                    if (rear == capacity - 1) {
                        rear = 0;
                    }
                    queueArr[rear] = data;
                    currentSize++;
                    System.out.println("Element " + data + " is pushed to Queue !");
                }
    
            }
    
            public int dequeue() {
                int element=-1;
                if (isQueueEmpty()) {
                    System.out.println("UnderFlow");
                } else {
                    element = queueArr[front];
                    front=front+1;
                    if (front == capacity - 1) {
                        System.out.println("Pop operation done ! removed: "
                                + queueArr[front - 1]);
                        front = 0;
                    } else {
                        System.out.println("Pop operation done ! removed: "
                                + queueArr[front - 1]);
                    }
                    currentSize--;
                }
                return element;
    
            }
    
            private boolean isQueueEmpty() {
                boolean status = false;
                if (currentSize == 0) {
                    status = true;
                }
                return status;
            }
    
            private boolean isQueueFull() {
                boolean status = false;
                if (currentSize == capacity) {
                    status = true;
                }
                return status;
            }
    
            public static void main(String arg[]) {
                QueueImpl queueImpl = new QueueImpl(5);
                queueImpl.enqueue(5);
                queueImpl.enqueue(2);
                queueImpl.enqueue(9);
                queueImpl.enqueue(1);
    
                queueImpl.printQueue(queueImpl);
                queueImpl.reverse(queueImpl);
                queueImpl.printQueue(queueImpl);
            }
    
            private void printQueue(QueueImpl queueImpl) {
                System.out.println(queueImpl.toString());
            }
    
            @Override
            public String toString() {
                return "Queue [front=" + front + ", rear=" + rear + ", size="
                        + currentSize + ", queue=" + Arrays.toString(queueArr) + "]";
            }
    
            private QueueImpl reverse(QueueImpl queueImpl)
                    throws ArrayIndexOutOfBoundsException {
                Stack<Integer> stack = new Stack<Integer>();
                while (!queueImpl.isQueueEmpty()) {
                    stack.push(queueImpl.dequeue());
                }
                while (!stack.isEmpty()) {
                    queueImpl.enqueue(stack.pop());
                }
                return queueImpl;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 2014-12-18
      • 2020-03-21
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多