01.棧由簡單數據實現php
02.棧由動態數組實現node
03.棧由鏈表實現git
04.Android棧Stack源碼分析github
05.建立增強版自定義棧面試
00.棧基礎介紹segmentfault
02.棧的常見操做markdown
04.使用棧實現字符串逆序源碼分析
首先看一下實現的代碼
public class ArrayStack{ private static final String TAG = "ArrayStack"; private Object[] contents; private int top = -1; private int bottom = -1; private int SIZE = 10;//有一個初始值大小 public ArrayStack(){ contents = new Object[SIZE]; top = -1; } public int push(Object obj) throws Exception { if (top > SIZE) throw new Exception("小楊逗比,棧已經滿了!"); top++; contents[top] = obj; return top; } public Object pop() throws Exception{ if (top == bottom) throw new Exception("小楊逗比,棧已經空了!"); Object obj = contents[top]; contents[top] = null; top--; return obj; } public boolean isEmpty(){ return top == bottom; } public int getSize(){ return top + 1; } public void display() throws Exception{ if (getSize() == 0) throw new Exception("空棧!"); for (int i=getSize()-1;i>=0;i--){ System.out.print(contents[i].toString() + "->"); } System.out.println(""); } } public void test{ ArrayStack as = new ArrayStack(); //as.display(); as.push("小楊逗比"); as.push("瀟湘劍雨"); as.push("yc"); as.push("逗比"); as.push("aaa"); as.push("ertte"); as.push("hello"); as.display(); as.pop(); System.out.println(as.getSize()); as.pop(); as.display(); }
可能會出現的問題
性能和侷限性分析
可能首先會想到,每次將數組大小增長1或者減少1,將會怎麼樣?
這樣作存在極大問題
基於動態數據實現棧的代碼以下所示
class DynArrayStack{ private int top; private int capacity; private int[] array; private void doubleStack(){ int[] newArray=new int[capacity*2]; System.arraycopy(array,0,newArray,0,capacity); capacity=capacity*2; array=newArray; } public DynArrayStack(){ top=-1; capacity=1; array=new int[capacity]; } public boolean isEmpty(){ return (top==-1); } public boolean isStackFull(){ return (top==capacity-1); } public void push(int date){ if(isStackFull()){ doubleStack(); } array[++top]=date; } public int pop(){ if(isEmpty()){ System.out.println("Stack Empty"); return 0; }else { return array[top--]; } } public void deleteStack(){ top=-1; } } public class Main { public static void main(String[] args) { // write your code here DynArrayStack dynArrayStack=new DynArrayStack(); dynArrayStack.push(1); dynArrayStack.push(2); dynArrayStack.push(3); System.out.println(dynArrayStack.pop()); System.out.println(dynArrayStack.pop()); System.out.println(dynArrayStack.pop()); } }
性能
存在侷限性
入棧的順序是:1 2 3 4 5,那麼保證出棧的順序是5 4 3 2 1,以此類推讓head節點指向棧頂節點保證讓其倒序輸出。
public class MyStack<T> { private T data; private MyStack<T> next; public MyStack(T data, MyStack<T> next) { this.data = data; this.next = next; } public T getData() { return data; } public void setData(T data) { this.data = data; } public MyStack<T> getNext() { return next; } public void setNext(MyStack<T> next) { this.next = next; } } public class LinkStack<N> { private MyStack<N> head; private MyStack<N> tail; private Integer size=0; public MyStack<N> getHead() { return head; } public void setHead(MyStack<N> head) { this.head = head; } public MyStack<N> getTail() { return tail; } public void setTail(MyStack<N> tail) { this.tail = tail; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } public void addStack(N data){ MyStack<N> node = new MyStack<>(data,null); if(headIsNull()){ head = node; tail = node; size++; }else{ //新加入的node是:(data,null) 讓這個新的node的next指向初始的head節點 head變爲(data,head)) node.setNext(head); head = node; size++; } } public N outStack(){ if(size>0){ N outData = head.getData(); head = head.getNext(); return outData; }else{ throw new RuntimeException("棧裏無元素!"); } } public boolean headIsNull(){ if(head == null){ return true; } return false; } }
測試一下
public void test() { LinkStack<Integer> linkStack = new LinkStack<>(); linkStack.addStack(1); linkStack.addStack(2); linkStack.addStack(3); linkStack.addStack(4); linkStack.addStack(5); for(int i=0;i<linkStack.getSize();i++){ System.out.println(linkStack.outStack()); } }
在Android中,對於activity使用棧stack進行管理的,下面看看棧源代碼。
public class Stack<E> extends Vector<E> { /** * 建立一個空的棧對象 */ public Stack() { } /** * 將對象推送到此堆棧的頂部。 */ public E push(E item) { addElement(item); return item; } /** * 移除此堆棧頂部的對象,並將該對象做爲此函數的值返回。 */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * 查看此堆棧頂部的對象,而不將其從堆棧中移除。 */ public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); } /** * 判斷是不是空棧 */ public boolean empty() { return size() == 0; } /** * 返回對象位於此堆棧上的基於1的位置。 */ public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } private static final long serialVersionUID = 1224463164541339165L; }
一個能自動擴容,第二個能存儲各類不一樣類型的數據,解決辦法以下:
public class ArrayStack { //存儲元素的數組,聲明爲Object類型能存儲任意類型的數據 private Object[] elementData; //指向棧頂的指針 private int top; //棧的總容量 private int size; //默認構造一個容量爲10的棧 public ArrayStack(){ this.elementData = new Object[10]; this.top = -1; this.size = 10; } public ArrayStack(int initialCapacity){ if(initialCapacity < 0){ throw new IllegalArgumentException("棧初始容量不能小於0: "+initialCapacity); } this.elementData = new Object[initialCapacity]; this.top = -1; this.size = initialCapacity; } //壓入元素 public Object push(Object item){ //是否須要擴容 isGrow(top+1); elementData[++top] = item; return item; } //彈出棧頂元素 public Object pop(){ Object obj = peek(); remove(top); return obj; } //獲取棧頂元素 public Object peek(){ if(top == -1){ throw new EmptyStackException(); } return elementData[top]; } //判斷棧是否爲空 public boolean isEmpty(){ return (top == -1); } //刪除棧頂元素 public void remove(int top){ //棧頂元素置爲null elementData[top] = null; this.top--; } /** * 是否須要擴容,若是須要,則擴大一倍並返回true,不須要則返回false * @param minCapacity
*/ public boolean isGrow(int minCapacity){ int oldCapacity = size; //若是當前元素壓入棧以後總容量大於前面定義的容量,則須要擴容 if(minCapacity >= oldCapacity){ //定義擴大以後棧的總容量 int newCapacity = 0; //棧容量擴大兩倍(左移一位)看是否超過int類型所表示的最大範圍 if((oldCapacity<<1) - Integer.MAX_VALUE >0){ newCapacity = Integer.MAX_VALUE; }else{ newCapacity = (oldCapacity<<1);//左移一位,至關於*2 } this.size = newCapacity; int[] newArray = new int[size]; elementData = Arrays.copyOf(elementData, size); return true; }else{ return false; } } } ```