学习一下链表算法(单双链表)
1.链表定义
链表排序是由头结点连接子节点,子节点再依次连接而形成;下面两幅图片可能更加直观的能展示出来。
相较于数组排序来说,链表的插入删除十分方便,只需要断开节点之间连接再建立新链接即可。
2.参数
对于链表而言,首先需要定义节点API,单向链表由头结点节节连接,所以只需要定义内部存储数据(item)和链接下一元素(next);而双向链表需要两头相连,所以多出来一个链接上一元素(pre)。
对于单向链表,只需定义头结点(head),以及链表的总长度(N);双向链表则还需要一个尾节点(last)。
// 首节点
private Node head;
// 尾节点
private Node last;
// 链表长度
private int N;
private class Node {
public Node(T item, Node pre, Node next) {
this.item = item;
this.pre = pre;
this.next = next;
}
public T item;
public Node pre;
public Node next;
3. API实现
类名 | LinkList(单向链表) |
---|---|
构造方法 | LinkList():创建LinkList对象 |
成员方法 | 1.public void clear():空置线性表 2.publicboolean isEmpty():判断线性表是否为空,是返回true,否返回false 3.public int length():获取线性表中元素的个数 4.public T get(int i):读取并返回线性表中的第i个元素的值 5.public void insert(T t):往线性表中添加一个元素; 6.public void insert(int i,T t):在线性表的第i个元素之前插入一个值为t的数据元素。 7.public T remove(int i):删除并返回线性表中第i个数据元素。 8.public int indexOf(T t):返回线性表中首次出现的指定的数据元素的位序号,若不存在,则返回-1。 |
成员内部类 | private class Node:结点类 |
成员变量 | 1.private Node head:记录首结点 2.private int N:记录链表的长度 |
类名 | TowWayLinkList(双向链表) |
---|---|
构造方法 | TowWayLinkList():创建TowWayLinkList对象 |
成员方法 | 1.public void clear():空置线性表 2.publicboolean isEmpty():判断线性表是否为空,是返回true,否返回false 3.public int length():获取线性表中元素的个数 4.public T get(int i):读取并返回线性表中的第i个元素的值 5.public void insert(T t):往线性表中添加一个元素 6.public void insert(int i,T t):在线性表的第i个元素之前插入一个值为t的数据元素 7.public T remove(int i):删除并返回线性表中第i个数据元素 8.public int indexOf(T t):返回线性表中首次出现的指定的数据元素的位序号,若不存在,则返回-1 9.public T getFirst():获取第一个元素 10.public T getLast():获取最后一个元素 |
成员内部类 | private class Node:结点类 |
成员变量 | 1.private Node first:记录首结点 2.private Node last:记录尾结点 3.private int N:记录链表的长度 |
可以看出双向链表由于双向链接原因,代码实现比单向链表稍微复杂一点点。所以下面代码实现我会以双向链表来实现,实现代码块之前应该思考如何实现,这几天的学习让我对此也更加深刻许多,我简单写了几个成员变量实现步骤,其他步骤也欢迎大家跟我讨论补充呀:
package alogorithm.linear;
import java.util.Iterator;
public class TowWayLinklist<T> implements Iterable<T> {
// 首节点
private Node head;
// 尾节点
private Node last;
// 链表长度
private int N;
private class Node {
public Node(T item, Node pre, Node next) {
this.item = item;
this.pre = pre;
this.next = next;
}
public T item;
public Node pre;
public Node next;
}
// 创造函数
public TowWayLinklist() {
// 初始化头结点和尾节点
this.head = new Node(null, null, null);
this.last = null;
// 初始化元素个数
this.N = 0;
}
// 清空列表
public void clear() {
this.head.next = null;
this.last = null;
this.N = 0;
}
/**
* 返回链表长度
*
* @return
*/
public int length() {
return N;
}
/**
* 判断链表是否为空
*
* @return
*/
public boolean isEmpty() {
return N == 0;
}
// 返回第一个元素
public T getFirst() {
if (isEmpty()) {
return null;
}
return head.next.item;
}
// 返回最后一个元素
public T getLast() {
if (isEmpty()) {
return null;
}
return last.item;
}
// 插入元素
public void insert(T t) {
if (isEmpty()) {
// 如果链表为空
// 创建新节点
Node NewNode = new Node(t, head, null);
// 让此节点成为尾节点
last = NewNode;
// 头结点连接尾节点
head.next = last;
} else {
// 如果链表不为空
// 创建新节点
Node NewNode = new Node(t, last, null);
// 尾节点指向新节点
last.next = NewNode;
// 新节点成为尾节点
last = NewNode;
}
N++;
}
// 指定位置插入
public void insert(int i, T t) {
// 先找到i位置前一个节点
Node n = head;
for (int index = 0; index < i; index++) {
n = n.next;
}
// 在找到i位置节点
Node curr = n.next;
// 创建新节点
Node newNode = new Node(t, n, curr);
// 前节点指向新节点
n.next = newNode;
// 尾节点前指向新节点
curr.pre = newNode;
N++;
}
// 获取i处数据
public T get(int i) {
Node n = head.next;
for (int index = 0; index < i; index++) {
n = n.next;
}
return n.item;
}
// 获取t元素第一次在链表中出现的位置
public int indexOf(T t) {
Node n = head;
for (int i = 0; n.next != null; i++) {
n = n.next;
if (n.item.equals(t)) {
return i;
}
}
return -1;
}
// 删除指定i位置节点返回值
public T remove(int i) {
// 获取i前一个节点
Node n = head;
for (int index = 0; index < i; index++) {
n = n.next;
}
T ok = (n.next).item;
// 获取i后一个节点
Node curr = (n.next).next;
// i前一个节点指向i后一个节点
n.next = curr;
// i后节点指向i前一个节点
curr.pre = n;
// 元素减一
N--;
return ok;
}
4. 遍历实现
在将链表基本方法实现后,再实现遍历方法,遍历方法实现我还不是特别了解全过程,欢迎大家多多交流我也会多多学习之后补充的!
@Override
public Iterator<T> iterator() {
return new TIterator();
}
private class TIterator implements Iterator<T> {
private Node n;
public TIterator() {
this.n = head;
}
@Override
public boolean hasNext() {
return n.next != null;
}
@Override
public T next() {
n = n.next;
return n.item;
}
@Override
public void remove() {
}
}
}
5.复杂度分析
-
get(int i):每一次查询,都需要从链表的头部开始,依次向后查找,随着数据元素N的增多,比较的元素越多,时间复杂度为O(n);
-
insert(int i,T t):每一次插入,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n);
-
remove(int i):每一次移除,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n)。
总的来说,链表相较于数组排序,能用更少的时间来实现增删,适合数据修改多的时候使用。
**以上就是最近我对链表学习的一些知识总结啦,学习可能不是特别全面,欢迎大家多多讨论交流,私密马赛 **