【发布时间】:2020-11-14 23:55:17
【问题描述】:
我正在尝试检查数组是否溢出。 这是我正在使用的逻辑:
/*
store the length of the stackArray[] into a temp. int.
compare the size of the temp int to the stackArray[] via 3 tests.
If the stackArray[]'s size is greater than the int's size,
then say that the array is overflowing, and that it doesn't have any more space to enter elements into.
If the stackArray[]'s size is less than the int's size,
then say that the stackArray[] is underflowing and that it still has space to enter some elements into it.
If the stackArray[]'s size is equal to the int's size,
then say that the array is full.
*/
为了实现这个逻辑,这是我正在使用的方法:
public void overflowCheck(int temp)
{
if(stackArraySize/stackArray.length > 100)
{
System.out.println("stack is overflowing");
}
else if(stackArraySize/stackArray.length < 100)
{
System.out.println("stack is not full");
}
else
{
System.out.println("stack is full");
}
}
但是当我在我的数组上运行它时,我不断收到“堆栈未满。这是我的数组:
Data stackTest = new Data(10, false); //create a new stack & confirm it's not a queue
int stackSize = stackTest.size;
stackTest.push(10); //push 10 into the stack
stackTest.push(20); //push 20 into the stack
stackTest.push(30); //push 30 into the stack
stackTest.push(40); //push 40 into the stack
stackTest.push(50); //push 50 into the stack
stackTest.push(60); //push 60 into the stack
stackTest.push(70); //push 70 into the stack
stackTest.push(80); //push 80 into the stack
stackTest.push(90); //push 90 into the stack
stackTest.push(100); //push 100 into the stack
stackTest.push(110); //push 110 into the stack
stackTest.push(120); //push 120 into the stack
stackTest.overflowCheck(stackSize);
我应该得到“堆栈溢出”的输出,但我不断得到“堆栈未满”。
这是因为我的逻辑有缺陷还是我的执行有缺陷? 我该如何解决这个问题?
全班
int size; //initialize size
int stackArray[]; //initialize array
int top; //initialize top
int stackArraySize;
public Data(int size, boolean isArrayQueue) //constructor
{
if(isArrayQueue)
{
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
else
{
this.size = size;
this.stackArray = new int[size];
this.top = -1;
}
}
public boolean isFull() //check if it's full
{
return(size-1 == top);
}
public boolean isEmpty() //check if it's empty
{
return(top == -1);
}
public void push(int pushedElement) //push an element into the stack, as long as isFull is false
{
if(!isFull()) //check if the stack is already full
{
top++; //increment top
stackArray[top] = pushedElement; //set array[top] to the pushedElement
System.out.println("The pushed element is: " + pushedElement);
}
else //if the stack is full, tell the user.
{
System.out.println("The stack is full.");
}
}
public int pop() //pop an element from the stack, as long as isEmpty is false
{
if(!isEmpty()) //check is the stack is already empty.
{
int originalTop = top; //store original top value into originalTop
top--;//decrement top
System.out.println("The popped element is: " + stackArray[originalTop]);
return stackArray[originalTop]; //return the originalTop again
}
else //if the stack is empty, tell the user.
{
System.out.println("The stack is empty.");
return -1;
}
}
public int top() //peek into the stack & return the element on the top
{
if(!this.isEmpty()) //if the stack has values, return the top element.
{
return stackArray[top];
}
else //if the stack is empty, tell the user.
{
System.out.println("The stack is empty");
return -1;
}
}
【问题讨论】:
-
您的意思是检查
-
如果我使用模数而不是除法会不会是这样?
-
是的。我将 StackArraySize 设置为 temp。
-
您的 Data 类是如何实现的,为什么要传递数组的大小来检查溢出。数组的大小将始终相同。
-
我将 Data 类用作对象中心,在其中创建对象以在其上运行其他类。我正在传递数组的大小以检查我放入 stackArray 的元素 # 是否大于该大小。