這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)java實(shí)現(xiàn)棧的數(shù)組和鏈表的方法,以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到北湖網(wǎng)站設(shè)計(jì)與北湖網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋北湖地區(qū)。
棧的介紹
棧,是一種先進(jìn)后出(FILO)的線性數(shù)據(jù)結(jié)構(gòu),主要操作為入棧和出棧。
棧底:最早進(jìn)入的元素存放的位置。
棧頂:最后進(jìn)入元素存放的位置(有些棧中將棧頂表示為棧頂元素的下一位置)。
棧的數(shù)組的實(shí)現(xiàn)
示例如下:
public class MyStack {
private int[] array;
private int top = -1;//用top來(lái)表示棧頂,指向棧頂元素
public MyStack(int capacity){
array = new int[capacity];
}
public void push(int data) throws Exception{
if(top >= array.length-1)
throw new IndexOutOfBoundsException("邊界超過(guò)范圍!");
else
array[++top] = data;//先將指針上移,后賦值
}
public int pop() throws Exception{
int temp;
if(top < 0)
throw new IndexOutOfBoundsException("棧為空,不能再刪除元素!");
else{
temp = array[top];
array[top--] = 0;
}
return temp;
}
public void output(){
for(int i = 0; i <= top; i++){
System.out.println(array[i]);
}
}
public static void main(String[] args) throws Exception{
MyStack myStack = new MyStack(5);
myStack.push(1);
myStack.push(3);
myStack.push(2);
myStack.pop();
myStack.push(4);
myStack.pop();
myStack.output();
}
}棧的鏈表實(shí)現(xiàn)
棧用鏈表來(lái)實(shí)現(xiàn)時(shí),和數(shù)組實(shí)現(xiàn)不同的是,在出棧時(shí),因?yàn)槲覀冎挥幸粋€(gè)top節(jié)點(diǎn)來(lái)指向棧頂,因此需要從頭到尾遍歷一遍,來(lái)找到棧頂?shù)那耙粋€(gè)位置。
具體的實(shí)現(xiàn)如下:
public class MyStack_LinkList {
private static class Node{
int data;
Node next;
Node(int data){
this.data = data;
}
}
private Node head;//定義鏈表的頭結(jié)點(diǎn)
private Node top;
private int size;//定義棧中的元素個(gè)數(shù)
private int maxSize;
private MyStack_LinkList(int capacity){
maxSize = capacity;
}
public void push(int data) throws Exception{
if(size >= maxSize){
throw new IndexOutOfBoundsException("棧已滿(mǎn),不能再入棧!");
}
Node pushedNode = new Node(data);
if(size == 0){
head = pushedNode;
top = pushedNode;
pushedNode.next = null;
}
else{
top.next = pushedNode;
pushedNode.next = null;
top = pushedNode;
}
size++;
}
public int pop() throws Exception{
int temp = 0;
if(size <= 0)
throw new IndexOutOfBoundsException("棧為空,不能再出棧!");
else if(size == 1){//當(dāng)棧中元素個(gè)數(shù)為1時(shí),單獨(dú)討論,需將頭節(jié)點(diǎn)置為空
temp = top.data;
top = null;
}
else{
temp = top.data;
top = get(size - 1);//此時(shí)需遍歷一遍鏈表,用top指向需出棧的上一個(gè)節(jié)點(diǎn)
top.next = null;
}
size--;
return temp;
}
/*
從頭到尾查找元素
*/
public Node get(int index){
Node temp = head;
for(int i = 1; i < index; i++){
temp = temp.next;
}
return temp;
}
public void output(){
Node temp = head;
for(int i = 0; i < size; i++){
System.out.println(temp.data);
temp = temp.next;
}
}
public static void main(String[] args) throws Exception{
MyStack_LinkList myStack_linkList = new MyStack_LinkList(5);
myStack_linkList.push(1);
myStack_linkList.push(2);
myStack_linkList.pop();
myStack_linkList.push(3);
myStack_linkList.push(5);
myStack_linkList.output();
}
}棧的應(yīng)用場(chǎng)景
(1)括號(hào)匹配判斷,用于判斷()、{}等是否匹配;
(2)進(jìn)制轉(zhuǎn)換時(shí),逆向輸出轉(zhuǎn)換后的數(shù);
(3)實(shí)現(xiàn)遞歸的邏輯可以用棧來(lái)實(shí)現(xiàn);
(4)棧還可以用于面包屑導(dǎo)航,使用戶(hù)在瀏覽頁(yè)面時(shí)可以輕松地回溯到上一級(jí)或更上一級(jí)頁(yè)面。
上述就是小編為大家分享的java實(shí)現(xiàn)棧的數(shù)組和鏈表的方法了,如果您也有類(lèi)似的疑惑,不妨參照上述方法進(jìn)行嘗試。如果想了解更多相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊。
文章標(biāo)題:java實(shí)現(xiàn)棧的數(shù)組和鏈表的方法
網(wǎng)頁(yè)路徑:http://chinadenli.net/article38/jighsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、建站公司、網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站收錄、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)