Java8實(shí)現(xiàn)任意參數(shù)的鏈棧
本文實(shí)例為大家分享了Java8實(shí)現(xiàn)任意參數(shù)的鏈棧,供大家參考,具體內(nèi)容如下
1、實(shí)現(xiàn)功能
1)push():入棧;
2)pop():出棧;
3)getSize():獲取棧大??;
4)display():展示棧。
以一下測(cè)試進(jìn)行特別說(shuō)明:
/** * The main function. */ public static void main(String[] args) { MyLinkedStack <Character> test = new MyLinkedStack<>(); test.push('2'); test.push('+'); test.push('-'); test.pop(); test.push('('); test.display(); }
輸出如下,即輸出順序?yàn)闂m?、棧頂下一個(gè)…
The linked stack is:
[(, +, 2]
2、代碼
package DataStructure; /** * @author: Inki * @email: inki.yinji@qq.com * @create: 2020 1026 * @last_modify: 2020 1026 */ public class MyLinkedStack <AnyType> { /** * Only used to store the head node. */ private SingleNode<AnyType> head = new SingleNode(new Object()); /** * The single linked list current size. */ private int size = 0; /** * Push element to the end of the list. * @param: * paraVal: The given value. */ public void push(AnyType paraVal) { SingleNode <AnyType> tempNode = new SingleNode<>(paraVal); tempNode.next = head.next; head.next = tempNode; size++; }//Of push /** * Pop the last element. * @return: * The popped value. */ public AnyType pop(){ if (size == 0) { throw new RuntimeException("The stack is empty."); } AnyType retVal = head.next.val; head.next = head.next.next; size--; return retVal; }//Of pop /** * Get the current size of the single linked list. * @return: * The current size of the single linked list. */ public int getSize() { return size; }//Of getSize /** * Display the single linked list. */ public void display() { if (size == 0) { throw new RuntimeException("The stack is empty."); }//Of if System.out.print("The linked stack is:\n["); SingleNode <AnyType> tempNode = head; int i = 0; while (i++ < size - 1) { tempNode = tempNode.next; System.out.printf("%s, ", tempNode.val); }//Of while System.out.printf("%s]\n", tempNode.next.val); }//Of display /** * The main function. */ public static void main(String[] args) { MyLinkedStack <Character> test = new MyLinkedStack<>(); test.push('2'); test.push('+'); test.push('-'); test.pop(); test.push('('); test.display(); } }//Of class MyLinkedStack class SingleNode <AnyType>{ /** * The value. */ AnyType val; /** * The next node. */ SingleNode<AnyType> next; /** * The first constructor. * @param * paraVal: The given value. */ SingleNode (AnyType paraVal) { val = paraVal; }//The first constructor }//Of class SingleNode
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java封裝的實(shí)現(xiàn)訪問(wèn)限定符、包
封裝就是將數(shù)據(jù)和操作數(shù)據(jù)的方法進(jìn)行有機(jī)結(jié)合,隱藏對(duì)象的屬性(成員變量)和實(shí)現(xiàn)細(xì)節(jié),僅對(duì)外公開接口來(lái)和對(duì)象進(jìn)行交互,下面這篇文章主要給大家介紹了關(guān)于Java封裝實(shí)現(xiàn)訪問(wèn)限定符、包的相關(guān)資料2022-08-08Java實(shí)現(xiàn)XML與JSON秒級(jí)轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了Java實(shí)現(xiàn)XML與JSON秒級(jí)轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09java根據(jù)不同的參數(shù)調(diào)用不同的實(shí)現(xiàn)類操作
這篇文章主要介紹了java根據(jù)不同的參數(shù)調(diào)用不同的實(shí)現(xiàn)類操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09mybatis-plus報(bào)錯(cuò)net.sf.jsqlparser.statement.select.SelectBody的
本文主要介紹了mybatis-plus報(bào)錯(cuò)net.sf.jsqlparser.statement.select.SelectBody的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08淺談SpringBoot Bean加載優(yōu)先級(jí)的問(wèn)題
這篇文章主要介紹了淺談SpringBoot Bean加載優(yōu)先級(jí)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java實(shí)現(xiàn)的質(zhì)因數(shù)分解操作示例【基于遞歸算法】
這篇文章主要介紹了Java實(shí)現(xiàn)的質(zhì)因數(shù)分解操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Java基于遞歸算法實(shí)現(xiàn)針對(duì)整數(shù)的質(zhì)因數(shù)分解相關(guān)操作技巧,需要的朋友可以參考下2018-03-03Presto支持Elasticsearch數(shù)據(jù)源配置詳解
這篇文章主要為大家介紹了Presto支持Elasticsearch數(shù)據(jù)源配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12