溫馨提示×

溫馨提示×

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

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

使用Java 8實現任意參數的單鏈表

發布時間:2020-10-28 15:24:41 來源:億速云 閱讀:175 作者:Leah 欄目:開發技術

這期內容當中小編將會給大家帶來有關使用Java 8實現任意參數的單鏈表,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1、實現功能

1)add():鏈表末尾添加元素;
2)pop():移除鏈表尾部元素;
3)insert():指定索引處添加元素;
4)delete():指定索引處刪除元素;
5)getSize():獲取鏈表當前長度;
6)display():展示鏈表當前元素。

2、代碼

package DataStructure;

/**
 * @author: Inki
 * @email: inki.yinji@qq.com
 * @create: 2020 1024
 * @last_modify: 2020 1025
 */

public class MySingleLinkedList <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;

 /**
  * Add element to the end of the list.
  * @param:
  *  paraVal: The given value.
  */
 public void add(AnyType paraVal) {
  insert(size, paraVal);
 }//Of add

 /**
  * Pop the last element.
  * @return:
  *  The popped value.
  */
 public AnyType pop(){
  return delete(size - 1);
 }//Of pop

 /**
  * Insert element at specified index.
  * @param:
  *  paraIdx: The given index.
  *  paraVal: The given value.
  */
 public void insert(int paraIdx, AnyType paraVal) {

  if (paraIdx > size) {
   throw new IndexOutOfBoundsException("The index error.");
  }//Of if

  SingleNode <AnyType> tempNode = head;
  int i = 0;
  while (i++ < paraIdx) {
   tempNode = tempNode.next;
  }//Of while

  SingleNode<AnyType> paraNode = new SingleNode <>(paraVal);
  paraNode.next = tempNode.next;
  tempNode.next = paraNode;
  size++;
 }//of add

 /**
  * Delete the element at specified index.
  * @param:
  *  paraIdx: The given index of element to delete.
  * @return:
  *  The deleted value.
  */
 public AnyType delete(int paraIdx) {
  if (size == 0) {
   throw new RuntimeException("The single linked list is empty.");
  }//Of if

  if (size <= paraIdx) {
   throw new IndexOutOfBoundsException("The index error.");
  }//Of if

  SingleNode <AnyType> retNode = head;
  int i = 0;
  while (i++ < paraIdx) {
   retNode = retNode.next;
  }//Of while

  retNode.next = retNode.next.next;

  size--;
  return retNode.val;
 }//Of delete

 /**
  * 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 single linked list is empty.");
  }//Of if
  
  System.out.print("The single linked list 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) {
  MySingleLinkedList <Character> test = new MySingleLinkedList<>();
  test.add('a');
  test.add('b');
  test.insert(0, 'c');
  test.add('d');
  test.insert(0, '5');
  test.delete(4);
  test.pop();
  test.add('+');
  test.display();
  System.out.println(test.getSize());
 }//Of main

}//Of class MySingleLinkedList

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

上述就是小編為大家分享的使用Java 8實現任意參數的單鏈表了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女