public class stract {
public static void main(String[] args) {
MyStack stack = new MyStack(20);
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack);
System.out.println(stack.peek());
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
}
}
class MyStack {
// 底层用数组实现
private long[] arr;
private int pop;
public MyStack() {
arr = new long[10];
pop = -1;
}
public MyStack(int maxsize) {
arr = new long[maxsize];
pop = -1;
}
public void push(long data) { //入栈
arr[++pop] = data;
}
public long pop() {//弹栈
return arr[pop--];
}
public long peek() {//查看栈顶数据
return arr[pop];
}
public String list() {//遍历数据
String str = "";
for (int i = 0; i <= pop; i++) {
str += arr[i] + " ";
}
return str;
}
public String toString() {
return list();
}
}