链表
存储在不连续的内存空间上 更擅长插入和删除
此处我们在结尾以链表的插入接口为例 用图的形式详解该操作
public class TestLinkedlist {
public static void main(String[] args) {
testAddFirst();
testAddLast();
testAddIndex();
testContains();
testRemove();
}
public static void testAddFirst() {
System.out.println("测试头插");
LinkedList list = new LinkedList();
list.addFirst(1);
list.addFirst(2);
list.addFirst(3);
list.addFirst(4);
list.display();
}
public static void testAddLast() {
System.out.println("测试尾插");
LinkedList list = new LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
list.display();
}
public static void testAddIndex() {
System.out.println("测试插入任意位置");
LinkedList list = new LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
list.addIndex(2,5);
list.display();
}
public static void testContains() {
System.out.println("测试是否存在");
LinkedList list = new LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
list.contains(3);
list.display();
}
public static void testRemove() {
System.out.println("测试删除链表中的元素");
LinkedList list = new LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
list.remove(3);
list.display();
}
}
import com.sun.org.apache.bcel.internal.generic.LNEG;
import sun.awt.image.ImageWatched;
class LinkedNode {
public int data = 0;
public LinkedNode next = null;
public LinkedNode(int data) {
this.data = data;
}
}
public class LinkedList {
//头结点
private LinkedNode head = null;
public void display() {
//打印
System.out.print("[");
for (LinkedNode node = this.head;
node != null;
node = node.next) {
System.out.print(node.data);
if (node.next != null) {
System.out.print(",");
}
}
System.out.println("]");
}
//头插
public void addFirst(int elem) {
//先创建一个节点 让这个节点的值就是elem
LinkedNode node = new LinkedNode(elem);
if(this.head == null) {
//链表为空时
this.head = node;
return;
}
//如果不是空链表 就把新的节点放到链表的开始位置
node.next = head;
this.head = node;
return;
}
//尾插
public void addLast(int elem) {
LinkedNode node = new LinkedNode(elem);
if (this.head == null) {
//空链表
this.head = node;
return;
}
//非空 需要先找到最后一个节点
LinkedNode cur = this.head;
//循环结束 cur就是最后一个结点
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
//任意位置插入
public void addIndex(int index,int elem) {
LinkedNode node = new LinkedNode(elem);
//1.判断
int len = size();
if (index < 0 || index > len) {
return;
}
//2.头插
if (index == 0) {
addFirst(elem);
return;
}
//3.尾插
if (index == len) {
addLast(elem);
return;
}
//4.中间位置 需要找到 index - 1 这个位置
LinkedNode prev = getIndexPos(index - 1);
node.next = prev.next;
prev.next = node;
}
private LinkedNode getIndexPos(int index) {
LinkedNode cur = this.head;
for (int i = 0;i < index;i++) {
cur = cur.next;
}
return cur;
}
public int size() {
int size = 0;
for (LinkedNode cur = this.head;cur != null;cur = cur.next) {
size++;
}
return size;
}
//判断是否存在
public boolean contains(int toFind) {
for (LinkedNode cur = this.head;cur != null;cur = cur.next) {
if (cur.data == toFind) {
return true;
}
}
return false;
}
//删除第一次出现的元素
public void remove(int toRemove) {
//1.考虑空链表的情况
if (head == null) {
return;
}
//2.先判断是否是头结点
if (head.data == toRemove) {
this.head = this.head.next;
return;
}
//3.删除中间结点 找到前一个结点
LinkedNode prev = searchPrev(toRemove);
prev.next = prev.next.next;
}
private LinkedNode searchPrev(int toRemove) {
if (this.head == null) {
return null;
}
LinkedNode prev = this.head;
while (prev.next != null) {
if (prev.next.data == toRemove) {
return prev;
}
prev = prev.next;
}
return null;
}
}
链表插入(int index , int elem):
我们最基础的思想就是对输入进行判断,即判断要插入位置的情况
1.判定是否有效
即 index < 0 || index >len
2.头插
3.尾插
4.中间位置