1、链表的定义
链表是通过指针将一组零散的内存块串联在一起。其中我们把内存块称为链表的“结点”。为了将所有的结点串起来,每个链表的结点除了存储数据之外,还需要记录链上的下一个结点的地址。数据如图所示:
2、特点
- 不需要连续的内存空间
- 有指针引用
- 三种最常见的链表结构:单链表、双向链表和循环链表
2.1 单链表
从单链表图中,可以发现,有两个结点是比较特殊的,它们分别是第一个结点和最后一个结点。我们一般把第一个结点叫作头结点,把最后一个结点叫作尾结点。
其中,头结点用来记录链表的基地址。有了它,我们就可以遍历得到整条链表。而尾结点特殊的地方是:指针不是指向下一个结点,而是指向一个空地址NULL,表示这是链表上最后一个结点。
2.2 循环链表
循环链表是一种特殊的单链表。实际上,循环链表也很简单。它跟单链表唯一的区别就在尾结点。我们知道,单链表的尾结点指针指向空地址,表示这就是最后的结点了。而循环链表的尾结点指针是指向链表的头结点。从画的循环链表图中,你应该可以看出来,它像一个环一样首尾相连,所以叫作“循环”链表。
2.3 双向链表
单向链表只有一个方向,结点只有一个后继指针next指向后面的结点。双向链表,顾名思义,它支持两个方向,每个结点不止有一个后继指针next指向后面的结点,还有一个前驱指针prev指向前面的结点。双向链表需要额外的两个空间来存储后继结点和前驱结点的地址。所以,如果存储同样多的数据,双向链表要比单链表占用更多的内存空间。虽然两个指针比较浪费存储空间,但可以支持双向遍历,这样也带来了双向链表操作的灵活性。
3 数组和链表比较
时间复杂度 | 数组 | 链表 |
---|---|---|
插入删除 | O(n) | O(1) |
随机访问 | O(1) | O(n) |
重要区别:
- 数组简单易用,在实现上使用的是连续的内存空间,可以借助CPU的缓存机制,预读数组中的数据,所以访问效率更高。
- 链表在内存中并不是连续存储,所以对CPU缓存不友好,没办法有效预读。
- 数组的缺点是大小固定,一经声明就要占用整块连续内存空间。如果声明的数组过大,系统可能没有足够的连续内存空间分配给它,导致“内存不足(out ofmemory)”。如果声明的数组过小,则可能出现不够用的情况。
- 动态扩容:数组需再申请一个更大的内存空间,把原数组拷贝进去,非常费时。链表本身没有大小的限制,天然地支持动态扩容。
4、代码实现
4.1 链表接口
/**
* 链表接口
*/
public interface ListInterface<E> {
/**
* 新增元素
* @param e
*/
void add(E e);
/**
* 删除元素(根据下标)
* @param i
*/
void remove(int i);
/**
* 删除元素(根据值)
* @param e
*/
void remove(Object e);
/**
* 查找值
* @param i
* @return
*/
E get(int i);
int size();
boolean isEmpty();
}
arrayList简单实现
/**
* arrayLisp简单实现
*/
public class CustArrayList<E> implements ListInterface<E> {
private static final int DEFAULT_SIZE = 10;
private int index;
private int size;
private Object data[];
public CustArrayList() {
data = new Object[DEFAULT_SIZE];
this.size = DEFAULT_SIZE;
}
@Override
public void add(E e) {
data[index++] = e;
if (index == size) {
//扩容
this.size = this.size * 2 + 1;
Object newData[] = new Object[this.size];
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
this.data = newData;
}
}
@Override
public void remove(int i) {
//数组移除
if (i >= 0 && i < index) {
for (int j = i; j < this.data.length - 1; j++) {
data[j] = data[j + 1];
}
this.index--;
}
}
@Override
public void remove(Object e) {
if (!isEmpty()) {
for (int i = 0; i < this.data.length; i++) {
if (data[i] == e) {
remove(i);
}
}
}
}
@Override
public E get(int i) {
if (i >= 0 && i < index) {
return (E) data[i];
}
return null;
}
@Override
public int size() {
return this.index;
}
@Override
public boolean isEmpty() {
return index <= 0;
}
}
4.2 单向链表简单实现
/**
* linkedList简单实现
*/
public class CustLinkedList {
private ListNode head;
private int size;
/**
* 插入链表的头部,data是要插入的数据
*
* @param data
*/
public void insertHead(int data) {
ListNode headData = new ListNode(data);
//重新指定链表头部的指向,并保留原数据
headData.next = head;
this.head = headData;
}
/**
* 插入链表中间,假设定义在第n个插入
* @param data
* @param position
*/
public void insertNth(int data, int position) {
if (position == 0) {
//插入到头部
insertHead(data);
}else {
ListNode cur = head;
for (int i = 0; i < position; i++) {
//一直往后遍历,p = p.next,指针查找
cur = cur.next;
}
ListNode newNode = new ListNode(data);
//新加的结点指向后面,保证不断链
newNode.next = cur.next;
//把当前的结点指向新加的结点
cur.next = newNode;
}
}
/**
* 删除头结点
*/
public void deleteHead() {
head = head.next;
}
/**
* 删除指定位置的结点
* @param position
*/
public void deleteNth(int position) {
if (position == 0) {
deleteHead();
}else {
ListNode cur = head;
for (int i = 0; i < position; i++) {
cur = cur.next;
}
//cur.next表示的是要删除的结点,后一个next就是要指向的结点
cur.next = cur.next.next;
}
}
public void find(int data) {
ListNode cur = head;
while (cur.next != null) {
if (cur.value == data) {
break;
}
cur = cur.next;
}
}
public void print() {
ListNode cur = head;
while (cur.next != null) {
System.out.println(cur.value);
}
cur = cur.next;
}
static class ListNode {
//值
int value;
//下一个值的指针
ListNode next;
public ListNode(int value) {
this.value = value;
this.next = null;
}
}
}
4.3 双向链表
/**
* 双向链表简单实现
* */
public class DoubleLinkedList {
/**
* 头
*/
private DNode head;
/**
* 尾
*/
private DNode tail;
public void insertHead(int data) {
DNode newNode = new DNode(data);
if (head == null) {
tail = newNode;
}else {
head.pre = newNode;
newNode.next = head;
}
head = newNode;
}
public void deleteHead(){
if (head == null) return;
if (head.next == null) {
//就一个结点
tail.next = null;
}else {
head.next.pre = null;
}
head = head.next;
}
public void deleteKey(int data) {
DNode cur = head;
while (cur.value != data) {
if (cur.next == null) {
System.out.println("没有找到结点!");
}
cur = cur.next;
}
if (cur == head) {
deleteHead();
}else {
cur.pre.next = cur.next;
if (cur == tail) {
//删除的是尾部
tail = cur.pre;
cur.pre = null;
}else {
cur.next.pre = cur.pre;
}
}
}
static class DNode{
int value;
/**
* 下一个指针
*/
DNode next;
/**
* 上个指针
*/
DNode pre;
public DNode(int value) {
this.value = value;
this.next = null;
this.pre = null;
}
}
}