Java 連結串列插入節點程式


連結串列是一種線性資料結構,由一系列稱為節點的元素組成,每個節點包含一個數據元素和一個指向序列中下一個節點的引用(或指標)。連結串列的重要性在於其靈活性,因為可以在列表中的任何位置插入或刪除節點,而陣列則需要移動元素以騰出空間。

連結串列也是動態的,這意味著它們可以根據需要增長或縮小,這與大小固定的陣列不同。最後,連結串列可以用來實現更復雜的資料結構,例如棧、佇列和圖。

這裡,我們將探討如何在連結串列中插入一個節點。

讓我們開始吧!

舉幾個例子

例項 1

  • 連結串列 - 音樂播放列表 - 天梯,波西米亞狂想曲,聞起來像青少年精神

  • 在這個連結串列的末尾新增一個新節點。

  • 新連結串列 - 天梯,波西米亞狂想曲,聞起來像青少年精神,比莉·簡

例項 2

  • 連結串列 - 腳踏車名稱 - 哈雷戴維森肥仔,本田金翼,川崎忍者,雅馬哈 YZF-R1

  • 在這個連結串列的第 2 個位置新增一個新節點。

  • 新連結串列 - 哈雷戴維森肥仔,杜卡迪 Panigale V4,本田金翼,川崎忍者,雅馬哈 YZF-R1

例項 3

  • 連結串列 - 汽車名稱 - 豐田凱美瑞,福特野馬,本田思域,特斯拉 Model S

  • 在這個連結串列的開頭新增一個新節點。

  • 新連結串列 - 雪佛蘭克爾維特,豐田凱美瑞,福特野馬,本田思域,特斯拉 Model S

演算法

演算法 1

  • 步驟 1 - 定義一個 Node 類和一個方法,用於在連結串列的開頭插入一個新節點。

  • 步驟 2 - 提示使用者輸入連結串列中的元素數量以及元素本身。

  • 步驟 3 - 迴圈遍歷每個輸入的元素,使用 insertAtBeginning() 方法將其插入到連結串列的開頭。

  • 步驟 4 - 然後透過遍歷連結串列並列印每個元素來列印連結串列。

  • 步驟 5 - 程式關閉掃描器物件並終止。

演算法 2

  • 步驟 1 - 定義一個 Node 類和一個方法,用於在連結串列的給定位置插入一個新節點。

  • 步驟 2 - 提示使用者輸入連結串列中的元素數量以及元素本身。

  • 步驟 3 - 迴圈遍歷每個輸入的元素,使用 insertAtPosition() 方法將其插入到當前位置。

  • 步驟 4 - 提示使用者輸入要插入新節點的元素和位置,然後使用這些引數呼叫 insertAtPosition()。

  • 步驟 5 - 透過遍歷連結串列並列印每個元素來列印更新後的連結串列。然後關閉掃描器物件,程式終止。

語法

在連結串列中,.next 是對列表中下一個節點的引用。

下面是其語法 -

new_node.next = head;

其中,“new_node” 指的是節點類的物件。

多種方法

我們提供了不同方法的解決方案。

  • 在連結串列的開頭插入節點

  • 在連結串列的給定位置插入節點

讓我們逐一檢視程式及其輸出。

方法 1:在連結串列的開頭插入節點

在這種方法中,使用者必須插入連結串列的元素數量,然後是元素本身。然後使用我們的演算法將節點元素插入到連結串列中。

示例

import java.util.*;
public class Main {
   static class Node {
      int data;
      Node next;

      Node(int d) {
         data = d;
         next = null;
      }
   }
   static Node insertAtBeginning(Node head, int data) {
      Node new_node = new Node(data);
      new_node.next = head;
      head = new_node;
      return head;
   }

   static void printList(Node head) {
      Node temp = head;
      while (temp != null) {
         System.out.print(temp.data + " ");
         temp = temp.next;
      }
   }

   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of elements in the linked list: ");
      int n = sc.nextInt();
      Node head = null;
      for (int i = 0; i < n; i++) {
         System.out.println("Enter the element " + (i + 1) + " : ");
         int data = sc.nextInt();
         head = insertAtBeginning(head, data);
      }
      System.out.println("The linked list is: ");
      printList(head);
      sc.close();
   }
}

輸出

Enter the number of elements in the linked list: 
5
Enter the element 1 :
1
Enter the element 2 :
3   
Enter the element 3 :
5
Enter the element 4 :
4
Enter the element 5 :
6
The linked list is:
6 4 5 3 1

方法 2:在連結串列的給定位置插入節點

在這種方法中,使用者必須插入連結串列的元素數量,然後是元素本身以及要新增新元素的位置。然後使用我們的演算法在連結串列的給定位置插入新節點。

示例

import java.util.*;
public class Main {
   static class Node {
      int data;
      Node next;
      Node(int d) {
         data = d;
         next = null;
      }
   }
   static Node insertAtPosition(Node head, int data, int position) {
      Node new_node = new Node(data);
      if (position == 1) {
         new_node.next = head;
         head = new_node;
         return head;
      }
      Node prev = head;
      for (int i = 1; i < position - 1; i++) {
         prev = prev.next;
      }

      new_node.next = prev.next;
      prev.next = new_node;
      return head;
   }
   static void printList(Node head) {
      Node temp = head;
      while (temp != null) {
         System.out.print(temp.data + " ");
         temp = temp.next;
      }
   }
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of elements in the linked list: ");
      int n = sc.nextInt();
      Node head = null;
      for (int i = 0; i < n; i++) {
         System.out.println("Enter the element " + (i + 1) + " : ");
         int data = sc.nextInt();
         head = insertAtPosition(head, data, i + 1);
      }
      System.out.println("The linked list is: ");
      printList(head);
      System.out.println("\nEnter the element to insert: ");
      int data = sc.nextInt();
      System.out.println("Enter the position to insert: ");
      int position = sc.nextInt();
      head = insertAtPosition(head, data, position);
      System.out.println("The linked list after insertion is: ");
      printList(head);
      sc.close();
   }
}

輸出

Enter the number of elements in the linked list: 
4
Enter the element 1 :
11
Enter the element 2 :
22
Enter the element 3 :
33
Enter the element 4 :
44
The linked list is:
11 22 33 44
Enter the element to insert:
55
Enter the position to insert:
2
The linked list after insertion is:
11 55 22 33 44

在本文中,我們探討了使用 Java 程式語言在連結串列中插入節點的不同方法。

更新於:2023 年 5 月 4 日

3K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告