java实现顺序栈

时间:2023-03-09 21:57:08
java实现顺序栈

public class MyStack{
Object[] data;
int top;
int maxSize; public MyStack(int maxSize)
{
this.maxSize = maxSize;
this.top = -1;
this.data = new Object[this.maxSize];
} public boolean isEmpty()
{
return this.top == -1;
} public boolean isFull()
{
return this.top == maxSize - 1;
} public boolean push(Object e)
{
if(isFull()) return false;
this.data[++this.top]=e;
return true;
} public Object pop()
{
if(isEmpty()) return null;
return this.data[top--];
} public Object getTop()
{
if(isEmpty()) return null;
return this.data[top];
} public static void main(String[] args)
{
MyStack Stack = new MyStack(100);
for(int i=0;i<10;i++)
{
Stack.push(i+1);
}
while(Stack.isEmpty()==false)
{
System.out.print(Stack.pop()+"\t");
}
}
}