最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8實現(xiàn)任意參數(shù)的鏈棧

 更新時間:2020年10月27日 17:12:50   作者:因吉  
這篇文章主要為大家詳細介紹了Java8實現(xiàn)任意參數(shù)的鏈棧,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java8實現(xiàn)任意參數(shù)的鏈棧,供大家參考,具體內容如下

1、實現(xiàn)功能

1)push():入棧;
2)pop():出棧;
3)getSize():獲取棧大小;
4)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();
}

輸出如下,即輸出順序為棧頂、棧頂下一個…

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關文章

最新評論

邵东县| 松江区| 江达县| 板桥市| 上栗县| 凤山县| 梅河口市| 安徽省| 延吉市| 句容市| 富平县| 剑河县| 浪卡子县| 济宁市| 古浪县| 邹平县| 镇江市| 株洲县| 楚雄市| 洪雅县| 米林县| 巴林右旗| 丰都县| 浙江省| 扎赉特旗| 武强县| 弥渡县| 内江市| 武平县| 明光市| 大港区| 博乐市| 梁平县| 新化县| 奉贤区| 碌曲县| 延安市| 延吉市| 临潭县| 商丘市| 永仁县|