之前学习了顺序表,接下来把链表的功能给模拟实现一遍
链表
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。

链表的结构有很多种,但是我们重点掌握两种:
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

整体结构就长这个样子
无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

链表的实现

第一个节点也称为头结点 head
依靠head 节点就可以找到所有的节点
单链表的模拟实现

creatList为我们已经创建好了一个链表,在它的基础上我们可以进行操作


实现接口的功能

一共实现的功能就这么多 现在我们先来一一实现
一.打印链表

注:一般情况不动head那个节点,创建一个cur节点来代替head节点,让它永远指向头结点
java
public void display() {
if(head == null) {
return ;
}
ListNode cur = head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
}

二. 查找是否包含关键字key是否在单链表当中
java
public boolean contains(int key) {
ListNode cur = head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
三.得到单链表的长度
java
public int size() {
int count = 0;
ListNode cur = head;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}

这几个都是遍历整个链表,所有会比较轻松
四.头插法
思路:
考虑head等于null的情况下,直接让node = head 就行 这题考虑与不考虑都无所谓
java
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(head == null) {
head = node;
} else {
node.next = head;
head = node;
}
}

五.尾插法

也是一样的 如果head等于null的话 直接让head = node
java
public void addLast(int data) {
ListNode node = new ListNode(data);
ListNode cur = head;
if(head == null) {
head = node;
} else {
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}

六.任意位置插入,第一个数据节点为0号下标

index = 0 或者等于size()大小时候,就可以用到头插和尾差,再者看看index值合法不合法
java
public void addIndex(int index, int data) {
ListNode node = new ListNode(data);
if(index < 0 || index >= size()) {
throw new InderException("index不合法");
}
if(index == 0) {
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
else {
ListNode cur = head;
ListNode curPre = cur;
while( index != 0) {
curPre = cur;
cur = cur.next;
index--;
}
curPre.next = node;
node.next = cur;
}
}

七. 删除第一次出现关键字为key的节点

万一第一个节点就是我们要删除的,直接将head 节点移动到下一个节点
java
public void remove(int key) {
if(head == null) {
return;
}
if(head.val == key) {
head = head.next;
return;
}
ListNode node = new ListNode(key);
ListNode cur = head.next;
ListNode curPre = head;
while(cur != null) {
if(cur.val == key) {
curPre.next = cur.next;
return;
}
curPre = curPre.next;
cur = cur.next;
}
}

八.删除所有值为key的节点

头结点也是最后一个判断,只有这种,才可以避免头结点是要删除的节点的问题
java
public void removeAllKey(int key) {
if(head == null) {
return ;
}
ListNode curPre = head;
ListNode cur = head.next;
while(cur != null) {
if(cur.val == key) {
curPre.next = cur.next;
cur = cur.next;
}
else {
cur = cur.next;
curPre = curPre.next;
}
}
if(head.val == key) {
head = head.next;
}
}

八.清空链表
java
public void clear() {
head = null;
}
直接让head 为空就行
ok以上就是整个单链表的模拟过程,这里只是简单入个门而已
单链表一般在笔试面试题常常出现,所有我们要经常刷题才能更好的掌握它
这里为大家整理好了一些题目
以前写的 之前因为个人原因断了 现在慢慢更新 感谢!!!