堆栈的ArrayList实现
堆栈的链表实现
堆栈的纯数组实现(只支持int类型数据,有待完善......)
-
Java code
-
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import java.util. * ; public class Stack{ private ArrayList pool = new ArrayList(); public Stack(){ } public Stack( int n){ pool.ensureCapacity(n); } public void clear(){ pool.clear(); } public boolean isEmpty(){ return pool.isEmpty(); } public Object pop(){ if (isEmpty()) throw new EmptyStackException(); return pool.remove(pool.size() - 1 ); } public void push(Object el){ pool.add(el); } public String toString(){ return pool.toString(); } } // end clss Stack
堆栈的链表实现
-
Java code
-
import java.util.LinkedList; import java.util.EmptyStackException; public class LIStack{ private LinkedList list=new LinkedList(); public LIStack(){ } public void clear(){ list.clear(); } public boolean isEmpty(){ return list.isEmpty(); } public Object topEl(){ if(isEmpty()) throw new EmptyStackException(); return list.getLast(); } public Object pop(){ if(isEmpty()) throw new EmptyStackException(); return list.removeLast(); } public void push(Object el){ list.addLast(el); } public String toString(){ return list.toString(); } }//end class LIStack
堆栈的纯数组实现(只支持int类型数据,有待完善......)
-
Java code
-
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /* *使用纯数组实现堆栈 */ public class ARStack{ private int [] a; private int MaxSize,nElem; public ARStack(){ MaxSize = 100 ; a = new int [MaxSize]; nElem = 0 ; } public void clear(){ nElem = 0 ; } public boolean isEmpty(){ return nElem == 0 ; } public int pop(){ return a[ -- nElem]; } public void push( int value){ a[nElem] = value; nElem ++ ; } public static void main(String[] args){ ARStack ar = new ARStack(); for ( int i = 10 ;i >= 0 ;i -- ){ ar.push(i); } while ( ! ar.isEmpty()){ System.out.println(ar.pop()); } } }