public class MyStack {
// 数组
int[] elements;
public MyStack() {
// 初始化栈
elements = new int[0];
}
/**
* 压入元素
*
* @param e
*/
public void push(int e) {
int[] newArr = new int[elements.length + 1];
for (int i = 0; i < elements.length; i++) {
newArr[i] = elements[i];
}
newArr[elements.length] = e;
elements = newArr;
}
/**
* 会删除元素
*
* @return
*/
public int pop() {
if (elements.length < 1) {
throw new RuntimeException("栈是空的");
}
int element = elements[elements.length - 1];
int[] newArr = new int[elements.length - 1];
for (int i = 0; i < elements.length - 1; i++) {
newArr[i] = elements[i];
}
elements = newArr;
return element;
}
/**
* 只是查询不删除元素
*
* @return
*/
public int peek() {
if (elements.length < 1) {
throw new RuntimeException("栈是空的");
}
// 0 - elements.length - 1
return elements[elements.length - 1];
}
}
测试:
public class TestMyStackDemo {
public static void main(String[] args) throws InterruptedException {
MyStack myStack = new MyStack();
myStack.push(9);
myStack.push(8);
myStack.push(7);
System.out.println(myStack.pop()); // 输出7
System.out.println(myStack.peek());// 输出8
}