溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java8實(shí)現(xiàn)一個(gè)任意參數(shù)的鏈棧

發(fā)布時(shí)間:2020-10-28 15:15:08 來源:億速云 閱讀:168 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章運(yùn)用簡單易懂的例子給大家介紹Java8實(shí)現(xiàn)一個(gè)任意參數(shù)的鏈棧,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

1、實(shí)現(xiàn)功能

1)push():入棧;
2)pop():出棧;
3)getSize():獲取棧大?。?br/>4)display():展示棧。

以一下測試進(jìn)行特別說明:

 /**
   * 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敗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

關(guān)于Java8實(shí)現(xiàn)一個(gè)任意參數(shù)的鏈棧就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI