/**
* 顺序结构栈
*
*/
public class SeqStack {
private final int SIZE=10;
private int[] stack=new int[SIZE];
private int top=0;
//入栈
public boolean push(int elem){
if(top==SIZE){
return false;
}else{
stack[top]=elem;
top++;
return true;
}
}
//是否为空
public boolean isEmpty(){
if(top==0){
return true;
}else
return true;
}
//是否为满
public boolean isFull(){
if(top==SIZE){
return true;
}else
return false;
}
//出栈
public int pop(){
int temp;
if(top==0){
return 0;
}else{
top--;
temp=stack[top];
return temp;
}
}
public static void main(String[] args) {
SeqStack stack=new SeqStack();
stack.push(2);
stack.push(3);
stack.push(7);
stack.push(5);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
* 顺序结构栈
*
*/
public class SeqStack {
private final int SIZE=10;
private int[] stack=new int[SIZE];
private int top=0;
//入栈
public boolean push(int elem){
if(top==SIZE){
return false;
}else{
stack[top]=elem;
top++;
return true;
}
}
//是否为空
public boolean isEmpty(){
if(top==0){
return true;
}else
return true;
}
//是否为满
public boolean isFull(){
if(top==SIZE){
return true;
}else
return false;
}
//出栈
public int pop(){
int temp;
if(top==0){
return 0;
}else{
top--;
temp=stack[top];
return temp;
}
}
public static void main(String[] args) {
SeqStack stack=new SeqStack();
stack.push(2);
stack.push(3);
stack.push(7);
stack.push(5);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
顺序结构栈实现
本文介绍了一个简单的顺序结构栈的Java实现。该栈使用固定大小的数组作为存储结构,并提供了基本的操作如入栈、出栈、判断栈是否为空及是否已满等。通过示例展示了如何操作顺序栈。
1010

被折叠的 条评论
为什么被折叠?



