堆栈和队列数组返回错误的值

时间:2018-02-14 19:50:39

标签: java

我创建了堆栈和队列数组,它们保存整数并且最大容量为10。我添加了值1-11,然后将删除并打印每个值,直到为空。队列不仅打印11个值,而且还打印1而不是11作为第一个值。但是,堆栈只打印并删除了10个值,但却给了我一个未被捕获的IndexOutBoundsException。知道为什么吗?

输出:

run:
Print queue: 
11 2 3 4 5 6 7 8 9 10 11 
Print stack: 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
  at lab4.Stack.pop(Stack.java:44)
  at lab4.Lab4.main(Lab4.java:54)
10 9 8 7 6 5 4 3 2 1 
BUILD FAILED (total time: 0 seconds)

主:

    Queue queue = new Queue(10);
    Stack stack = new Stack(10);

    queue.insert(1);
    queue.insert(2);
    queue.insert(3);
    queue.insert(4);
    queue.insert(5);
    queue.insert(6);
    queue.insert(7);
    queue.insert(8);
    queue.insert(9);
    queue.insert(10);
    queue.insert(11);

    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    stack.push(5);
    stack.push(6);
    stack.push(7);
    stack.push(8);
    stack.push(9);
    stack.push(10);
    stack.push(11);

    System.out.println("Print queue: ");
    while (!queue.isEmpty()) {
        long n = queue.remove();
        System.out.print(n + " ");
    }
    System.out.println("");

    System.out.println("Print stack: ");
    while (!stack.isEmpty()) {
        Object value = stack.pop();
        System.out.print(value + " ");
    }

Stack.java

public class Stack {

private int[] stackArray;   // Stack array of objects
private int maxSize = 10;       // Max size of the stack
private int top;                    // top integer of stack

public Stack(int maxSize) {
    stackArray = new int[maxSize];
    top = -1;
}

public boolean isEmpty() {
    return (top == maxSize); // true if stack is empty
}

public boolean isFull() {
    return (top == maxSize - 1); // true if full
}

public int size() {
    return (top + 1); // Return the # of items
}

public void push(int item) // put element into array
{
    if (!isFull()) {
        top++;
        stackArray[top] = item;
    }

}

public Object pop() {
    if (!isEmpty()) {
        top--;
        return stackArray[top + 1];
    } else
        throw new IllegalStateException("Stack empty");
}

public Object peek() {
    return stackArray[top];
}

public String toString() {
    return "Size: " + maxSize + " Capcaity: " + top; // complete: show the size and capacity
}

}

Queue.java

public class Queue {

private int maxSize = 10;
private int[] queueArray;
private int front;
private int rear;
private int nItems;

// Constructor
public Queue(int s) {
    queueArray = new int[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
}

// Insert item at rear of queue
public void insert(int j) {
    if (rear == maxSize - 1) {  // Deal with wraparound
        rear = -1;
    }
    queueArray[++rear] = j;
    nItems++;
}

public int remove() {
    int temp = queueArray[front++];
    if (front == maxSize) {
        front = 0;
    }
    nItems--;
    return temp;
}

public int peekFront() {
    return queueArray[front];
}

public int peekRear() {
    return queueArray[front];
}

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

public boolean isFull() {
    return (nItems == maxSize);
}

public int size() {
    return nItems;
}

}

1 个答案:

答案 0 :(得分:0)

这些都是非常简单的修复!对于队列,您需要将maxSize设置为11.这将允许您正确打印队列中的每个数字,而无需在前面打印11。简而言之,假设队列中有11个元素,则最大队列大小为11。

public class Queue {

private int maxSize = 11;
private int[] queueArray;
private int front;
private int rear;
private int nItems;
}

现在,就你的堆栈而言,这也是一个非常简单的修复。截至目前,当堆栈为空时,您的代码返回top == maxSize(或10)。这导致pop函数出现问题。但是,当堆栈为空时,top应该等于-1。因此,请确保在isEmpty()函数中设置top == -1。您还应该将int maxSize = 10更改为int maxSize = 11并在pop()函数中进行必要的更改,以及这是发生错误的位置。最后,确保将堆栈初始化中的值从10更改为11.我已经更改了代码,现在应该可以正常工作了!请享用!

public class Stack {

private int[] stackArray;   // Stack array of objects
private int maxSize = 11;       // Max size of the stack
private int top;                    // top integer of stack

public Stack(int maxSize) {
    stackArray = new int[maxSize];
    top = -1;
}

public boolean isEmpty() {
    return (top == -1); // true if stack is empty
}

public boolean isFull() {
    return (top == maxSize); // true if full
}

public int size() {
    return (top-1); // Return the # of items
}

public void push(int item) // put element into array
{
    if (!isFull()) {
        top++;
        stackArray[top] = item;
    }

}
public Object pop() {
    if (!isEmpty()) {
        return stackArray[top--];
    } else
        throw new IllegalStateException("Stack empty");
}

public Object peek() {
    return stackArray[top+1];
}

public String toString() {
    return "Size: " + maxSize + " Capcaity: " + top; // complete: show the size and capacity
}