重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
本篇文章給大家分享的是有關怎么在java項目中實現一個鏈棧存儲,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
納雍網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、成都響應式網站建設等網站項目制作,到程序開發,運營維護。創新互聯成立于2013年到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
如下所示:
package com.learn.algorithm.linkStack; /** * 鏈棧實現 * @author Jiekun.Cui * @param*/ public class LinkStack { private LinkStack .Node top = new Node (); private int size=0; /** * 進棧 * @param t * @return ; */ public boolean push(T t){ if ( isEmpty() ) { top.next = new Node (t); } else { Node newNode = new Node (t, top.next); top.next = newNode; } size ++ ; return true; } /** * 出棧 * @param t * @return */ public T pop(){ if ( isEmpty() ) { return null; } else { LinkStack .Node node = top.next; top.next = node.next; size --; return node.getT(); } } /** * 獲取棧頂元素 * @return */ public T getTop(){ if ( isEmpty() ) { return null; } else { return top.next.getT(); } } /** * 判斷棧是不是為空 * @return */ public boolean isEmpty(){ return size() == 0; } /** * 返回棧的大小 * @return */ public int size(){ return size; } /** * @author 鏈棧的節點類 * @param */ class Node { private T t = null; private Node next = null; public Node(){ } public Node(T t){ this.t = t; } public Node(T t,Node next){ this.t = t; this.next =next; } public T getT() { return t; } public void setT(T t) { this.t = t; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } }
package com.learn.algorithm.linkStack; /** * 鏈棧測試 * @author Jiekun.Cui */ public class Demo { public static void main(String[] args) { LinkStackls = new LinkStack<>(); ls.push(1); ls.push(2); ls.pop(); ls.push(4); ls.push(5); ls.push(6); while ( !ls.isEmpty() ) { System.out.println(ls.pop()); } } }
以上就是怎么在java項目中實現一個鏈棧存儲,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯行業資訊頻道。