栈的抽象类型描述(ADT):
public interface Stack { // Stack values are stacks whose elements are arbitrary objects. // 栈元素类型为任意对象 //Modifiers 修改器--修改栈 public void clear(); public void push(Object obj); public Object pop(); // 删除并返回栈顶元素 //Accessors 访问器--获取栈信息 public boolean isEmpty(); public int size(); public Object peek(); // 返回栈顶元素 }
栈的数组实现:
// 数组实现栈 public class ArrayStack implements Stack { private static final int DEFAULT_SIZE = 10; // 指定--默认栈深度 private Object[] array; private int top; // 指向栈顶指针/游标 // public ArrayStack(){} public ArrayStack() { // 初始化 array = new Object[DEFAULT_SIZE]; top = 0; } @Override public void clear() { for(int i = 0; i < top; i++) { array[i] = null; } // for(Object i:array) { // i = null; // } top = 0; } @Override public boolean isEmpty() { return top == 0; } @Override <span style="color:#660cc;">public Object peek() { if (top == 0) { // 操作空栈时抛出异常,异常可以自己编写(只要继承Exception) throw new IllegalStateException(); } return array[top - 1]; }</span> @Override <span style="color:#660cc;">public Object pop() { if (top == 0) { throw new IllegalStateException(); } Object obj = array[--top]; array[top] = null; return obj; }</span> @Override public void push(Object obj) { if(top == array.length) { expand(); } array[top ++] = obj; // 只能向栈顶插入数据 } @Override public int size() { return top; } private void expand() { // 辅助方法,数组长度加倍 Object[] newArray = new Object[2*array.length]; for(int i = 0; i < array.length; i++) { newArray[i] = array[i]; } array = newArray; } public String toString() { // StringBuilder str = new StringBuilder(); String buf = "[ "; for (int i = 0; i < top; i++) { if(i > 0) { // 在每个元素后面加“,”分隔 buf += ", "; } buf += array[i]; } buf += " ]"; return buf; } }
栈--链表实现:
public class LinkedStack implements Stack { // 链表节点类 private static class SLLNode { private Object data; private SLLNode next; public SLLNode() { } public SLLNode(Object data) { this.data = data; } public SLLNode(Object data, SLLNode next) { this.data = data; this.next = next; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public SLLNode getNext() { return next; } public void setNext(SLLNode next) { this.next = next; } public String toString() { return data.toString(); } } private SLLNode top; private int count; public LinkedStack() { // 初始化 空栈,栈深度任意 clear(); } @Override public void clear() { top = null; count = 0; } @Override public boolean isEmpty() { return top == null; } @Override public Object peek() { if (top == null) { throw new IllegalStateException(); } return top.data; } @Override public Object pop() { if (top == null) { throw new IllegalStateException(); } Object val = top.data; top = top.next; count--; return val; } @Override public void push(Object obj) { top = new SLLNode(obj, top); count++; } @Override public int size() { return count; } public String toString() { String buf = "[ "; for (SLLNode curr = top; curr != null; curr = curr.next) { if (curr != top) { buf += ", "; } buf += curr.data; } buf += " ]"; return buf; } }
测试类:
public class ArrayStackTest { /** * @param args */ public static void main(String[] args) { Stack stack = new ArrayStack(); stack.push("Array"); stack.push("Number"); stack.push("String"); stack.push("Object"); System.out.println("栈顶:" + stack.peek().toString()); System.out.println(stack.toString()); // System.out.println("删除栈顶后:" + stack.pop().toString()); // System.out.println(stack.toString()); // stack.clear(); System.out.println(stack.size()); System.out.println(stack.isEmpty()); System.out.println(stack.toString()); } }