package none002;
//数据结构:数组,有序数组,栈,队列,图,二叉树,链表,堆。
//栈只允许访问一个数据项,即最后插入的数据项,移除这个数据项后才能访问倒数第二个插入的数据项,以此类推,这种机制在不少环境上是有用的。
public class StackApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StackX s=new StackX(20);
s.push(12);
s.push(14);
s.push(1);
s.push(12);
s.push(19);
s.push(10);
s.push(2);
s.push(4);
System.out.println(s.peek());
System.out.println(s.isFull());
while(!s.isEmpty()){
System.out.print(s.pop()+" ");
}
System.out.println();
System.out.println(s.peek());
}
}
class StackX{
int items;
long[] arrayStack;
int s;
StackX(int max){
s=max;
arrayStack=new long[s];
items=-1;
}
public void push(long value){
if(items==s-1) System.out.println("栈已经等于最大值了。");
else arrayStack[++items]=value;
}
public long pop(){
return arrayStack[items--];
}
public boolean isEmpty(){
return items==-1;
}
public long peek(){
if(items==-1) return 0;
else return arrayStack[items];
}
public boolean isFull(){
return items==s-1;
}
}
数据结构栈,基于数组
最新推荐文章于 2019-06-13 17:56:50 发布