数据结构中的栈
Stack in data structure
我是数据新手 structures.I 尝试输入 5 个字符并使用 pop() 函数反转它。但数组中最后一个索引的值显示为空。谁能解释一下原因吗?
堆叠Class:
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();
}
}
输入
Value to add for stack
a
new value for index0 is a
Value to add for stack
b
new value for index1 is b
Value to add for stack
c
new value for index2 is c
Value to add for stack
d
new value for index3 is d
Value to add for stack
e
输出
Overflow Condition
Removed Index: 4is null
Removed Index: 3is d
Removed Index: 2is c
Removed Index: 1is b
Removed Index: 0is a
我想指出一些更改以简化您的代码。
您不需要在 main()
方法中创建新的 array
,因为您已经创建了 array
(用作 stack
) 在 Stack
class.
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
指向数组的最后一个索引,然后您可以 return 弹出的元素。
Input/Output如下图:
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
希望这能回答您的问题。
我是数据新手 structures.I 尝试输入 5 个字符并使用 pop() 函数反转它。但数组中最后一个索引的值显示为空。谁能解释一下原因吗?
堆叠Class:
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();
}
}
输入
Value to add for stack
a
new value for index0 is a
Value to add for stack
b
new value for index1 is b
Value to add for stack
c
new value for index2 is c
Value to add for stack
d
new value for index3 is d
Value to add for stack
e
输出
Overflow Condition
Removed Index: 4is null
Removed Index: 3is d
Removed Index: 2is c
Removed Index: 1is b Removed Index: 0is a
我想指出一些更改以简化您的代码。
您不需要在
main()
方法中创建新的array
,因为您已经创建了array
(用作stack
) 在Stack
class.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
指向数组的最后一个索引,然后您可以 return 弹出的元素。
Input/Output如下图:
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
希望这能回答您的问题。