IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 【数据结构链表(一)】一一一一单向链表 -> 正文阅读

[数据结构与算法]【数据结构链表(一)】一一一一单向链表

【数据结构链表(一)】一一一一单向链表

1、🍎 什么是单向链表

? 链表包含单链表,双向链表,循环链表等等。相对于线性表,添加,删除操作非常方便,因为不用移动大量的节点,只需要修改对应的前后节点指针即可。下面用一个具体实例来说明下这种结构。现在有一需求,是将具有不同编号,姓名,昵称的人添加到系统中。首先需要创建节点,既然是链表,节点除了基本信息也要加入下一节点指针,方便计算机在内存中查找。

? 单向链表是通过指针构建的列表,基本结构就是头节点+下一节点地址指针—>节点+下一节点地址指针—>尾节点。
在这里插入图片描述

2、🍌 概念

**单链表:**用一个指向后继元素的指针将具有线性关系的各个结点链接起来,并且最后一个节点的后继指针为空指针。

img

img

3、🍊 链表特点

  1. 获取数据麻烦,需要遍历查找,比数组慢
  2. 方便插入、删除

4、🎃 单向链表的实现原理

  1. 创建一个节点类,其中节点类包含两个部分,第一个是数据域(你到时候要往节点里面储存的信息),第二个是引用域(相当于指针,单向链表有一个指针,指向下一个节点;双向链表有两个指针,分别指向下一个和上一个节点)
  2. 创建一个链表类,其中链表类包含三个属性:头结点、尾节点和大小,方法包含添加、删除、插入等等方法。

4.1 🌛 单向链表的实现类

public class Node {
    public Object data;
    public Node next;

    public Node(Object e){
        this.data = e;
    }
}

4.2 🅱? 如何自己实现链表

4.2.1 1?? 创建一个节点类

💅:注意这里的类节点是创建链表类的一个内部类;

/**
 * @author xiaoCoder
 * @version 1.0
 * @description: TODO 单向列表实现
 * @date 2021/7/29 15:03
 */
 private class Node {
        private T data;// 数据信息
        private Node next;// 下一节点的引用

        public Node() {
        }

        public Node(T data) {
            this.data = data;
        }

        // 添加节点
        public void add(T data) {
            if (this.next == null) {
                this.next = new Node(data); // 如果当前节点为空就直接添加
            } else {
                this.next.add(data);
            }
        }

        public T get(int index) {
            if (ListDemoXiao.this.foot++ == index) {
                return this.data;
            } else {
                return this.next.get(index);
            }
        }

        public boolean contains(T data) {
            if (this.data.equals(data)) {
                return true;
            } else {
                if (this.next == null) {
                    return false;
                } else {
                    return this.next.contains(data);
                }
            }
        }

        public void replace(T oldData, T newData) {
            if (this.data.equals(oldData)) {
                this.data = newData;
            } else {
                if (this.next == null) {
                    System.out.println("没有找到替换的元素!");
                } else {
                    this.next.replace(oldData, newData);
                }
            }
        }

        public void remove(Node previous, T data) {
            if (this.data.equals(data)) {
                previous.next = this.next;
                this.next = null;
                ListDemoXiao.this.count--;
                return;
            } else {
                if (this.next != null) {
                    this.next.remove(this, data);
                } else {
                    return;
                }
            }
        }

        public void remove(Node previous, int index) {
            if (ListDemoXiao.this.foot++ == index) {
                previous.next = this.next;
                this.next = null;
                ListDemoXiao.this.count--;
            }else {
                this.next.remove(this,index);
            }
        }
    }

4.2.2 2?? 创建链表类

package com.dataStructure;

/**
 * @author xiaoCoder
 * @version 1.0
 * @description: TODO 单向列表实现
 * @date 2021/7/29 15:03
 */
public class ListDemoXiao<T> {

