棧
定義:
棧是一種先進后出的數據結構,我們把允許插入和刪除的一端稱為棧頂,另一端稱為棧底,不含任何元素的棧稱為空棧
棧的java代碼實現:
基於數組:
1 import org.junit.jupiter.api.Test; 2 3 /** 4 * 用數組實現棧 5 * @author wydream 6 * 7 */ 8 9 public class ArrayStack<T> { 10 11 private T data[]; 12 private int maxSize; 13 private int top; 14 15 16 //初始化棧 17 public ArrayStack(int maxSize) { 18 this.maxSize=maxSize; 19 data=(T[])new Object[maxSize]; 20 this.top=-1; 21 } 22 23 //判斷棧是否為空 24 public boolean isEmpty() { 25 return (top==-1); 26 } 27 28 //判斷棧是否已經滿了 29 public boolean isFull() { 30 return (top==maxSize-1); 31 } 32 33 34 //壓棧 35 public boolean push(T value) { 36 if(isFull()) { 37 return false; 38 } 39 top++; 40 data[top]=value; 41 return true; 42 } 43 44 45 //取出棧頂元素 46 public T pop() { 47 if(isEmpty()) { 48 return null; 49 } 50 T tmp=data[top]; 51 data[top]=null; 52 top--; 53 return tmp; 54 } 55 56 //============測試代碼============ 57 public static void main(String[] args) { 58 ArrayStack<String> as=new ArrayStack<String>(4); 59 as.push("anhui"); 60 as.push("shanghai"); 61 as.push("beijing"); 62 as.push("nanj"); 63 //測試棧已經滿了的情況 64 System.out.println(as.push("aa")); 65 for(int i=0;i<4;i++) { 66 System.out.println(as.pop()); 67 } 68 } 69 70 }
基於鏈表:
1 import org.junit.jupiter.api.Test; 2 3 /** 4 * 基於鏈表實現的棧 5 * @author wydream 6 * 7 */ 8 9 public class NodeStack<T> { 10 11 private Node<T> top=null;//棧頂 12 public NodeStack() { 13 this.top=null; 14 } 15 16 //判斷棧是否為空 17 public boolean isEmpty() { 18 if(top!=null) { 19 return false; 20 } 21 return true; 22 } 23 24 //壓棧 25 public boolean push(T value) { 26 Node<T> node=new Node<T>(value); 27 node.setNext(top); 28 top=node; 29 return true; 30 } 31 32 //出棧 33 public T pop() { 34 if(top==null) { 35 return null; 36 } 37 T tmp=top.data; 38 top=top.getNext(); 39 return tmp; 40 } 41 //取出棧頂的值 42 public T peek() { 43 if(isEmpty()) { 44 return null; 45 } 46 return top.data; 47 } 48 49 50 class Node<T>{ 51 private T data;//數據 52 private Node<T> next;//指向下一個節點的指針 53 //初始化鏈表 54 public Node(T data) { 55 this.data=data; 56 } 57 //獲取下一個節點 58 public Node<T> getNext(){ 59 return this.next; 60 } 61 //設置下一個節點 62 public void setNext(Node<T> n) { 63 this.next=n; 64 } 65 //獲取節點數據 66 public T getData() { 67 return this.data; 68 } 69 //設置節點數據 70 public void setData(T d) { 71 this.data=d; 72 } 73 74 } 75 76 public static void main(String[] args) { 77 78 NodeStack<String> ns=new NodeStack<String>(); 79 80 //測試是否為空 81 System.out.println("=======是否為空======"); 82 System.out.println(ns.isEmpty()); 83 System.out.println("============="); 84 //壓棧測試 85 System.out.println("=======壓棧======"); 86 ns.push("北京"); 87 ns.push("上海"); 88 ns.push("深證"); 89 ns.push("廣州"); 90 //是否為空 91 System.out.println("=======是否為空======"); 92 System.out.println(ns.isEmpty()); 93 System.out.println("============="); 94 95 System.out.println("=======出棧======="); 96 //出棧 97 System.out.println(ns.pop()); 98 System.out.println(ns.pop()); 99 System.out.println(ns.pop()); 100 System.out.println(ns.pop()); 101 System.out.println(ns.pop()); 102 103 //是否為空 104 System.out.println("=======是否為空======"); 105 System.out.println(ns.isEmpty()); 106 System.out.println("============="); 107 108 109 } 110 }
兩棧共享空間:
棧有個缺陷,必須事先確定數組的大小,這樣如果棧滿了的話,想在存儲元素就必須通過編程手段來擴充數組的容量,這樣就很麻煩。於是我們就設計一個數組,里面存放着兩個棧,共享這一個數組空間,這樣就可以充分利用空間。數組有兩個端點,兩個棧有兩個棧底,讓一個棧的棧底為數組的0下標,另一個棧的棧為數組的長度n-1處
代碼實現:
1 import javax.crypto.Mac; 2 3 /** 4 * 兩棧共享空間 5 * @author wydream 6 * 7 */ 8 9 public class DoubleStatk { 10 11 private final static int MAXSIZE=20; 12 private int[] stackElem; 13 private int top1; //將top1設置為指向棧1棧頂元素的存儲位置即數組下標0 14 private int top2; //將top2設置為指向棧2棧頂元素的存儲位置即數組下標n-1 15 16 public DoubleStatk() { 17 top1=-1; 18 top2=MAXSIZE; 19 stackElem=new int[MAXSIZE]; 20 } 21 22 //是否是空棧 23 public boolean isEmptyStack() { 24 if(top1==-1&&top2==MAXSIZE) { 25 return true; 26 } 27 return false; 28 } 29 30 //清空棧 31 public void clearStack() { 32 top1=-1; 33 top2=MAXSIZE; 34 } 35 36 //棧的長度 37 public int lengthStak() { 38 return (top1+1)+(MAXSIZE-top2); 39 } 40 41 //獲取top1的元素 42 public int getTop1Elem() { 43 if(top1==-1) { 44 return -1; 45 } 46 return stackElem[top1]; 47 } 48 49 //獲取top2的元素 50 public int getTop2Elem() { 51 if(top2==MAXSIZE) { 52 return -1; 53 } 54 return stackElem[top2]; 55 } 56 57 //壓棧 58 public void stackPush(int stackNumber,int e) { 59 //如果棧已經滿了 60 if(top1+1==top2) { 61 System.out.println("棧已滿"); 62 return; 63 } 64 if(stackNumber==1) { 65 top1+=1; 66 stackElem[top1]=e; 67 return; 68 } 69 if(stackNumber==2) { 70 top2-=1; 71 stackElem[top2]=e; 72 return; 73 } 74 75 } 76 77 //出棧 78 public int stackPop(int stackNumber) { 79 int rs; 80 if(isEmptyStack()) { 81 System.out.println("棧為空"); 82 return -1; 83 } 84 if(stackNumber==1) { 85 rs= stackElem[top1]; 86 top1--; 87 }else if(stackNumber==2) { 88 rs=stackElem[top2]; 89 top2++; 90 }else { 91 System.out.println("輸入數據有誤"); 92 return -1; 93 } 94 return rs; 95 } 96 97 public void stackTraverse() { 98 System.out.println("此時,棧中的元素為:"); 99 int i=0; 100 while(i<=top1) { 101 System.out.println(stackElem[i++]+" "); 102 } 103 i=top2; 104 while(i<MAXSIZE) { 105 System.out.println(stackElem[i++]+" "); 106 } 107 System.out.println(); 108 } 109 110 111 public static void main(String[] args) { 112 DoubleStatk seqStack=new DoubleStatk(); 113 114 //1壓棧 115 for(int j=1;j<=5;j++) { 116 seqStack.stackPush(1,j); 117 } 118 //2壓棧 119 for(int i=MAXSIZE;i>=MAXSIZE-2;i--) { 120 seqStack.stackPush(2, i); 121 } 122 //輸出 123 seqStack.stackTraverse(); 124 System.out.println("棧的長度為:"+seqStack.lengthStak()); 125 126 seqStack.stackPop(2); 127 seqStack.stackTraverse(); 128 System.out.println("棧1的棧頂元素為: " + seqStack.getTop1Elem()); 129 System.out.println("棧2的棧頂元素為: " + seqStack.getTop2Elem()); 130 System.out.println("棧的長度為: " + seqStack.lengthStak()); 131 132 for (int i = 6; i <= MAXSIZE-2; i++) { 133 seqStack.stackPush(1,i); 134 } 135 seqStack.stackTraverse(); 136 System.out.println("棧1的棧頂元素為: " + seqStack.getTop1Elem()); 137 System.out.println("棧2的棧頂元素為: " + seqStack.getTop2Elem()); 138 System.out.println("棧頂元素為: " + seqStack.getTop2Elem()); 139 System.out.println("棧的長度為: " + seqStack.lengthStak()); 140 141 System.out.println("棧是否為空: " + seqStack.isEmptyStack()); 142 seqStack.clearStack(); 143 System.out.println("棧是否為空: " + seqStack.isEmptyStack()); 144 } 145 146 147 }