java单链表基本操作

/**
 *
 */
package cn.com.wwh;

/**
 * @Description:TODO
 * @author:wwh
 * @time:2021-1-18 19:24:47
 */
public class SingleLinkedList {

    Node list;//头结点
    int size;//节点个数

    public class Node{
        T data;
        Node next;

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

    /**
     *
     * @Description:头部增加节点
     * @param data
     * @return
     * @author: wwh
     * @time:2021-1-18 19:27:24
     */
    public void add(T data) {
        Node node = new Node(data, list);
        list = node;
        size++;
    }

    /**
     *
     * @Description:指定位置增加结点
     * @param data
     * @param index
     * @return
     * @author: wwh
     * @time:2021-1-18 19:28:51
     */
    public void add(T data,int index) {
        checkIndex(index);
        if (index == 0) {
            add(data);
            return;
        }else {
            Node preNode = list;
            Node curNode = list;
            for (int i = 0; i < index; i++) {
                preNode = curNode;
                curNode = curNode.next;
            }
            Node node = new Node(data, curNode);
            preNode.next = node;
            size++;
        }
    }

    /**
     *
     * @Description:删除头部结点
     * @return
     * @author: wwh
     * @time:2021-1-18 19:33:51
     */
    public T remove() {
        if (list != null) {
            Node headNode = list;
            list = list.next;
            headNode.next = null;//GC
            size--;
            return headNode.data;
        }
        return null;
    }

    /**
     *
     * @Description:删除指定位置的结点
     * @param index
     * @return
     * @return
     * @author: wwh
     * @time:2021-1-18 19:35:49
     */
    public T remove(int index) {
        checkIndex(index);
        if (list != null) {
            if (index == 0) {
                return remove();
            }
            Node preNode = list;
            Node curNode = list;
            for (int i = 0; i < index; i++) {
                preNode = curNode;
                curNode = curNode.next;
            }
            preNode.next = curNode.next;
            curNode.next = null;
            size--;
            return curNode.data;
        }
        return null;
    }

    /**
     *
     * @Description:删除最后一个结点
     * @return
     * @return
     * @author: wwh
     * @time:2021-1-18 19:38:42
     */
    public T removeLast() {
        if (list != null) {
            Node preNode = list;
            Node curNode = list;
            while (curNode.next != null) {
                preNode = curNode;
                curNode = curNode.next;
            }
            preNode.next = null;
            size--;
            return curNode.data;
        }
        return null;
    }

    /**
     *
     * @Description:修改指定位置的值
     * @param index
     * @param data
     * @return
     * @author: wwh
     * @time:2021-1-18 19:41:23
     */
    public void set(int index,T data) {
        checkIndex(index);
        Node curNode = list;
        for (int i = 0; i < index; i++) {
            curNode = curNode.next;
        }
        curNode.data = data;
    }

    /**
     *
     * @Description:获取头结点
     * @return
     * @return
     * @author: wwh
     * @time:2021-1-18 19:42:48
     */
    public T get() {
        if (list != null) {
            return list.data;
        }
        return null;
    }

    /**
     *
     * @Description:获取指定位置的结点
     * @param index
     * @return
     * @return
     * @author: wwh
     * @time:2021-1-18 19:44:06
     */
    public T get(int index) {
        checkIndex(index);
        if (list != null) {
            Node curNode = list;
            for (int i = 0; i < index; i++) {
                curNode = curNode.next;
            }
            return curNode.data;
        }
        return null;
    }

    /**
     *
     * @Description:下标检查
     * @param index
     * @return
     * @author: wwh
     * @time:2021-1-18 19:29:24
     */
    public void checkIndex(int index) {
        if (!(index >= 0 && index  size)) {
            throw new IndexOutOfBoundsException(index +": out of " + size );
        }
    }

    @Override
    public String toString() {
        Node node = list;
        for (int i = 0; i < size; i++) {
            System.err.print(node.data + " ");
            node = node.next;
        }
        System.err.println();
        return super.toString();
    }

    public static void main(String[] args) {
        SingleLinkedList list1 = new SingleLinkedList();
        for (int i = 0; i < 5; i++) {
            list1.add(i);
        }
        list1.toString();
        list1.add(3);
        list1.toString();
        list1.add(34, 6);
        list1.toString();
        list1.remove();
        list1.toString();
        list1.removeLast();
        list1.toString();
        list1.remove(0);
        list1.toString();
        list1.set(0, 88);
        list1.toString();
        System.err.println(list1.get());
        System.err.println(list1.get(3));
    }
}

Original: https://www.cnblogs.com/wha6239/p/14295123.html
Author: 一只烤鸭朝北走
Title: java单链表基本操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611539/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 二手车价格预测 | 构建AI模型并部署Web应用 ⛵

