/**
*
*/
package cn.com.wwh; /**
* @Description:TODO
* @author:wwh
* @time:2021-1-18 19:24:47
*/
public class SingleLinkedList <T>{ 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));
}
}

java单链表基本操作的更多相关文章

  1. Java单链表实现

    /** * * 单链表基本操作 * * @author John * */ class LinkList { private Node first; private int pos = 0; publ ...

  2. c++学习笔记—单链表基本操作的实现

    用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表).结点的查找.删除.排序.打印输出.逆置.链表销毁等基本操作. IDE:vs2013 具体实现代码如下: #include  ...

  3. Java单链表反转 详细过程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...

  4. java 单链表 练习

    练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...

  5. Java单链表反转图文详解

    Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...

  6. C++ 单链表基本操作

    链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的.本文主要有单链表的创建.插入.删除节点等. 1.概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数 ...

  7. java单链表代码实现

    用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...

  8. java单链表常用操作

    总结提高,与君共勉 概述. 数据结构与算法亘古不变的主题,链表也是面试常考的问题,特别是手写代码常常出现,将从以下方面做个小结 [链表个数] [反转链表-循环] [反转链表-递归] [查找链表倒数第K ...

  9. JAVA单链表的实现-不带头结点但带有尾指针

    1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...

随机推荐

  1. MATLAB R2019b超详细安装教程(附完整安装文件)

    摘要:本文详细介绍Matlab的安装步骤,为方便安装这里提供了完整安装文件的百度网盘下载链接供大家使用.从文件下载到证书安装本文都给出了每个步骤的截图,按照图示进行即可轻松完成安装使用.本文目录如首页 ...

  2. selenium模块跳过用户名密码验证码输入,加载浏览器标签和cookie,进行翻页爬虫多页动态加载的数据(js)

    能解决登陆一次后,之后不需要二次登陆的动态加载数据,网页保存的cookie和标签,加入到selenium自动化测试浏览器中 1 from selenium import webdriver 2 imp ...

  3. Exception in thread "main" java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibilit

    系统环境 Ubuntu 20.04 focal 问题分析 该异常出现的原因,从谷歌上可以得到答案 one of the more common causes of this exception is ...

  4. wireshark、tcpdump使用笔记

    最近使用wireshark抓包icmp协议,过滤的命令如下所示: ip.addr eq 192.168.20.54 and ip.addr eq 192.168.50.131 and (icmp) 如 ...

  5. Mysql基本操作语句 增-删-改-查

    增 INSERT INTO 表名(属性名1,属性名2) VALUES(值1,值2) 删 DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子 ...

  6. [AcWing 26] 二进制中1的个数

    点击查看代码 class Solution { public: int NumberOf1(int n) { unsigned un = n; int res = 0; while (un) { re ...

  7. FreeRTOS --(5)内存管理 heap4

    FreeRTOS 中的 heap 4 内存管理,可以算是 heap 2 的增强版本,在 <FreeRTOS --(3)内存管理 heap2>中,我们可以看到,每次内存分配后都会产生一个内存 ...

  8. Idea分享项目到全球最大同x交友网站gayhub居然失败了!我居然没有权限!来看看解决方法吧

    Idea分享项目到全球最大同x交友网站gayhub居然失败了! 事情是这样的,刚写完一个动态网页就想着部署到github上让大家看看(装逼),然而在我share project时,它告诉我: 大概意思 ...

  9. .NET混合开发解决方案7 WinForm程序中通过NuGet管理器引用集成WebView2控件

    系列目录     [已更新最新开发文章,点击查看详细] WebView2组件支持在WinForm.WPF.WinUI3.Win32应用程序中集成加载Web网页功能应用.本篇主要介绍如何在WinForm ...

  10. Spring 源码(11)Spring Bean 的创建过程(2)

    Spring Bean 的创建过程介绍了FactoryBean 的创建方式,那么接下来介绍不是FactoryBean的创建方式,在创建过程中,又会分为单例的Bean的创建,原型类型的Bean的创建等. ...