英文:
Not displaying elements from stack
问题
以下是翻译好的内容:
我已经为堆栈编写了这个JAVA程序,但问题是我无法使用代码中的display()方法显示元素。
这是我的Stack类。
public class Stack {
// 成员变量
private int top;
private int size;
private int[] a;
private int i;
// 构造方法
public Stack() {
this.top = -1;
this.size = 5;
this.a = new int[size];
}
private boolean isempty() {
if (top == -1) {
System.out.println("堆栈下溢");
return true;
}
return false;
}
private boolean isfull() {
if (top == size - 1) {
System.out.println("堆栈上溢");
return true;
}
return false;
}
public void push(int n) {
if (isempty()) {
top += 1;
a[top] = n;
}
}
public void pop() {
if (isfull()) {
System.out.println("弹出:" + a[top]);
top -= 1;
}
}
public void display() {
for (i = 0; i < top; i++) {
System.out.println(a[i]);
}
}
}
这是主方法类。
public class Stackex {
public static void main(String[] args) {
Stack s = new Stack();
s.push(2);
s.push(4);
s.display();
}
}
当我尝试执行时,从isempty()获得的是"堆栈下溢",之后没有显示任何内容。请帮我指出我需要在代码中进行修正的地方。
英文:
So I have written this JAVA-program for stack and the problem is I cant display the elements using the display() method which I used in the code.
here is my Stack class.
public class Stack {
//members
private int top;
private int size;
private int[] a;
private int i;
//constructor
public Stack() {
this.top = -1;
this.size = 5;
this.a = new int[size];
}
private boolean isempty() {
if(top == -1) {
System.out.println("Stack Underflow");
return true;
}
return false;
}
private boolean isfull() {
if(top == size-1) {
System.out.println("Stack Overflow");
return true;
}
return false;
}
public void push(int n) {
if(isempty()) {
top+=1;
a[top] = n;
}
}
public void pop() {
if(isfull()) {
System.out.println("popped : "+ a[top]);
top-=1;
}
}
public void display() {
for(i=0;i<top;i++) {
System.out.println(a[i]);
}
}
}
here is the main method class
public class Stackex {
public static void main(String[] args) {
Stack s = new Stack();
s.push(2);
s.push(4);
s.display();
}
}
when I try to execute what is get is "Stack underflow" from the isempty() and nothing is displayed after that. Please help me where I need to correct this code.
答案1
得分: 0
你对 push
的调用只有在 isempty
返回 true
时才会执行推送操作。
因此,第一次推送成功并将顶部设为0。
第二次推送由于 isempty
返回 false 而未能执行。
因此,在你执行 display
时,你的 for
循环不会迭代,因为 top
是0。
英文:
Your call to push
only pushes if isempty
returns true
.
So the first push succeeds and sets top to 0.
The second push never happens because isempty
returns false.
So when you go to display
your for
loop never iterates, because top
is 0.
答案2
得分: 0
修复方法 push
和 display
:
public void push(int n) {
if (!isfull()) {
top += 1;
a[top] = n;
}
}
public void display() {
for (int i = 0; i <= top; i++) {
System.out.println(a[i]);
}
}
英文:
fix methods push
& display
:
public void push(int n) {
if (!isfull()) {
top += 1;
a[top] = n;
}
}
public void display() {
for (int i = 0; i <= top; i++) {
System.out.println(a[i]);
}
}
答案3
得分: 0
有一些编译错误需要先修复。
在方法 display
中,i
没有被声明,像这样修复它:
public void display() {
for (int i = 0; i < top; i++) { // 添加 int i = 0
System.out.println(a[i]);
}
}
然后将这部分代码进行更改:
private int[] a;
private int ijk]kkkk
更改为:
private int[] a;
现在你的问题是因为 isempty
方法返回了 false。所以将 push 方法更改为如下:
public void push(int n) {
if (!isfull()) {
top += 1;
a[top] = n;
}
}
当你添加一个元素时,你要检查栈是否不满。
英文:
There are some compilation errors to fix first.
In the method display
the i
is not declared, fix it like this:
public void display() {
for (int i = 0; i < top; i++) { // add int i = 0
System.out.println(a[i]);
}
}
Than change this:
private int[] a;;
private int ijk]kkkk
To this:
private int[] a;
Now your issue is because isempty
returns false. So change the push method like this:
public void push(int n) {
if (!isfull()) {
top += 1;
a[top] = n;
}
}
When you add an element you want to check that the stack is not full.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论