熟悉一下各种数据结构了 话不多说
package DataStruction;
public class MyStack {
private long[] stackArray;
private int top;
private int maxSize;
public MyStack(int num) {
maxSize = num;
top = -1;
stackArray = new long[maxSize];
}
public void push(long speNum) { //进入栈顶
stackArray[++top] = speNum;
}
public long pop() { //出栈
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public void traverse() { //从栈尾向上遍历
for (int i = 0; i <= top; i++) {
System.out.println("the result of" + i + " is: " + stackArray[i]);
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
MyStack stack = new MyStack(10);
stack.push(41);
stack.push(34);
stack.push(16);
stack.push(145);
stack.push(42);
stack.traverse();
while (!stack.isEmpty()) {
long value = stack.pop();
System.out.println(value);
}
System.out.println("已经结束所有数字");
stack.traverse();
}
}
<img src="https://img-blog.csdn.net/20150512214255657?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTU4NDIxNjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />