如何解决数据结构中的堆栈
我是数据结构的新手。我尝试输入 5 个字符并使用 pop() 函数将其反转。但数组中最后一个索引的值显示为空。谁能解释一下原因?
堆栈类:
package stack;
public class Stack {
int top = 0;
String stack[] = new String[5];
public void push(String val) {
if(top >= 4) {
System.out.println("Overflow Condition");
}
else {
stack[top] = val;
System.out.println("new value for index"+top+" " +"is"+" "+stack[top]);
top++;
}
}
public void pop() {
String output ;
if(top == -1) {
System.out.println("Underflow Condition");
}
else {
for(int i= top; i >= 0; i--) {
output = stack[top];
System.out.println("Removed Index:"+" "+(top+1)+"is"+ output);
top--;
}
}
}
public void peek() {
String output;
if(top >= 4) {
System.out.println("Overflow Condition");
}
else {
output = stack[top];
}
}
}
主程序:
package stack;
import java.util.Scanner;
public class StackHome {
public static void main(String[] args) {
// Todo Auto-generated method stub
Scanner inp = new Scanner(system.in);
Stack obj = new Stack();
String arr[] = new String[5];
for(int i=0; i<5; i++) {
System.out.println("Value to add for stack");
String value = inp.next();
arr[i] = value;
obj.push(arr[i]);
}
obj.pop();
//obj.peek();
}
}
输入
要为堆栈添加的值
一个
index0 的新值是 a
为堆栈添加的值
乙
index1 的新值是 b
为堆栈添加的值
c
index2 的新值是 c
为堆栈添加的值
d
index3 的新值是 d
为堆栈添加的值
溢出条件
删除索引:4is null
删除索引:3is d
删除索引:2is c
删除索引:1is b
移除索引:0is a
解决方法
我想指出一些更改以简化您的代码。
-
您不需要在
array
方法中创建新的main()
,因为您已经创建了array
(用作stack
) 在Stack
类中。public static void main(String[] args) { Scanner inp = new Scanner(System.in); Stack obj = new Stack(); //this loop is for pushing elements into the stack for (int i = 0; i < 5; i++) { System.out.println("Value to add for stack"); String value = inp.next(); //you only need to call the push() method to push the element into the array created in the Stack class. obj.push(value); } //this loop is for popping the elements from the stack for (int i = 0; i < 5; i++) { System.out.println("Value to remove from stack"); //you just need to call the pop() method to pop an element from the stack obj.pop(); } inp.close(); }
-
您的
pop()
方法不需要for
循环,因为它唯一的工作就是删除一个元素。您可以在main()
中运行一个循环来一个一个弹出元素,如我上面所示。所以你的pop()
方法可以修改如下:public String pop() { if (top == 0) { System.out.println("Underflow Condition"); } else { //no need of for loop here. top--; String output = stack[top]; System.out.println("Removed Index:" + " " + (top + 1) + "is" + output); return output; } return null; }
注意:一旦所有元素都被推入 stack
,top
的值将是 5,这是数组的 length
所以你需要首先使用 top
递减 top--
的值,使 top
指向数组的最后一个索引,然后您可以返回弹出的元素。
输入/输出如下图:
Value to add for stack
a b c d e
new value for index0 is a
Value to add for stack
new value for index1 is b
Value to add for stack
new value for index2 is c
Value to add for stack
new value for index3 is d
Value to add for stack
new value for index4 is e
Value to remove from stack
Removed Index: 5ise
Value to remove from stack
Removed Index: 4isd
Value to remove from stack
Removed Index: 3isc
Value to remove from stack
Removed Index: 2isb
Value to remove from stack
Removed Index: 1isa
我希望这能解答您的疑问。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。