    💡 作者:韩信子@ShowMeAI📘 数据分析实战系列:https://www.showmeai.tech/tutorials/40📘 机器学习实战系列:https://www.s…

    数据库 2023年6月14日
    084
  • MySQL操作题(mysql_V20190307)

    DB操作题(mysql_V20190307) 登陆数据库 MYSQL -H LOCALHOST -U ROOT -P 创建DEMO01数据库 CREATE DATABASE DEM…

    数据库 2023年6月9日
    062
  • InnoDB数据存储结构

    MySQL服务器上 &#x5B58;&#x50A8;&#x5F15;&#x64CE;负责对表中数据的读取和写入工作,不同存储引擎中 &#x5…

    数据库 2023年5月24日
    067
  • SqlSessionFactory工具类抽取

    多次SqlSessionFactory创建对象问题解决 SqlSessionFactory工具类抽取 问题描述: 当我们多次使用SqlSessionFactory创建并获取对象时会…

    数据库 2023年6月16日
    076
  • mysql解压版简洁式本地配置方式

    1. 设置全局变量 解压mysql压缩包到指定位置, 然后配置全局变量, 在 path 中添加全局变量, 值为 mysql 根目录下 bin 目录路径, 比如: D:\code_s…

    数据库 2023年6月14日
    074
  • NO.6 HTML+CSS 笔记

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    数据库 2023年6月14日
    081
  • 有趣的网络知识

    简单的网络入侵方法 命令 描述 attrib +s +a +h +r &#x78C1;&#x76D8;:&#x6587;&#x4EF6;&#…

    数据库 2023年6月11日
    0100
  • Docker镜像操作

    Docker镜像操作 Docker 镜像是由文件系统叠加而成(是一种文件的存储形式)。最底端是一个文件引 导系统,即 bootfs,这很像典型的 Linux/Unix 的引导文件系…

    数据库 2023年6月14日
    0117
  • Mysql的知识梳理

    数据准备: –建表 create table customer_jia(CID int(4), Cname varchar(20), Csex varchar(2), …

    数据库 2023年6月16日
    086
  • DistSQL 深度解析:打造动态化的分布式数据库

    一、背景 自 ShardingSphere 5.0.0 版本发布以来,DistSQL 为 ShardingSphere 生态带来了强大的动态管理能力,通过 DistSQL,用户可以…

    数据库 2023年6月16日
    075
  • [spring]spring管理的入门项目快速搭建

    1.spring简介 Spring框架是一个开源的应用程序框架,是针对bean的生命周期进行管理的轻量级容器。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了…

    数据库 2023年6月16日
    0106
  • 三分钟小短文:一致性非锁定读与一致性锁定读

    台上三分钟,台下三小时,兄弟们,今天咱们花三分钟了解下数据库中的两种读(select)操作: 一致性非锁定读 和 一致性锁定读 一致性非锁定读 什么是一致的未锁定读取?在这里,我想…

    数据库 2023年5月24日
    080
  • Linux 守护进程

    1. 守护进程是什么 2. 怎么用守护进程 2.1 有趣小例子 2.2 man daemon 3. 源码解析 3.1 GUN C daemon.c 3.2 daemon.c 解析 …

    数据库 2023年6月9日
    076
  • MySQL变量、流程控制和游标

    变量、流程控制和游标 变量 在MySQL数据库的存储过程和函数中,可以使用变量来存储查询或计算的中间结果数据,或者输出最终的结果的数据 系统变量 变量由系统定义,属于服务器级别 […

    数据库 2023年5月24日
    071
  • MySQL实战45讲 10

    10 | MySQL为什么有时候会选错索引? 使用哪个索引是由 MySQL 来确定的 可能遇到的情况:一条本来可以执行得很快的语句,却由于 MySQL 选错了索引,而导致执行速度变…

    数据库 2023年6月16日
    098
  • String字符串用逗号拼接,防止最后一位是逗号

    StringBuilder sb = new StringBuilder(); for(String s strArr) { if (sb.length() > 0) {//…

    数据库 2023年6月16日
    0104
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球