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 编程语言在链表中插入节点的不同方法。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP