四、链表-单向链表

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

前言

一、概述

二、结点类

1.结点类API

2.结点类实现

三、单向链表

1.单向链表API

2.单向链表实现

总结


前言

提示:这里可以添加本文要记录的大概内容:

自学JAVA数据结构笔记,跟学视频为:

黑马程序员Java数据结构与java算法全套教程,数据结构+算法教程全资料发布,包含154张java数据结构图_哔哩哔哩_bilibili

提示:以下是本篇文章正文内容,下面案例可供参考

一、概述

链表是一种物理存储单元上非连续、非顺序的存储结构,其物理结构不能只管的表示数据元素的逻辑顺序,数据元 素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列的结点(链表中的每一个元素称为结点)组成, 结点可以在运行时动态生成。

二、结点类

1.结点类API

类名         Node

构造方法 Node(T t,Node next):创建Node对象

成员变量 T item:存储数据 Node next:指向下一个结点

2.结点类实现

public class Node<T> {
    //存储元素
    public T item;
    //指向下一个结点
    public Node next;
    public Node(T item, Node next) {
        this.item = item;
        this.next = next;
    }
}

三、单向链表

1.单向链表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:记录链表的长度

2.单向链表实现

链表类:

package List;

import java.util.Iterator;

public class LinkList<T> implements Iterable<T> {
    //记录头结点
    private Node head;

    //记录链表长度
    private int N;


    //结点类
    public class Node<T> {
        //存储元素
        public T item;
        //指向下一个结点
        public Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

    //构造方法
    public LinkList() {
        //初始化头结点
        this.head = new Node(null, null);
        this.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) {
            throw new RuntimeException("位置不合法");
        }
        Node curr = head.next;

        for (int index = 0; index < i; index++) {
            curr = curr.next;
        }

        return (T) curr.item;
    }

    //向链表中添加元素t
    public void insert(T t) {
        //找到最后一个节点
        Node curr = head;
        while (curr.next != null) {
            curr = curr.next;
        }

        //创建新结点
        Node newNode = new Node(t, null);

        //将新结点加入链表
        curr.next = newNode;

        //链表长度+1
        N++;
    }

    //向指定位置i处,添加元素t
    public void insert(int i, T t) {
        if (i < 0) {
            throw new RuntimeException("位置不合法");
        }
        //寻找位置i之前的结点
        Node pre = head;
        for (int index = 0; index <= i - 1; index++) {
            pre = pre.next;
        }

        //位置i的结点
        Node curr = pre.next;

        //构建新的结点,让新结点指向位置i的结点
        Node newNode = new Node(t, curr);

        //让之前的结点指向新结点
        pre.next = newNode;

        //长度+1
        N++;
    }


    //删除指定位置i处的元素,并返回被删除的元素
    public T remove(int i) {

        if (i < 0) {
            throw new RuntimeException("位置不合法");
        }

        //寻找i之前的元素
        Node pre = head;
        for (int index = 0; index <= i - 1; index++) {
            pre = pre.next;
        }

        //当前i位置的结点
        Node curr = pre.next;

        //前一个结点指向下一个结点,删除当前结点
        pre.next = curr.next;

        //长度-1
        N--;

        return (T) curr.item;
    }


    //查找元素t在链表中第一次出现的位置
    public int indexOf(T t) {
        Node curr = head;
        for (int i = 0; curr.next != null; i++) {
            curr = curr.next;
            if (curr.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 (T) n.item;
        }
    }

}

测试类:

package List;

public class LinkListTest {
    public static void main(String[] args) throws Exception {

        LinkList<String> list = new LinkList<>();
        list.insert(0, "张三");
        list.insert(1, "李四");
        list.insert(2, "王五");
        list.insert(3, "赵六");

        //测试length方法
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println(list.length());
        System.out.println("-------------------");

        //测试get方法
        System.out.println(list.get(2));
        System.out.println("------------------------");

        //测试remove方法
        String remove = list.remove(1);
        System.out.println(remove);
        System.out.println(list.length());
        System.out.println("----------------");
        ;
        for (String s : list) {
            System.out.println(s);
        }

    }
}

总结

提示:这里对文章进行总结:
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mikudd3

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

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

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

打赏作者

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

抵扣说明:

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

余额充值