package com.chenyu.zuo.stackAndQueue;
public class StackS {
public T[] arrays;
public int max;
public int items;
public int head;
public StackS(int size){//初始化
this.max=size;
arrays=(T[])new Object[max];
items=0;
head=0;
}
public void push(T t){//压栈
if(head>max-1){
throw new RuntimeException("栈已经满了,不能加入数据了");
}
arrays[head++]=t;
items++;
}
public T peek(){//得到顶端元素
return arrays[head-1];
}
public T pop(){//出栈
T t=arrays[--head];
items--;
return t;
}
public boolean isEmpty(){//判断是否为空
return items==0;
}
public void showAll(){//从栈顶到栈底打印出来
for(int i=items-1;i>=0;i--){
System.out.println(arrays[i]);
}
}
public boolean isFull(){//判断是否满了
return max==items;
}
public int size(){//返回栈的大小
return items;
}
public static void main(String[] args) {
StackS s=new StackS(5);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
System.out.println("speek"+s.peek());
System.out.println("size"+s.size());
s.showAll();
System.out.println("pop"+s.pop());
System.out.println("speek"+s.peek());
s.showAll();
}
}