package com.suredata.MQTT;
public class MyStack {
private int top;//栈顶
private int maxSize;//栈的最大容量
// private int size;//栈的大小
private Object object[];
public MyStack() {
//初始化栈
top=-1;
maxSize=5;
//size=0;
object=new Object[maxSize];
}
public MyStack(int capacity) {
//初始化栈
top=-1;
maxSize=5;
//size=0;
int maxValue=Math.max(maxSize,capacity);
object=new Object[maxValue];
}
public Object getStackTopValue(){
if(!isEmpty()){
return object[top];
}else return "空栈";
}
public boolean isEmpty(){
if(this.top==-1){
return true;
}else {
return false;
}
}
public boolean isFull(){
if(this.top==this.maxSize-1){
return true;
}else {
return false;
}
}
public int size(){
return this.top+1;
}
public void push(Object obj){
if(!isFull()){
this.object[++top]=obj;
}else System.out.println("栈满");
}
public void printStack(){
for(int i=0;i<size();i++){
System.out.print(this.object[i]+"\t");
}
}
public static void main(String[] args) {
MyStack myStack=new MyStack(3);
myStack.push("hello");
myStack.push(true);
myStack.push(23.32);
myStack.push("hello2");
myStack.push("hello3");
myStack.push("hello4");
System.out.println(myStack.getStackTopValue());
System.out.println(myStack.size());
myStack.printStack();
}
}