📅  最后修改于: 2023-12-03 14:43:00.169000             🧑  作者: Mango
本示例是关于如何使用Java实现单链表的一个例子。单链表是由一系列节点组成的线性结构。每个节点都包含一个数据域和一个指针域,指向下一个节点。
在本示例中,我们将以Java语言编写代码,使用面向对象的思想来实现单链表的基本操作,包括插入、删除、查找等。
代码实现过程中,我们定义了一个Node类来表示链表中的一个节点,该节点包含两个属性:
/**
* 定义链表中的节点类
*/
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
然后我们定义一个链表类来表示整个链表,该链表类包含以下几个方法:
/**
* 定义链表类
*/
class LinkedList {
Node head;
// 在链表头部插入一个节点
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
System.out.println("Inserted " + data + " at beginning of list");
}
// 在链表尾部插入一个节点
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
System.out.println("Inserted " + data + " at end of list");
}
// 在链表的指定位置插入一个节点
public void insertAtPos(int data, int position) {
if (position < 1) {
System.out.println("Invalid position!");
return;
}
Node newNode = new Node(data);
if (position == 1) {
newNode.next = head;
head = newNode;
System.out.println("Inserted " + data + " at position " + position + " of list");
return;
}
Node current = head;
for (int i = 1; i < position - 1; i++) {
if (current == null) {
System.out.println("Position out of range!");
return;
}
current = current.next;
}
if (current == null) {
System.out.println("Position out of range!");
return;
}
newNode.next = current.next;
current.next = newNode;
System.out.println("Inserted " + data + " at position " + position + " of list");
}
// 删除链表的头部节点
public void deleteAtBeginning() {
if (head == null) {
System.out.println("List is empty!");
return;
}
head = head.next;
System.out.println("Deleted node from beginning of list");
}
// 删除链表的尾部节点
public void deleteAtEnd() {
if (head == null) {
System.out.println("List is empty!");
return;
}
if (head.next == null) {
head = null;
System.out.println("Deleted node from end of list");
return;
}
Node current = head;
while (current.next.next != null) {
current = current.next;
}
current.next = null;
System.out.println("Deleted node from end of list");
}
// 删除链表的指定位置节点
public void deleteAtPos(int position) {
if (head == null) {
System.out.println("List is empty!");
return;
}
if (position == 1) {
head = head.next;
System.out.println("Deleted node from position " + position + " of list");
return;
}
Node current = head;
for (int i = 1; i < position - 1; i++) {
if (current == null) {
System.out.println("Position out of range!");
return;
}
current = current.next;
}
if (current == null || current.next == null) {
System.out.println("Position out of range!");
return;
}
current.next = current.next.next;
System.out.println("Deleted node from position " + position + " of list");
}
// 在链表中查找指定的数据
public void search(int data) {
if (head == null) {
System.out.println("List is empty!");
return;
}
Node current = head;
int position = 1;
while (current != null) {
if (current.data == data) {
System.out.println(data + " found at position " + position);
return;
}
position++;
current = current.next;
}
System.out.println(data + " not found in list");
}
// 遍历并输出整个链表
public void display() {
if (head == null) {
System.out.println("List is empty!");
return;
}
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
使用本示例中的单链表类的方法很简单,只需要先创建一个链表对象,然后调用对象的相应方法即可。
下面是一个示例,演示如何使用单链表类的方法实现插入、删除、查找等操作:
public static void main(String[] args) {
LinkedList list = new LinkedList();
// 插入节点
list.insertAtBeginning(5);
list.insertAtEnd(10);
list.insertAtPos(7, 2);
list.display(); // 输出: 5 7 10
// 删除节点
list.deleteAtBeginning();
list.deleteAtEnd();
list.deleteAtPos(1);
list.display(); // 输出: 7
// 查找节点
list.search(7); // 输出: 7 found at position 1
list.search(10); // 输出: 10 not found in list
}
本示例演示了一个简单的Java单链表实现,这是Java面试中一个常被问到的问题。当然,实际使用中,可能还需要实现更多的操作,比如排序、反转链表等。建议大家多做练习,加深对单链表的理解和掌握。