欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 實現(xiàn)鏈棧存儲的方法

 更新時間:2017年08月14日 07:45:15   投稿:jingxian  
下面小編就為大家?guī)硪黄猨ava 實現(xiàn)鏈棧存儲的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

如下所示:

package com.learn.algorithm.linkStack;
/**
 * 鏈棧實現(xiàn)
 * @author Jiekun.Cui
 * @param <T>
 */
public class LinkStack<T> {

  private LinkStack<T>.Node<T> top = new Node<T>();
  private int size=0;
  
  /**
   * 進(jìn)棧
   * @param t
   * @return ;
   */
  public boolean push(T t){
    
    if ( isEmpty() ) {
      top.next = new Node<T>(t);
      
    } else {
      Node<T> newNode = new Node<T>(t, top.next);
      top.next = newNode;
    }
    size ++ ;
    return true;
  }
  
  /**
   * 出棧
   * @param t
   * @return
   */
  public T pop(){
    
    if ( isEmpty() ) {
      return null;
    } else {
      LinkStack<T>.Node<T> 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 鏈棧的節(jié)點類
   * @param <T>
   */
  class Node<T>{
    private T t = null;
    private Node<T> next = null;
    
    public Node(){
      
    }
    public Node(T t){
      this.t = t;
    }
    public Node(T t,Node<T> next){
      this.t = t;
      this.next =next;
    }
    
    
    public T getT() {
      return t;
    }
    public void setT(T t) {
      this.t = t;
    }
    
    public Node<T> getNext() {
      return next;
    }
    public void setNext(Node<T> next) {
      this.next = next;
    }
  }
}
package com.learn.algorithm.linkStack;

/**
 * 鏈棧測試
 * @author Jiekun.Cui
 */
public class Demo {
  
  public static void main(String[] args) {
    LinkStack<Integer> ls = 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 實現(xiàn)鏈棧存儲的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評論