您的位置:首页 > 编程语言 > Java开发

java实现顺序栈

2016-07-21 18:26 447 查看
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");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java class