Java 精炼解读数据结构的链表的概念与实现
作者:K媾? 时间:2022-03-02 05:17:11
前言:
顺序表的问题及思考
1. 顺序表中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续 插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考: 如何解决以上问题呢?下面给出了链表的结构来看看。
一、什么是链表
链表的概念
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
链表的结构
链表结构分为8种:
这里我们只讲最下面两种,因为在工作中、业务里、考试题、刷到的链表题、面试题里面都是用到这两种链表。
链表如何存储数据
链表是由一个一个节点组成的。(这里我们以单链表为例)
什么叫做节点?
节点分为两个域 ,假设一个叫做val域,一个叫做next域。
val:数据域
next:下一个节点的地址
链表的实现
//ListNode代表一个节点
class ListNode{
public int val;
public ListNode next;
//构造方法
public ListNode(int val){
this.val = val;
}
}
//MyLinkedList 代表这是一个链表
public class MyLinkedList {
public ListNode head;//链表的头引用,所以定义在链表里,head是链表的头,不是节点的头,节点只有两个属性,一个属性是val值,一个属性是next值,所以不能定义在ListNode类里面
ListNode listNode = new ListNode(2);//节点实例化,val域赋值为2
}
穷举法创建链表
/MyLinkedList 代表这是一个链表
public class MyLinkedList {
public ListNode head;//链表的头引用,所以定义在链表里
public void createList(){
ListNode listNode0 = new ListNode(11);
ListNode listNode1 = new ListNode(26);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(45);
ListNode listNode4 = new ListNode(56);
listNode0.next = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
this.head = listNode0;
}
打印链表
//打印链表
public void display(){
ListNode cur = this.head;
while (cur != null){
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
打印结果:
查找是否包含关键字key是否在单链表当中
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
打印结果:
得到单链表的长度:
//得到单链表的长度
public int size(){
ListNode cur = this.head;
int count = 0;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
打印结果:
头插法
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if (this.head == null) {
this.head = node;
} else {
node.next = this.head;
head = node;
}
}
打印结果:
尾插法
//尾插法
public void addLast(int data){
ListNode node = new ListNode(data);
ListNode cur = this.head;
if(this.head == null){
this.head = node;
}else {
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
}
打印结果:
任意位置插入,第一个数据节点为0号下标
public ListNode findIndex(int index){
ListNode cur = this.head;
while(index -1 != 0){
cur = cur.next;
index--;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
if(index < 0 || index > size()){
System.out.println("位置不合法");
return;
}
if(index == 0){
addFirst(data);
return;
}
if(index == size()){
addLast(data);
return;
}
ListNode cur = findIndex(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
打印结果:
删除第一次出现关键字为key的节点
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head == null){
System.out.println("没有你要删除的节");
return;
}
if (this.head.val == key){
this.head = this.head.next;
return;
}
ListNode cur = this.head;
while (cur.next != null){
if(cur.next.val == key){
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
if(cur.next == null){
System.out.println("没有该节点");
return;
}
}
打印结果:
删除所有值为key的节点
//删除所有值为key的节点
public ListNode removeAllKey(int key){
if(this.head == null) return null;
ListNode prev = this.head;
ListNode cur = this.head;
while (cur != null){
if(cur.val == key){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
if(this.head.val == key){
this.head = this.head.next;
}
return this.head;
}
打印结果:
总结:
本文简单介绍了数据结构的链表,如何创建链表,链表上如何操作数据。通过简单例题的方式加深对顺序表的理解。上述就是今天的内容,有任何疑问的话可以随时私信我,文章哪里出现了问题我都会积极改正,也希望大家能更快的掌握自己想要的知识,让我们一起加油!!!!!
来源:https://blog.csdn.net/m0_64397675/article/details/123401757
标签:Java,数据结构,链表
0
投稿
猜你喜欢
C#不重复输出一个数组中所有元素的方法
2022-07-02 14:25:58
SpringCloud Feign转发请求头(防止session失效)的解决方案
2022-08-29 12:25:59
java实现鲜花销售系统
2023-08-29 20:23:42
Spring源码完美导入IDEA的过程
2023-05-13 14:11:50
详解Java后端优雅验证参数合法性
2021-09-06 16:07:22
java版微信公众平台消息接口应用示例
2022-10-04 10:22:58
Java 实现网络爬虫框架详细代码
2021-12-11 05:15:43
spring mvc 实现获取后端传递的值操作示例
2023-08-10 12:55:52
MyBatis源码浅析(一)开篇
2022-09-28 03:28:24
详解Java中Callable和Future的区别
2023-07-25 21:18:58
Jackson序列化和反序列化忽略字段操作
2022-08-29 14:01:14
java实现简单年龄计算器
2022-01-28 02:23:34
MyBatis中一对多的xml配置方式(嵌套查询/嵌套结果)
2023-11-16 16:34:23
JavaWeb建立简单三层项目步骤图解
2023-03-08 16:51:02
java图形界面编程之模拟血压计
2023-10-01 07:16:05
Spring MVC 基于URL的映射规则(注解版)
2021-05-23 15:05:09
Java中的字节,字符输出流与字节和字符输入流的简单理解
2022-11-30 01:56:13
SpringBoot配置GlobalExceptionHandler全局异常处理器案例
2023-06-11 12:14:36
SpringBoot+SpringSession+Redis实现session共享及唯一登录示例
2023-10-07 07:56:17
Java最全文件操作实例汇总
2023-11-14 13:00:17