    public static void main(String[] args) {
        ListDemoXiao<String> myList = new ListDemoXiao<>();
        myList.add("111");
        myList.add("222");
        myList.add("333");
        myList.add("444");
        myList.add("555");
        //System.out.println(myList.get(1));
        //System.out.println(myList.contains("222"));
        //myList.replace("222", "dasdsad");
        //System.out.println(myList.get(1));
        Object[] objects = myList.toArray();
        for (Object object : objects) {
            System.out.println(object.toString());
        }
        System.out.println("rrrrrrrrrrrrrrrrrrrrr");
        //myList.remove("111");
        //for (Object o : myList.toArray()) {
        //    System.out.println(o.toString());
        //}
        myList.remove(1);
        for (Object o : myList.toArray()) {
            System.out.println(o.toString());
        }
    }

    private int foot; // 根节点索引的位置
    private int count;// 链表的长度
    private Node root;// 标识根节点

    private class Node {
        private T data;// 数据信息
        private Node next;// 下一节点的引用

        public Node() {
        }

        public Node(T data) {
            this.data = data;
        }

        // 添加节点
        public void add(T data) {
            if (this.next == null) {
                this.next = new Node(data); // 如果当前节点为空就直接添加
            } else {
                this.next.add(data);
            }
        }

        // 根据索引获取数据
        public T get(int index) {
            if (ListDemoXiao.this.foot++ == index) {
                return this.data;
            } else {
                return this.next.get(index);
            }
        }

        // 查看链表中是否包含当前数据
        public boolean contains(T data) {
            if (this.data.equals(data)) {
                return true;
            } else {
                if (this.next == null) {
                    return false;
                } else {
                    return this.next.contains(data);
                }
            }
        }

        // 根据数据来替换对应的数据信息
        public void replace(T oldData, T newData) {
            if (this.data.equals(oldData)) {
                this.data = newData;
            } else {
                if (this.next == null) {
                    System.out.println("没有找到替换的元素!");
                } else {
                    this.next.replace(oldData, newData);
                }
            }
        }

        // 找到相同的node节点进行删除
        public void remove(Node previous, T data) {
            if (this.data.equals(data)) {
                previous.next = this.next;
                this.next = null;
                ListDemoXiao.this.count--;
                return;
            } else {
                if (this.next != null) {
                    this.next.remove(this, data);
                } else {
                    return;
                }
            }
        }

        // 根据索引值删除对呀的node节点
        public void remove(Node previous, int index) {
            if (ListDemoXiao.this.foot++ == index) {
                previous.next = this.next;
                this.next = null;
                ListDemoXiao.this.count--;
            }else {
                this.next.remove(this,index);
            }
        }
    }

    public boolean isEmpty() {
        return (count == 0 || this.root == null);
    }

    // 往链表中加入数据
    public void add(T data) {
        if (this.isEmpty()) {
            this.root = new Node(data);
        } else {
            this.root.add(data);
        }
        this.count++;
    }

    // 获取链表中指定索引上的数据
    public T get(int index) {
        if (this.isEmpty()) {
            return null;
        }
        this.foot = 0;
        return this.root.get(index);
    }

    public boolean contains(T data) {
        if (this.isEmpty()) {
            return false;
        }
        return this.root.contains(data);
    }

    public void replace(T oldData, T newData) {
        if (this.isEmpty()) {
            return;
        }
        this.root.replace(oldData, newData);
    }

    public Object[] toArray() {
        if (this.isEmpty()) return null;

        int count = this.count;
        Object[] objects = new Object[count];
        for (int i = 0; i < objects.length; i++) {
            objects[i] = this.get(i);
        }
        return objects;
    }

    /**
     * 根据传入的数值进行删除
     *
     * @param data
     */
    public void remove(T data) {
        if (this.isEmpty()) return;

        if (this.root.data.equals(data)) {// 删除的正好是根节点
            Node temp = this.root;
            this.root = this.root.next;
            temp.next = null;
            this.count--;
            return;
        } else {
            this.root.remove(this.root, data);
        }
    }

    /**
     *  Delete based on the index
     * @param index
     */
    public void remove(int index) {
        if (this.isEmpty()) {
            return;
        }
        if (index < 0 || this.count <= index) {
            return;
        }
        if (index == 0) {
            Node temp = this.root;
            this.root = this.root.next;
            temp.next = null;
            this.count--;
            return;
        } else {
            this.foot = 0;
            this.root.remove(this.root, index);
        }
    }
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-30 12:59:14  更:2021-07-30 13:00:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 12:25:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码