单链表简介
单链表简介
现虽然顺序表的查询很快,时间复杂度为O(1),但是增删的效率是比较低的,因为每一次增删操作都伴随着大量的数据元素移动。我们可以使用另外一种存储结构实现线性表,链式存储结构。链表是一种物理存储单元上非连续、非顺序的存储结构,其物理结构不能只管的表示数据元素的逻辑顺序,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列的结点(链表中的每一个元素称为结点)组成,结点可以在运行时动态生成。
那我们如何使用链表呢?按照面向对象的思想,我们可以设计一个类,来描述结点这个事物,用一个属性描述这个结点存储的元素,用来另外一个属性描述这个结点的下一个结点。
结点API设计:
定义结点类
// 定义节点类
public class Node<T>{
// 存储元素
T item;
// 指向下一个节点
Node next;
public Node(T item,Node next){
this.item = item;
this.next = next;
}
生成链表
单向链表
单向链表是链表的一种,它由多个结点组成,每个结点都由一个数据域和一个指针域组成,数据域用来存储数据,指针域用来指向其后继结点。链表的头结点的数据域不存储数据,指针域指向第一个真正存储数据的结点。
单向链表API设计
单向链表代码实现
package com.peihj;
import com.sun.org.apache.bcel.internal.generic.NEW;
import javax.xml.soap.Node;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
public class Linklist<T> implements Iterable<T>{
/*
* 单向链表代码实现
* */
// 记录头节点
private Node head;
private int N;
// 定义节点类
public class Node{
// 存储元素
T item;
// 指向下一个节点
Node next;
public Node(T item,Node next){
this.item = item;
this.next = next;
}
}
public Linklist(){
// 初始化头节点
this.head = new Node(null,null);
N = 0;
}
// 清空链表
public void clear(){
head.next = null;
head.item = null;
N = 0;
}
// 获取链表的长度
public int length(){
return N;
}
// 判断链表是否为空
public boolean isEmpty(){
return N == 0;
}
// 获取指定位置i处的元素
public T get(int i){
if(i<0 || i >= N){
throw new RuntimeException("位置非法");
}
Node n = head.next; // 注意这是从头节点的后一个元素开始的
for (int index = 0; index < i; index++) {
n = n.next;
}
return n.item;
}
// 向链表中添加元素,添加元素默认是在链表的尾端添加
public void insert(T t){
Node n = head;
while (n.next != null){
n = n.next;
}
// 定义一个新节点
// 原链表指向新节点
n.next = new Node(t,null);
N++;
}
// 向指定位置添加元素t
public void insert(int i,T t){
// if (i < 0 || i >=N){
// throw new RuntimeException("位置非法");
// }
// 寻找指定位置i之前的节点
Node pre = head; // 注意这是从头节点开始的
for (int j = 0; j <=i-1; j++) {
pre = pre.next;
}
// 位置i的节点
Node curr = pre.next;
// 构建新的节点,让新节点指向位置i的节点
// 前一个节点与当前这个节点进行连接
pre.next = new Node(t, curr);
// 链表的长度+1
N++;
}
// 删除指定的元素
public T remove(int i){
if (i < 0 || i > N){
throw new RuntimeException("位置非法");
}
// 寻找i之前的元素
Node pre = head;
for (int j = 0; j < i; j++) {
pre = pre.next;
}
// 当前的元素
Node curr = pre.next;
pre.next = curr.next;
N--;
return curr.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;
}
@Override
public Iterator<T> iterator() {
return new LIterator();
}
private class LIterator implements Iterator<T> {
private Node n;
public LIterator() {
this.n = head;
}
@Override
public boolean hasNext() {
return n.next != null;
}
@Override
public T next() {
n = n.next;
return n.item;
}
}
}
测试用例
package com.peihj;
public class test {
public static void main(String[] args) {
Linklist<String> list = new Linklist<>();
System.out.println(list.isEmpty());
System.out.println(list.length());
list.insert(0,"张三");
list.insert(1,"李四");
list.insert(2,"赵武");
list.insert(3,"王六");
for (Object s : list) {
System.out.println(s);
}
System.out.println(list.length());
System.out.println("==============================");
System.out.println(list.get(2));
System.out.println("==============================");
String remove = list.remove(0);
int length = list.length();
System.out.println(list);
System.out.println("==============================");
}
}
参考
黑马程序员