栈的基本认识
程序员文章站
2022-04-12 10:32:12
...
public class Stack{
public static void main(String args[]){
Stackx theStack = new Stackx(10);
theStack.push(12);
theStack.push(13);
theStack.push(14);
theStack.push(15);
while(!theStack.isEmpty()){
long value=theStack.pop();
System.out.print(value+" ");
}
System.out.println();
}
}
//栈
class Stackx{
//三个必须元素
private int maxSize;
private long[] stackArray;
private int top;
//构造栈
public Stackx(int s){
maxSize=s;
stackArray=new long[maxSize];
top=-1;//栈为空
}
//栈所具有的动作
public void push(long j){
stackArray[++top]=j;//创建顶层空间并插入值
}
public long pop(){
return stackArray[top--];
}
public long peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top==-1);
}
public boolean isFull(){
return (top==maxSize-1);
}
}
上一篇: 如何使用vue全局与局部组件
下一篇: PyQt学习之路