Java 在LinkedList中插入节点
链表是一种线性数据结构,它由一系列称为节点的元素组成,每个节点包含一个数据元素和一个指向序列中下一个节点的引用(或指针)。链表的重要性在于它们的灵活性,节点可以在列表的任何位置插入或删除,而数组需要将元素移动以腾出空间。
链表也是动态的,这意味着它们可以根据需要增长或缩小,而不像固定大小的数组。最后,链表可以用来实现更复杂的数据结构,如栈、队列和图。
在这里,我们将探讨如何在链表中插入节点。
让我们开始吧!
展示一些示例
示例-1
- 链表 – 音乐播放列表 - 《天堂之路》、《波西米亚狂想曲》、《闻起来像是青春精神》
-
在该链表的末尾添加一个新节点。
-
新链表 – 《天堂之路》、《波西米亚狂想曲》、《闻起来像是青春精神》、《比利·让》
示例-2
-
链表 – 摩托车名称 - 哈雷戴维森Fat Boy、本田Gold Wing、川崎Ninja、雅马哈YZF-R1
-
在该链表的第2个位置添加一个新节点。
-
新链表 – 哈雷戴维森Fat Boy、杜卡迪Panigale V4、本田Gold Wing、川崎Ninja、雅马哈YZF-R1
示例-3
-
链表 – 汽车名称 - 丰田Camry、福特Mustang、本田Civic、特斯拉Model S
-
在该链表的开头添加一个新节点。
-
新链表 – 雪佛兰Corvette、丰田Camry、福特Mustang、本田Civic、特斯拉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编程语言,探讨了在链表中插入节点的不同方法。