Java棧的三種實現方式(完整版)
阿新 • • 發佈:2020-12-07
java什麼是棧
系統中的堆、棧和資料結構堆、棧不是一個概念。可以說系統中的堆、棧是真實的記憶體物理區,資料結構中的堆、棧是抽象的資料儲存結構。
棧:實際上就是滿足後進先出的性質,是一種資料項按序排列的資料結構,只能在一端(稱為棧頂(top))對資料項進行插入和刪除。
棧區(stack)— 由編譯器自動分配釋放 ,存放函式的引數值,區域性變數的值等。其操作方式類似於資料結構中的棧。
棧的優勢是,存取速度比堆要快,僅次於直接位於CPU中的暫存器。但缺點是,存在棧中的資料大小與生存期必須是確定的,缺乏靈活性。
程式碼:
Stack的基本使用 初始化 Stack stack=new Stack 判斷是否為空 stack.empty() 取棧頂值(不出棧) stack.peek() 進棧 stack.push(Object); 出棧 stack.pop(); 例項: public class Test01 { public static void main(String[] args) { Stack stack=new Stack(); //1.empty()棧是否為空 System.out.println(stack.empty()); //2.peek()棧頂值 3.進棧push() stack.push(new Integer(1)); stack.push("b"); System.out.println(stack.peek()); //4.pop()出棧 stack.pop(); System.out.println(stack.peek()); } }
棧的主要操作
- void push(int data):將data(資料)插入棧
- int pop():刪除並返回最後一個插入棧的
棧的輔助操作
- int top():返回最後一個插入棧的元素,但不刪除
- int size():返回儲存在棧中的元素的個數
- int isEmpty():判斷棧中是否有元素
- int isStackFull():判斷棧中是否滿元素
實現
棧抽象資料型別有多種實現方式。下面是常用的方法:
- 基於簡單陣列的實現方法
- 基於動態陣列的實現方法
- 基於連結串列的實現方法
1)基於簡單陣列實現:
public class Stack{ private int size;//棧的大小 private int top;//棧頂元素的下標 private char[] stackArray;//棧的容器 public Stack(int size){ stackArray = new char[size]; top = -1; //初始化棧的時候由於棧內沒有元素,棧頂下標設為-1 this.size = size; } //入棧,棧頂的下標+1 public void push(char item){ stackArray[++top] = item; } //出棧,刪除棧頂元素,棧頂元素的下標-1 public int pop(){ return stackArray[top--]; } //檢視棧頂元素,不刪除 public char find(){ return stackArray[top]; } //判空 public boolean isEmpty(){ return (top == -1); } //判滿 public boolean isFull(){ return (top == size - 1); } public static void main(String[] args){ Stack stack = new Stack(5); stack.push('a'); stack.push('b'); stack.push('c'); stack.push('d'); char ch = stack.find(); System.out.println(ch); } }
執行結果:
d
2)基於動態陣列實現:
擴容——給我的感覺就像是在搬家,搬完了東西,還得把鑰匙給主人
public class Stack { public int size;//棧的大小 public int top;//棧頂元素的下標 public static char[] stackArray;//棧的容器 public Stack(int size){ stackArray = new char[size]; top = -1; //初始化棧的時候由於棧內沒有元素,棧頂下標設為-1 this.size = size; } //入棧,棧頂的下標+1 public void push(char item){ if(isFull()){ doubleStack(); } stackArray[++top] = item; } //模擬陣列的擴容 public void doubleStack(){ char[] newStackArray = new char[size*2]; for(int i = 0;i<size;i++){ newStackArray[i] = stackArray[i]; } size = size*2; stackArray = newStackArray; } //出棧,刪除棧頂元素,棧頂元素的下標-1 public int pop(){ if(isEmpty()){ System.out.println("Stack is Empty"); return 0; }else{ return stackArray[top--]; } } //檢視棧頂元素,不刪除 public char find(){ return stackArray[top]; } //判空 public boolean isEmpty(){ return (top == -1); } //判滿 public boolean isFull(){ return (top == size - 1); } public static void main(String[] args){ Stack stack = new Stack(5); stack.push('a'); stack.push('b'); stack.push('c'); stack.push('d'); stack.push('e'); stack.push('f'); stack.push('g'); stack.push('h');//一共8個元素 char ch = stack.find(); System.out.println(ch); System.out.println(stackArray.length); } }
執行結果:
h
10
3)基於連結串列實現
使用連結串列實現棧,通過在連結串列的表頭插入元素的方式實現push操作,刪除連結串列的表頭結點實現pop操作。表頭結點即棧頂結點
import java.util.EmptyStackException; class Link{ public char data; public Link next; public void show(){ System.out.println(data + " "); } public Link(char data){ this.data = data; } } public class Stack2 { Link head; public int size;//棧的大小 public int top;//棧頂元素的下標 public static char[] stackArray;//棧的容器 public void push(char data){ if(head == null){ head = new Link(data); }else{ Link node = new Link(data); node.next = head; head = node; } } public void pop(){ if(head == null){ throw new EmptyStackException(); }else{ char dat = head.data; head.show(); head = head.next; } } public int top(){ if(head == null){ return 0; }else{ return head.data; } } public boolean isEmpty(){ if(head == null) return true; return false; } public static void main(String[] args){ Stack2 stack = new Stack2(); stack.push('A'); stack.push('B'); stack.push('C'); stack.push('D'); stack.push('E'); stack.push('F'); stack.pop(); } }
執行結果:
F
到此這篇關於Java棧的三種實現方式(完整版)的文章就介紹到這了,更多相關Java棧內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!