数据结构之链表(Linked list)
链表概述
单链表(Singly linked list)
双链表(Doubly linked list)
循环链表(Circular linked list)
链表操作
单链表操作
//E->anyType object
private static class Node<E> {
//数据项
private E data;
//指向next节点的引用
private Node<E> next; //构造
public Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
private Node<E> head = null;
插入
插入链表首部
public void addFirst(E item) {
head = new Node<E>(item, head);
}
插入目标节点的前面
public void insertBefore(E key, E toInsert) {
//若head为null,即链表为空,不存在目标节点 直接返回
if(head == null) return;
//若head指向的链表第一个节点 就是目标节点,即要把新建节点插入链表首部
if(head.data.equals(key)) {
addFirst(toInsert);
return;
} Node<E> prev = null;
Node<E> curr = head;
//curr定义了 指向当前节点,prev指向当前节点的上个节点。一直顺序查找,若找到目标节点(数据为key),则curr指向目标节点,prev指向了目标节点的上个节点。
while(curr != null && !curr.data.equals(key)) {
prev = curr;
curr = curr.next;
}
//若curr不为空,即找到目标节点,在curr和prev之间插入了新节点(数据为toInsert)。若curr为null,即没找到。
if(curr != null) prev.next = new Node<E>(toInsert, curr);
}
插入某个节点后面
public void insertAfter(E key, E toInsert) {
Node<E> curr = head;
//查找目标节点,找到即curr指向目标节点
while(curr != null && !curr.data.equals(key)) {
curr = curr.next;
} //curr不为null,即找到目标节点。在之后插入即可。
if(curr != null)
curr.next = new Node<E>(toInsert, curr.next);
}
插入链表尾部
public void addLast(E item) {
if(head == null) {
addFirst(item);
} else {
Node<E> tmp = head;
while(tmp.next != null) tmp = tmp.next;
tmp.next = new Node<E>(item, null);
}
}
删除
public void remove(E key) {
//head为null,即链表为空
if(head == null) throw new RuntimeException("linkedlist is null, cannot delete"); //链表第一个节点即目标节点,改变head指向下个节点就可以了。
if(head.data.equals(key)) {
head = head.next;
return;
} Node<E> prev = null;
Node<E> curr = head;
//查找,若找到即curr指向目标节点,prev指向目标节点的上个节点
while(curr != null && !curr.data.equals(key)) {
prev = curr;
curr = curr.next;
}
//curr为null,即没找到
if(curr == null) throw new RuntimeException("cannot find your node, cannot delete");
//curr不为null,删除目标节点(即改变prev的next 指向curr的next即可)
prev.next = curr.next;
}
双链表操作
transient int size = 0; /**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;//指向首部(第一个节点) /**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;//指向尾部(最后一个节点) private static class Node<E> {
//数据项
E item;
//指向下个节点
Node<E> next;
//指向上个节点
Node<E> prev; Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
插入到链表首部
/**
* Links e as first element.
*/
private void linkFirst(E e) {
//f指向first指向的节点。即f和first同时指向第一个节点。f在方法中标志 链表插入前的第一个节点
final Node<E> f = first;
//创建新的节点,节点的prev指向null,next指向f(即链表插入前第一个节点)
final Node<E> newNode = new Node<>(null, e, f);
//first指向新节点(链表首部已改变)
first = newNode;
if (f == null)
//若f为null,即插入前链表是空的,插入到新节点既是开始节点也是结束节点,所以last也指向了它
last = newNode;
else
//如果f不为空,则让f(插入前的第一个节点)的prev指向新节点就完成了。
f.prev = newNode;
size++;
modCount++;
}
插入到某个元素前面
/**
* Inserts element e before non-null Node succ.
*/
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
//这里把succ称为目标节点。succ在这里是已经查找到并存在的,查找过程可参考单链表中相关代码。
//用pred指向 目标节点succ的上个节点。
final Node<E> pred = succ.prev;
//新建节点,prev指向pred 即新建节点的prev指向目标节点的上个节点,next指向目标节点
final Node<E> newNode = new Node<>(pred, e, succ);
//目标节点的prev指向 新建节点
succ.prev = newNode;
if (pred == null)
//若pred为null 是目标节点上个节点为null,即链表插入前只有一个节点,所以first会指向新建节点。
first = newNode;
else
//若pred不为null,目标节点的上个节点的next指向新节点
pred.next = newNode;
size++;
modCount++;
}
插入到链表尾部
/**
* Links e as last element.
*/
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
删除
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If this list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* {@code i} such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns {@code true} if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return {@code true} if this list contained the specified element
*/
public boolean remove(Object o) {
//这里就是区分o是否为null,找到第一个指定element的节点,通过unlink删除。
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
} /**
* Unlinks non-null node x.
*/
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev; if (prev == null) {
//prev为null 即x是首部(第一个节点),first指向next即可
first = next;
} else {
//prev不为null,x与prev之间关联即断开。prev的next指向next了,x的prev为null
prev.next = next;
x.prev = null;
} //同prev处理类似,断开x与next联系
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
} x.item = null;
size--;
modCount++;
return element;
}
循环链表
private Node<E> head = null;
private Node<E> tail = null; private class Node<E> {
E value;
Node<E> next;
public Node(E value) {
this.value = value;
}
} //往尾部(即tail节点之后)添加节点
public void addNode(E value) {
Node<E> newNode = new Node<>(value);
if (head == null) {
//head为null,即链表为空,head指向新建节点。
head = newNode;
} else {
tail.next = newNode;
}
//tail指向新节点,即新节点相当于链表的尾部
tail = newNode;
//tail.next即新节点next指向head,形成环
tail.next = head;
} //查找,链表中是否包含数据为searchValue的节点
public boolean containsNode(E searchValue) {
Node<E> currentNode = head;
if (head == null) {
return false;
} else {
//以head开始依次向后查找,直到碰到的仍是head停止。找到返回
do {
if (currentNode.value == searchValue) {
return true;
}
currentNode = currentNode.next;
} while (currentNode != head);
return false;
}
} //删除查找到的第一个数据为valueToDelete的节点
public void deleteNode(E valueToDelete) {
Node<E> currentNode = head;
if (head != null) {
if (currentNode.value == valueToDelete) {
//head节点且值为valueToDelete
head = head.next;
tail.next = head;
} else {
//以head开始依次向后查找,直到碰到的仍是head停止。找到删除
do {
Node<E> next = currentNode.next;
if (next.value == valueToDelete) {
currentNode.next = next.next;
break;
}
currentNode = currentNode.next;
} while (currentNode != head);
}
}
}
链表与数组比较
简单做了个表格
链表
|
数组
|
动态分配:需要时才分配内存
|
固定分配:大小固定,new时即分配所有内存
|
分散存储:内存中不连续存储
|
连续存储:内存中连续存储
|
总量限制:由于分散存储,受内存总量限制
|
使用限制:由于连续存储,若无合适连续控件即无法完成分配,且容易形成内存碎片
|
插入/删除方便:改变节点中指向next的或者prev的引用即可
|
插入/删除代价大:需创建新数组并移动元素
|
有内存浪费:节点中需额外存储next或prev的信息
|
无内存浪费:数组元素只存放数据
|
顺序访问:在某个节点沿某一方向只能逐一访问
|
随机访问:可以直接计算得到某一元素的地址
|
链表的一些操作复杂度
操作
|
链表
|
数组
|
访问(访问第N个元素)
|
O(n)
|
O(1)
|
插入到首部
|
O(1)
|
O(n)
|
插入到尾部
|
有尾部引用tail:O(1)
无尾部引用O(n)
|
O(n)
|
插入到中部
|
查找时间+O(1)~=O(n)
|
O(n) |
查找
|
O(n)
|
O(n) |
删除 |
类似插入,看删除位置或者链表的设计
|
O(n)
|
数据结构之链表(Linked list)的更多相关文章
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
- 数据结构-单链表(Linked List)
#include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...
- 数据结构与算法 —— 链表linked list(01)
链表(维基百科) 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储, ...
- Java数据结构之链表(Linked List)
1.链表(Linked List)介绍 链表是有序的列表,但是它在内存存储结构如下: 2.特点: 链表是以节点的方式来存储,是链式存储 每个节点包含 data 域, next 域:指向下一个节点. 链 ...
- Python与数据结构[0] -> 链表/LinkedList[0] -> 单链表与带表头单链表的 Python 实现
单链表 / Linked List 目录 单链表 带表头单链表 链表是一种基本的线性数据结构,在C语言中,这种数据结构通过指针实现,由于存储空间不要求连续性,因此插入和删除操作将变得十分快速.下面将利 ...
- 链表Linked List
链表Linked List 1. 链表 数组是一种顺序表,index与value之间是一种顺序映射,以O(1)O(1)的复杂度访问数据元素.但是,若要在表的中间部分插入(或删除)某一个元素时,需要将后 ...
- 数据结构之链表(LinkedList)(一)
链表(Linked List)介绍 链表是有序的列表,但是它在内存中是存储如下 1)链表是以节点方式存储的,是链式存储 2)每个节点包含data域(value),next域,指向下一个节点 3)各个节 ...
- 学习javascript数据结构(二)——链表
前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...
- linux内核数据结构之链表
linux内核数据结构之链表 1.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该 ...
随机推荐
- 黑马程序员_毕向东_Java基础视频教程——赋值(随笔)
赋值 class Test{ public static void main(String[] args) { int i = 3; // += -= *= /= %= 它们凑一块成为一个运算符 x ...
- MyBatis入门知识汇总
为什么要使用MyBatis? 我们都知道,在学习mybatis之前,要在Java中操作数据库,需要用到JDBC,但是在使用JDBC时会有许多缺陷. 比如: 1.使用时需要先进行数据库连接,不用后要立 ...
- ol,li,ul,dl,dt,dd
块级元素div尽量少用,其实和table一样,嵌套越少越好,它也是会影响速度的! ol 有序列表. <ol> <li>……</li> <li>……< ...
- 点击劫持ClickJacking
原文:https://beenle-xiaojie.github.io/2019/01/07/ClickJacking/ 引言 当我们的页面嵌入到一个iframe中时,安全测试提出一个于我而言很新鲜的 ...
- 索引 'GXHRCS.PK_A253' 或这类索引的分区处于不可用状态
ORA-01502: 索引 'GXHRCS.PK_A253' 或这类索引的分区处于不可用状态 http://blog.sina.com.cn/s/blog_7ab8d2720101ozw6.html ...
- 最小生成树 状压+prim hdu2489
Minimal Ratio TreeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- [批处理教程之Shell]001.文本处理
在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序. 同时 ...
- [批处理教程之Shell]002.Linux 常用命令大全
这一次收集的是比较基础的 Linux 命令,其实 Linux 命令未必每个都要记住,只要在用到时能查阅到需要的命令的用法. 系统信息 *.显示机器的处理器架构(1)arch *.显示机器的处理器架构( ...
- Python每日一练(1)
这两天在做Python的每日一练,感觉收获颇丰,所以来记录分享一下,一共做了三个,涉及socket,PIL,pymysql三个库,另外终于开始了Flask框架的学习,后续也会做出一些分析 第一个是一个 ...
- 一篇文章,全面掌握Git
版本控制 版本控制就是记录项目文件的历史变化.它为我们查阅日志,回退,协作等方面提供了有力的帮助. 版本控制一般分为集中化版本控制和分布式版本控制. 集中化主要的版本数据都保存服务端. 分布式版本数据 ...