package stackandque;
interface Stack{
//public void InitStack();
public void InitStack(SeStack stack);
public boolean StackEmpty(SeStack stack);
public void PushStack(SeStack stack,Object x);
public Object PopStack(SeStack stack);
public Object GetTop(SeStack stack);
public void printStack(SeStack stack);
}
class SeStack implements Stack{
public Object[] stackData ;
int top;
public static final int Maxsize = 20;
@Override
public void InitStack(SeStack stack) {
stack.top = -1;
stackData = new Object[Maxsize];
}
@Override
public boolean StackEmpty(SeStack stack) {
if (stack.top==-1) {
return true;
}
else
return false;
}
@Override
public void PushStack(SeStack stack,Object x) {
if (stack.top==Maxsize-1) {
System.out.println("栈满了,不能压栈");
}else {
stack.stackData[++stack.top]=x;
}
}
@Override
public Object PopStack(SeStack stack) {
if (stack.top==-1) {
System.out.println("栈为空,不能弹出来");
return 0;
}else {
return stack.stackData[stack.top--];
}
}
@Override
public Object GetTop(SeStack stack) {
if (stack.top==-1) {
return 0;
}else
{
return stack.stackData[stack.top];
}
}
@Override
public void printStack(SeStack stack) {
if (stack.StackEmpty(stack)) {
System.out.println("栈里面没的元素");
}else{
for(int i=0;i<=stack.top;i++){
System.out.println("第"+i+"个元素为:"+stack.stackData[i]);
}
}
}
}
public class SequenceStack {
public static void main(String[] args){
SeStack seustack = new SeStack();
seustack.InitStack(seustack);
seustack.PushStack(seustack, 1);
seustack.PushStack(seustack, 2);
seustack.PushStack(seustack, 3);
seustack.PushStack(seustack, 4);
Object a=seustack.GetTop(seustack);
System.out.println("栈顶元素为:"+a);
seustack.printStack(seustack);
System.out.println("出栈操作");
Object a1 = seustack.PopStack(seustack);
Object a2 = seustack.PopStack(seustack);
System.out.println("a1= "+a1+"\n"+"a2= "+a2);
seustack.printStack(seustack);
boolean a3 = seustack.StackEmpty(seustack);
System.out.println("a3为:"+a3);
}
}