Java代码之栈原理简介
下文笔者讲述java代码栈原理的相关说明,如下所示
栈原理: 下文笔者采用数组的方式 模拟java代码中栈原理例:
public class MyStack { private int maxSize; private long[] stackArray; private int top; public MyStack(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; } public void push(long j) { stackArray[++top] = j; } 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 static void main(String[] args) { MyStack stack = new MyStack(10); stack.push(10); stack.push(20); stack.push(30); stack.push(40); stack.push(50); while (!theStack.isEmpty()) { long value = stack.pop(); System.out.print(value); System.out.print(" "); } System.out.println(""); } } -----运行以上代码,将输出以下信息---- 50 40 30 20 10
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。