Java棧的三種實(shí)現(xiàn)方式(完整版)
java什么是棧
系統(tǒng)中的堆、棧和數(shù)據(jù)結(jié)構(gòu)堆、棧不是一個(gè)概念。可以說(shuō)系統(tǒng)中的堆、棧是真實(shí)的內(nèi)存物理區(qū),數(shù)據(jù)結(jié)構(gòu)中的堆、棧是抽象的數(shù)據(jù)存儲(chǔ)結(jié)構(gòu)。
棧:實(shí)際上就是滿足后進(jìn)先出的性質(zhì),是一種數(shù)據(jù)項(xiàng)按序排列的數(shù)據(jù)結(jié)構(gòu),只能在一端(稱為棧頂(top))對(duì)數(shù)據(jù)項(xiàng)進(jìn)行插入和刪除。

棧區(qū)(stack)— 由編譯器自動(dòng)分配釋放 ,存放函數(shù)的參數(shù)值,局部變量的值等。其操作方式類似于數(shù)據(jù)結(jié)構(gòu)中的棧。
棧的優(yōu)勢(shì)是,存取速度比堆要快,僅次于直接位于CPU中的寄存器。但缺點(diǎn)是,存在棧中的數(shù)據(jù)大小與生存期必須是確定的,缺乏靈活性。
代碼:
Stack的基本使用
初始化
Stack stack=new Stack
判斷是否為空
stack.empty()
取棧頂值(不出棧)
stack.peek()
進(jìn)棧
stack.push(Object);
出棧
stack.pop();
實(shí)例:
public class Test01 {
public static void main(String[] args) {
Stack stack=new Stack();
//1.empty()棧是否為空
System.out.println(stack.empty());
//2.peek()棧頂值 3.進(jìn)棧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(數(shù)據(jù))插入棧
- int pop():刪除并返回最后一個(gè)插入棧的
棧的輔助操作
- int top():返回最后一個(gè)插入棧的元素,但不刪除
- int size():返回存儲(chǔ)在棧中的元素的個(gè)數(shù)
- int isEmpty():判斷棧中是否有元素
- int isStackFull():判斷棧中是否滿元素
實(shí)現(xiàn)
棧抽象數(shù)據(jù)類型有多種實(shí)現(xiàn)方式。下面是常用的方法:
- 基于簡(jiǎn)單數(shù)組的實(shí)現(xiàn)方法
- 基于動(dòng)態(tài)數(shù)組的實(shí)現(xiàn)方法
- 基于鏈表的實(shí)現(xiàn)方法
1)基于簡(jiǎn)單數(shù)組實(shí)現(xiàn):
public class Stack{
private int size;//棧的大小
private int top;//棧頂元素的下標(biāo)
private char[] stackArray;//棧的容器
public Stack(int size){
stackArray = new char[size];
top = -1; //初始化棧的時(shí)候由于棧內(nèi)沒(méi)有元素,棧頂下標(biāo)設(shè)為-1
this.size = size;
}
//入棧,棧頂?shù)南聵?biāo)+1
public void push(char item){
stackArray[++top] = item;
}
//出棧,刪除棧頂元素,棧頂元素的下標(biāo)-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);
}
}
運(yùn)行結(jié)果:
d
2)基于動(dòng)態(tài)數(shù)組實(shí)現(xiàn):
擴(kuò)容——給我的感覺(jué)就像是在搬家,搬完了東西,還得把鑰匙給主人
public class Stack {
public int size;//棧的大小
public int top;//棧頂元素的下標(biāo)
public static char[] stackArray;//棧的容器
public Stack(int size){
stackArray = new char[size];
top = -1; //初始化棧的時(shí)候由于棧內(nèi)沒(méi)有元素,棧頂下標(biāo)設(shè)為-1
this.size = size;
}
//入棧,棧頂?shù)南聵?biāo)+1
public void push(char item){
if(isFull()){
doubleStack();
}
stackArray[++top] = item;
}
//模擬數(shù)組的擴(kuò)容
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;
}
//出棧,刪除棧頂元素,棧頂元素的下標(biāo)-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個(gè)元素
char ch = stack.find();
System.out.println(ch);
System.out.println(stackArray.length);
}
}
運(yùn)行結(jié)果:
h
10
3)基于鏈表實(shí)現(xiàn)
使用鏈表實(shí)現(xiàn)棧,通過(guò)在鏈表的表頭插入元素的方式實(shí)現(xiàn)push操作,刪除鏈表的表頭結(jié)點(diǎn)實(shí)現(xiàn)pop操作。表頭結(jié)點(diǎn)即棧頂結(jié)點(diǎn)
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;//棧頂元素的下標(biāo)
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();
}
}
運(yùn)行結(jié)果:
F
到此這篇關(guān)于Java棧的三種實(shí)現(xiàn)方式(完整版)的文章就介紹到這了,更多相關(guān)Java棧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java使用數(shù)組和鏈表實(shí)現(xiàn)隊(duì)列示例
- java數(shù)組算法例題代碼詳解(冒泡排序,選擇排序,找最大值、最小值,添加、刪除元素等)
- Java基礎(chǔ)語(yǔ)法之二維數(shù)組詳解
- Java基礎(chǔ)之?dāng)?shù)組超詳細(xì)知識(shí)總結(jié)
- Java基礎(chǔ)之?dāng)?shù)組模擬循環(huán)隊(duì)列
- Java 自定義動(dòng)態(tài)數(shù)組方式
- java 定義長(zhǎng)度為0的數(shù)組/空數(shù)組案例
- 詳細(xì)總結(jié)Java堆棧內(nèi)存、堆外內(nèi)存、零拷貝淺析與代碼實(shí)現(xiàn)
- 深入了解Java虛擬機(jī)棧以及內(nèi)存模型
- 使用java一維數(shù)組模擬壓棧彈棧
- Java 利用棧來(lái)反轉(zhuǎn)鏈表和排序的操作
- Java異常日志堆棧丟失的原因與排查
- 教你怎么用Java數(shù)組和鏈表實(shí)現(xiàn)棧
相關(guān)文章
springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
這篇文章主要介紹了springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java 獲取Web項(xiàng)目相對(duì)webapp地址的實(shí)例
下面小編就為大家?guī)?lái)一篇Java 獲取Web項(xiàng)目相對(duì)webapp地址的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
spring?boot整合mongo查詢converter異常排查記錄
這篇文章主要為大家介紹了spring?boot整合mongo查詢時(shí)拋出converter異常的排查解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
Java的接口調(diào)用時(shí)的權(quán)限驗(yàn)證功能的實(shí)現(xiàn)
這篇文章主要介紹了Java的接口調(diào)用時(shí)的權(quán)限驗(yàn)證功能的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
java實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
SpringBoot參數(shù)校驗(yàn)與國(guó)際化使用教程
這篇文章主要給大家介紹了關(guān)于SpringBoot參數(shù)校驗(yàn)與國(guó)際化使用教程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
request.getRequestURL()等方法得到路徑的區(qū)別及說(shuō)明
這篇文章主要介紹了request.getRequestURL()等方法得到路徑的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.
這篇文章主要介紹了解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問(wèn)題,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

