首先来了解下栈的特点:
First in last out :先进后出
包扩入栈,出栈。以及对栈空,栈满的判断
这里包括了两种实现方法:
数组实现
链表实现
数组实现:
package yaobo.stack; /** * 使用数组实现堆栈,包括入栈、出栈、获取堆栈长度、 * @author yaobo */ public class Stack { Object[] data; int maxSize; //栈顶位置 int top; public Stack(int maxSize) { this.maxSize = maxSize; data = new Object[maxSize]; top = -1; } /** * 获取堆栈长度 * @return 堆栈长度 */ public int getSize() { return maxSize; } /** * 返回栈中元素的个数 * @return 栈中元素的个数 */ public int getElementCount() { return top; } /** * 判断栈空 * @return 栈空 */ public boolean isEmpty() { return top == -1; } /** * 判断栈满 * @return 栈满 */ public boolean isFull() { return top+1 == maxSize; } /** * 依次加入数据 * @param data 要加入的数据通信 * @return 添加是否成功 */ public boolean push(Object data) { if(isFull()) { System.out.println("栈已满!"); return false; } this.data[++top] = data; return true; } /** * 从栈中取出数据 * @return 取出的数据 */ public Object pop() throws Exception{ if(isEmpty()) { throw new Exception("栈已空!"); } return this.data[top--]; } /** * 返回栈顶元素 * @return */ public Object peek() { return this.data[getElementCount()]; } public static void main(String[] args) throws Exception { Stack stack=new Stack(1000); stack.push(new String("1")); stack.push(new String("2")); stack.push(new String("3")); stack.push(new String("4")); stack.push(new String("5")); System.out.println(stack.peek()); while(stack.top>=0) { System.out.println(stack.pop()); } } }
链表实现:
package yaobo.stack; /** * 表示链表的一个节点 * @author yaobo */ /** * 用链表实现堆栈 * @author Adair */ public class ListStack { Node header; //栈顶元素 int elementCount;// 栈内元素个数 int size;// 栈的大小 //以这个内部类来表示链表 class Node{ Object element; Node next; public Node(Object element) { this(element, null); } public Node(Object element, Node n) { this.element = element; next = n; } public Object getElement() { return element; } public void setElement(Object element) { this.element = element; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } /** * 构造函数,构造一个空的堆栈 */ public ListStack() { header = null; elementCount = 0; size = 0; } /** * 通过构造器 自定义栈的大小 * @param size 栈的大小 */ public ListStack(int size) { header = null; elementCount = 0; this.size = size; } /** * 设置堆栈大小 * @param size 堆栈大小 */ public void setSize(int size) { this.size = size; } /** * 设置栈顶元素 * @param header 栈顶元素 */ public void setHeader(Node header) { this.header = header; } /** * 获取堆栈长度 * @return 堆栈长度 */ public int getSize() { return size; } /** * 返回栈中元素的个数 * @return 栈中元素的个数 */ public int getElementCount() { return elementCount; } /** * 判断栈是否为空 * @return 如果栈是空的,返回真,否则,返回假 */ public boolean isEmpty() { if (elementCount == 0) return true; return false; } /** * 判断栈满 * @return 如果栈是满的,返回真,否则,返回假 */ public boolean isFull() { if (elementCount == size) return true; return false; } /** * 把对象入栈 * @param value 对象 */ public void push(Object value) { if (this.isFull()) { throw new RuntimeException("Stack is Full"); } header = new Node(value, header); elementCount++; } /** * 出栈,并返回被出栈的元素 * @return 被出栈的元素 */ public Object pop() { if (this.isEmpty()) { throw new RuntimeException("Stack is empty"); } Object obj = header.getElement(); header = header.getNext(); elementCount--; return obj; } /** * 返回栈顶元素 * @return 栈顶元素 */ public Object peek() { if (this.isEmpty()) { throw new RuntimeException("Stack is empty"); } return header.getElement(); } public static void main(String[] args) throws Exception { ListStack stack=new ListStack(1000); stack.push(new String("1")); stack.push(new String("2")); stack.push(new String("3")); stack.push(new String("4")); stack.push(new String("5")); System.out.println(stack.peek()); while(stack.header!=null) { System.out.println(stack.pop()); } } }