下面介紹三種實現stack的方法。儘管java中有stack類,但是已經被廢棄了。自己動手實現!!
1. 用鏈表實現
package com.jzm.stackQueueTree;public class LinkedStack<T> {private Node topNode; //引用鏈表中的第一個節點public LinkedStack(){ clear();}//end default constructorprivate class Node{ private T data; //棧的元素 private Node next; //指向下一個節點的串連 private Node(T data){ this.data = data; next = null; }//end constructor private T getData(){ return data; } private Node getNextNode(){ return next; } }private boolean isEmpty(){ return topNode == null; } private void clear(){ topNode = null; } public void push(T newEntry){ Node newNode = new Node(newEntry); newNode.next = topNode; topNode = newNode;} public T pop(){ T top = null; if(!isEmpty()){ top = topNode.getData(); topNode = topNode.getNextNode(); } return top; } public void display(){ Node point = topNode; System.out.println("從頂端元素開始:"); while(point != null){ System.out.println(point.getData()); point = point.next; } } public static void main(String[] args) { LinkedStack<Integer> linkedStack = new LinkedStack<Integer>(); for (int i = 0; i < 100; i++){ linkedStack.push(i); if (i %2 ==0) { linkedStack.pop();}} linkedStack.display(); }}
2. 用數組實現
package com.jzm.stackQueueTree;public class ArrayStack<T> { private T[] a; //用於存放值的數組private int topIndex; //棧的頂端索引private final static int MAX = 500; //預設棧大小 public ArrayStack(){ clear(); a = (T[]) new Object [MAX]; } public ArrayStack(int size){ //自訂棧大小 clear(); a = (T[])new Object[size];}private boolean isFull(){return (topIndex >= a.length-1);}public void clear(){ //清空棧 topIndex = -1; a = null;} public void push(T newEntry){ if (!isFull()) { a[++topIndex] = newEntry; }else { System.out.println("棧滿了 topIndex="+topIndex); }} public T pop(){ T top = null; if(topIndex >= 0){ top = a[topIndex]; topIndex--; }else{ System.out.println("棧為空白"); } return top; } public void display(){ System.out.println("ArrayStack從頂端元素開始:"); for(int i=topIndex; i>=0; i--){ System.out.println(a[i]); } } public static void main(String[] args) { ArrayStack<Integer> arrayStack = new ArrayStack<Integer>(); for (int i = 0; i < 100; i++){ arrayStack.push(i); if (i %2 == 0){ arrayStack.pop(); } } //end for arrayStack.display(); } }
3. 用vector實現
package com.jzm.stackQueueTree;import java.util.Vector;public class VectorStack<T>{private Vector <T> stack;public VectorStack (){stack = new Vector<T>();} public VectorStack (int maxsize){stack = new Vector<T>(maxsize);} public void push(T newEntry){ stack.addElement(newEntry);} public T pop(){ T top = null; if (!isEmpty()) { top = stack.lastElement(); stack.removeElementAt(stack.size()-1); } return top; } private boolean isEmpty(){ return stack.isEmpty(); } public void display(){ System.out.println("vectorStack從頂端元素開始:"); for (int i = stack.size()-1; i>=0; i--) { System.out.println(stack.elementAt(i)); } } public static void main(String[] args) { VectorStack<Integer> vectorStack = new VectorStack<Integer>(); for (int i = 0; i < 100; i++){ vectorStack.push(i); if (i %2 == 0){ vectorStack.pop(); } } //end for vectorStack.display(); } }