栈(First-in,Last-out)
StackX.java
package test.stack;public class StackX { private int maxSize; private int top; private String[] stackArr ; public StackX(int size) { maxSize = size; stackArr = new String[maxSize]; top = -1; } public void push(String j){ stackArr[++top] = j; } public String pop(){ return stackArr[top--]; } public String seek(){ return stackArr[top]; } public boolean isEmpty(){ return (top == -1); } public boolean isFull(){ return (top ==(maxSize-1)); }}
StackApp.java
package test.stack;public class StackApp { /** * @param args */ public static void main(String[] args) {// StackX stack = new StackX(3);// stack.push("a");// stack.push("b");// stack.push("c");// while(!stack.isEmpty()){// System.out.println(stack.pop()); // }// String []arr = {"a","b","c"}; StackX stack = new StackX(arr.length); for(int i=0; i<arr.length; i++){ stack.push(arr[i]); } while(!stack.isEmpty()){ System.out.println(stack.pop()); } String hello ="Hello World"; String[] res = hello.split(" ");// for(int i=0; i<res.length; i++){// System.out.println(res[i]);// } StackX stack2 = new StackX(res.length); for(int i=0;i<res.length;i++){ stack2.push(res[i]); } while(!stack2.isEmpty()){ System.out.println(stack2.pop()); } }}