单链表简介

单链表简介

单链表简介

现虽然顺序表的查询很快,时间复杂度为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("==============================");


    }
}

在这里插入图片描述

参考

黑马程序员

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peihj2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值