Java中的栈

来源:互联网 发布:网络摄像机ip修改器 编辑:程序博客网 时间:2024/06/08 10:18

栈是先进后出的数据结构,下面的代码通过Java实现栈式储存


/** * 栈 */public class JavaStack {private String[] stackArray;private int stackSize;//大小private int topOfStack = -1;// 头指针/** * 初始化大小 * @param size */public JavaStack(int size){stackSize = size;stackArray = new String[stackSize];Arrays.fill(stackArray, "-1");// -1作为没有内容的标记}/** * 入栈 * @param element */public void push(String element){// 栈未满if(topOfStack + 1 < stackSize){stackArray[++topOfStack] = element;System.out.println("push: "+stackArray[topOfStack]+" is added to the stack");display();} else{// 栈已经满System.out.println("Stack is full");}}/** * 出栈 */public String pop(){if(topOfStack >= 0){System.out.println("pop: "+stackArray[topOfStack]+" is removed from the stack");stackArray[topOfStack] = "-1";// 栈顶元素置为-1,作为无内容的标记return stackArray[topOfStack --];} else{System.out.println("pop: stack is empty");return "-1";}}/** * 查找栈顶元素 * @return */public String peek(){display();System.out.println("peek: "+stackArray[topOfStack]+" is at the top of the stack");return stackArray[topOfStack];}public static void main(String[] args) {JavaStack stack = new JavaStack(10);stack.display();stack.push("8");stack.push("9");stack.push("10");stack.peek();stack.pop();stack.display();stack.pop();stack.display();stack.pop();}/** * 格式化输出 */public void display() {for(int n = 0; n < 61; n++) System.out.print("-");System.out.println();for(int n = 0; n < stackSize; n++){System.out.format("| %2s"+"  ", n);}System.out.println("|");for(int n = 0; n < 61; n++) System.out.print("-");System.out.println();for(int n = 0; n < stackSize; n++){if(!stackArray[n].equals("-1")){System.out.format("| %2s"+"  ", stackArray[n]);} else{System.out.format("| %2s"+"  ", "");}}System.out.println("|");for(int n = 0; n < 61; n++) System.out.print("-");System.out.println();}}</span>


运行结果:

-------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|     |     |     |     |     |     |     |     |     |     |-------------------------------------------------------------push: 8 is added to the stack-------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|  8  |     |     |     |     |     |     |     |     |     |-------------------------------------------------------------push: 9 is added to the stack-------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|  8  |  9  |     |     |     |     |     |     |     |     |-------------------------------------------------------------push: 10 is added to the stack-------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|  8  |  9  | 10  |     |     |     |     |     |     |     |--------------------------------------------------------------------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|  8  |  9  | 10  |     |     |     |     |     |     |     |-------------------------------------------------------------peek: 10 is at the top of the stackpop: 10 is removed from the stack-------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|  8  |  9  |     |     |     |     |     |     |     |     |-------------------------------------------------------------pop: 9 is removed from the stack-------------------------------------------------------------|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |-------------------------------------------------------------|  8  |     |     |     |     |     |     |     |     |     |-------------------------------------------------------------pop: 8 is removed from the stack


0 0