一、单链表的头插法
1、创建一个NODE节点的类,写一个public void addFirst()的函数来实现链表的头部插入,最后写一个public void display()的函数实现链表的打印输出
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
*
* @User:Mingaho
* @Date:2021/04/10
* @Time:16:50
*/
class NODE { //构造一个节点的类
public int data;
public NODE next;
public NODE(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedList {
public NODE head; //保存单链表的头节点的引用 代表的是整个链表的头 所以定义在这个地方
//头插法
public void addFirst(int data) {
NODE node = new NODE(data);
if(this.head == null) {
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
//display 打印该链表
public void display() {
if(this.head == null) {
System.out.println("The List is empty.");
return;
}
NODE cur = this.head;
while(cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.addFirst(12); //进行头插
linkedList.addFirst(13);
linkedList.addFirst(14);
linkedList.addFirst(15);
linkedList.addFirst(16);
linkedList.display();
}
}
2、以下是头插法代码运行结果
二、单链表的尾插法
1、创建一个NODE节点的类,写一个public void addLast()的函数来实现链表的尾部插入,最后写一个public void display()的函数实现链表的打印输出
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
*
* @User:Mingaho
* @Date:2021/04/10
* @Time:16:50
*/
class NODE { //构造一个节点的类
public int data;
public NODE next;
public NODE(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedList {
public NODE head; //保存单链表的头节点的引用 代表的是整个链表的头 所以定义在这个地方
//尾插法
public void addLast(int data) {
NODE node = new NODE(data);
if(this.head == null) {
this.head = node;
return;
}
NODE cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
//display 打印该链表
public void display() {
if(this.head == null) {
System.out.println("The List is empty.");
return;
}
NODE cur = this.head;
while(cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.addLast(11); //进行尾插
linkedList.addLast(10);
linkedList.addLast(9);
linkedList.addLast(8);
linkedList.display();
}
}
2、以下是尾插法的代码运行结果
三、单链表任意位置的插入
1、创建一个NODE节点的类,利用public NODE searchIndex()函数先找到插入位置的前驱节点,再利用public void addIndex()的函数来实现链表的任意插入,最后写一个public void display()的函数实现链表的打印输出
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
*
* @User:Mingaho
* @Date:2021/04/10
* @Time:16:50
*/
class NODE { //构造一个节点的类
public int data;
public NODE next;
public NODE(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedList {
public NODE head; //保存单链表的头节点的引用 代表的是整个链表的头 所以定义在这个地方
//查找index的前驱位置
public NODE searchIndex(int index) {
if(index < 0 || index > this.size()) {
throw new RuntimeException("index's location is not right.");
}
NODE prev = this.head;
//NODE cur = this.head.next;
while(index > 1) {
prev = prev.next;
index--;
}
return prev;
}
//任意位置插入data
public void addIndex(int index, int data) {
NODE node = new NODE(data);
if(this.head == null) {
this.head = node;
return;
}
NODE prev = searchIndex(index);
//NODE cur = prev.next;
node.next = prev.next;
prev.next = node;
}
//display 打印该链表
public void display() {
if(this.head == null) {
System.out.println("The List is empty.");
return;
}
NODE cur = this.head;
while(cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.addIndex(0,90); //index位置插入data数据
linkedList.addIndex(1,91);
linkedList.addIndex(2,92);
linkedList.addIndex(3,93);
linkedList.addIndex(4,94);
linkedList.addIndex(5,95);
linkedList.addIndex(6,96);
linkedList.addIndex(6,100);
linkedList.addIndex(6,123);
linkedList.addIndex(6,234);
linkedList.display();
}
}
2、以下是任意位置插入的代码运行结果