java单链表基本操作
/**
*
*/
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单链表基本操作的更多相关文章
- Java单链表实现
/** * * 单链表基本操作 * * @author John * */ class LinkList { private Node first; private int pos = 0; publ ...
- c++学习笔记—单链表基本操作的实现
用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表).结点的查找.删除.排序.打印输出.逆置.链表销毁等基本操作. IDE:vs2013 具体实现代码如下: #include ...
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
- java 单链表 练习
练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...
- Java单链表反转图文详解
Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...
- C++ 单链表基本操作
链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的.本文主要有单链表的创建.插入.删除节点等. 1.概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数 ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
- java单链表常用操作
总结提高,与君共勉 概述. 数据结构与算法亘古不变的主题,链表也是面试常考的问题,特别是手写代码常常出现,将从以下方面做个小结 [链表个数] [反转链表-循环] [反转链表-递归] [查找链表倒数第K ...
- JAVA单链表的实现-不带头结点但带有尾指针
1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...
随机推荐
- 学习打卡day14&&构建之法阅读笔记第二篇
对于书中所提到的结对编程我还是有些许感受的,在大二上学期我就有和同学合作,共同完成编码.有时候可能是我来做非常非常简易的前端页面部分,然后给同学一个基础框架,让同学往框架里面填充,时而遇到问题我再来沟 ...
- rabbitmq简单运用
<?php /** * 生产者 */ $connection = new AMQPConnection([ 'host' => '192.168.23.130', 'port' => ...
- 面试官:为什么Vue中的v-if和v-for不建议一起用?
一.作用 v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 true值的时候被渲染 v-for 指令基于一个数组来渲染一个列表.v-for 指令需要使用 item in item ...
- KotlinMall实战之注册部分MVP架构配置
包目录如下: ①BaseView部分:基本的回调 interface BaseView { fun showLoading() fun hideLoading() fun onError()} ②Ba ...
- GAIA-IR: GraphScope 上的并行化图查询引擎
在本文中,我们将介绍 GraphScope 图交互式查询引擎 GAIA-IR,它支持高效的 Gremlin 语言表达的交互图查询,同时高度抽象了图上的查询计算,具有高可扩展性. 背景介绍 在海量数据的 ...
- docker:registry
存放docker镜像(mage)的地址,可供人上传下载镜像包: 下载 docker search whalesay --搜索whalesay镜像,该镜像用命令行的形式画了个鲸鱼并说了句话 docker ...
- BUUCTF-Web:[GXYCTF2019]Ping Ping Ping
题目 解题过程 1.题目页面提示?ip=,猜测是让我们把这个当做变量上传参数,由此猜想是命令注入 2.用管道符加上linux常用命令ls(windwos可以尝试dir)试试 所谓管道符(linux)的 ...
- Idea使用入门
jeecgboot推荐使用的idea是2019,原使用的2017在自动识别maven项目上有点问题,甚至2019在一些报错下仍然可以直接运行项目 idea的安装结构(卸载细节涉及) 下载文件位 ...
- Swift字符串操作-持续更新-2022
Swift字符串追加 var str = "OC" str.append(" Swfit") print(str) // 输出结果: OC Swift 输出结果 ...
- Microsoft Graph 的 .NET 6 之旅
这是一篇发布在dotnet 团队博客上由微软Graph首席软件工程师 Joao Paiva写的文章,原文地址: https://devblogs.microsoft.com/dotnet/micros ...