【Java数据结构】链表
目录
1. 概况
2. 思路
3. 定义链表节点
4. 实现方法
5. 源代码
MyLinkedList.java
test.java
1. 概况
链表在逻辑上是连续的,在物理上不一定连续。
分类:
- 单向、双向
- 带头、不带头
- 循环、非循环
2. 思路
data:数值域,里面存储的数据
next:引用变量 -> 下一个节点的地址
尾巴节点:当这个节点的next域为null的时候
头节点:整个链表当中的第一个节点
单向不带头非循环链表:
单向带头非循环链表:
单向不带头循环链表:
3. 定义链表节点
class Node {public int val;public Node next;public Node(int data) {this.data = data;}
}
4. 实现方法
//生成链表
public void createList()
//打印链表内容
public void display()
//打印链表长度
public int length()
//查找是否包含关键字key在链表当中
public boolean contains(int key)//头插法
public void addFirst(int data)
//尾插法
public void addLast(int data)//找到前一个地址
public Node searchPrev(int index)
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data)//找关键字key的前驱
public Node searchPrevNode(int key)
//删除第一次出现关键字为key的节点
public void remove(int key)
//删除所有值为key的节点
public void removeAllKey(int key)//清空链表
public void clear()
5. 源代码
MyLinkedList.java
class Node {public int data;public Node next;public Node(int data) {this.data = data;}
}
public class MyLinkedList {public Node head;//标识单链表的头节点public void createList() {Node node1 = new Node(1);Node node2 = new Node(2);Node node3 = new Node(3);Node node4 = new Node(4);Node node5 = new Node(5);node1.next = node2;node2.next = node3;node3.next = node4;node4.next = node5;this.head = node1;}//打印链表内容public void display() {Node cur = this.head;while(cur != null) {System.out.print(cur.data+" ");cur = cur.next;}System.out.println();}//打印链表长度public int length() {int count = 0;Node cur = this.head;while (cur != null) {cur = cur.next;count++;}return count;}//查找是否包含关键字key在链表当中public boolean contains(int key) {Node cur = this.head;while (cur != null) {if(cur.data == key) {return true;}cur = cur.next;}return false;}//头插法public void addFirst(int data) {Node node = new Node(data);if (this.head == null) {this.head = node;} else {node.next = this.head;this.head = node;}}//尾插法public void addLast(int data) {Node node = new Node(data);if (this.head == null) {this.head = node;} else {Node cur = this.head;while (cur.next != null) {cur = cur.next;}cur.next = node;}}//找到前一个地址public Node searchPrev(int index) {Node cur = this.head;int count = 0;while (count != index-1) {cur = cur.next;count++;}return cur;}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index, int data) {if (index < 0 || index > length()) {throw new RuntimeException("插入位置错误");}//头插法if (index == 0) {addFirst(data);return;}//尾插法if (index == length()) {addLast(data);return;}Node cur = searchPrev(index);Node node = new Node(data);node.next = cur.next;cur.next = node;}//找关键字key的前驱public Node searchPrevNode(int key) {Node cur = this.head;while (cur.next != null) {if (cur.next.data == key) {return cur;}cur = cur.next;}return null;}//删除第一次出现的关键字keypublic void remove(int key) {if (this.head == null) return;//单独判断头节点的问题if (this.head.data == key) {this.head = this.head.next;return;}Node cur = searchPrevNode(key);if (cur == null) {System.out.println("没有你要删除的节点!");return;}Node del = cur.next;cur.next = del.next;}//删除所有值为key的节点public void removeAllKey(int key) {if (this.head == null) {return;}Node prev = this.head;Node cur = this.head.next;while (cur != null) {if(cur.data == key) {prev.next = cur.next;cur = cur.next;} else {prev = cur;cur = cur.next;}}//最后判断头节点if (this.head.data == key) {this.head = this.head.next;}}//清空链表public void clear() {while (this.head != null) {Node curNext = this.head.next;this.head.next = null;this.head = curNext;}}
}
test.java
测试代码
public class test {public static void main(String[] args) {MyLinkedList myLinkedList = new MyLinkedList();myLinkedList.createList();myLinkedList.display();//1 2 3 4 5System.out.println(myLinkedList.length());myLinkedList.remove(1);myLinkedList.display();//2 3 4 5}
}
【Java数据结构】链表相关推荐
- 猴子选大王 java_基于java数据结构链表写的猴子选大王
[实例简介] 基于java数据结构链表写的猴子选大王,其实就是一个约瑟夫环问题,采用java数据结构链表写的.有点小问题.当输入一只猴子,报数为1时删除会出错.没有实现动态显示猴子的添加和删除. [实 ...
- Java数据结构链表面试题 作者:哇塞大嘴好帥(哇塞大嘴好帅) --持续更新
作者:哇塞大嘴好帥(哇塞大嘴好帅) Java数据结构链表面试题 4.1.查询链表有效数据个数 //判断有效数据个数 public int validDate(){//创建临时变量NodeDate no ...
- java数据结构-链表详解
文章目录 1.数据结构-链表详解 1.1单链表 1.1.1单链表节点的尾部添加 1.1.2单链表节点的自动排序添加 1.1.3单链表节点的修改 1.1.4单链表节点的删除 1.2单链表面试题 1.2. ...
- Java数据结构--链表
public interface Predecessor<E> {//定义接口 用于实现多态public ListNode<E> getNext(); public void ...
- java数据结构 -链表 -获取有效节点个数,单链表中倒数k个节点
// 1.获取到单链表的节点的个数(如果有头结点,不统计头结点)public static int getLength(HeroNode head){if (head.next == null){re ...
- 【Java数据结构[链表--单向链表]】
单向链表 链表是以节点的方式来存储的,是链式存储 每个节点包含一个数据域用来保存当前节点的数据,一个next域用于指向下一个节点 单链表结构示意图: 单链表内存示意图: 代码实现: //链表 clas ...
- Java数据结构——链表
目录 方法一 方法二 问题一:求单链表中有效节点的个数 问题二:查找单链表中的倒数第k个结点 问题三:单链表的反转 问题三:从尾到头打印单链表 [方式1:反向遍历 . 方式2:Stack栈] 问题四: ...
- Java数据结构 -- 链表
生活中的链表 链表其实是一个一环扣一环的东西,最简单明了的就是我们的链子了,它就是一个一环扣一环的东西 链表介绍 链表在内存中的图解可以用下图来表示 链表是以节点的方式存储,是一个链式存储. 每个节点 ...
- Java数据结构和算法(七)——链表
前面博客我们在讲解数组中,知道数组作为数据存储结构有一定的缺陷.在无序数组中,搜索性能差,在有序数组中,插入效率又很低,而且这两种数组的删除效率都很低,并且数组在创建后,其大小是固定了,设置的过大会造 ...
- java双链表基本方法_Java数据结构之双端链表原理与实现方法
本文实例讲述了Java数据结构之双端链表原理与实现方法.分享给大家供大家参考,具体如下: 一.概述: 1.什么时双端链表: 链表中保持这对最后一个连点引用的链表 2.从头部插入 要对链表进行判断,如果 ...
最新文章
- 免费短信猫开发包dll函数解析及下载
- JZOJ 5710. 【北大夏令营2018模拟5.13】Mex
- MySQL 的 RowNum 实现
- oracle pipelined返回值函数 针对数据汇总统计 返回结果集方法
- web.xml详细介绍
- C++的int初始化
- 简单的数据增强代码(C++与opencv)
- 成员函数指针与高性能的C++委托(下篇)
- 【机器学习-西瓜书】七、朴素贝叶斯分类器
- 关于embedding的理解,2020-7-30
- 【路径规划】基于matlab蚁群和粒子群算法双机器人协调路径规划【含Matlab源码 045期】
- java查询到更新之前的数据_java接口中查询数据为什么都不更新,ajax最后获取的数据还是和上次一样...
- project升降级_Project-教程—从入门到精通
- 爬楼梯算法的数学思路
- 企业竞争情报系统的业务模式深入分析
- 用户名修改后进入不了计算机,更改计算机用户名后不能登录到桌面怎么办?
- 零基础该如何学好3D建模,学些什么,达到什么标准才能入行?
- 苹果怎么换行打字_2周内将你的打字速度提高3倍的方法
- 《Java程序设计》课程代码题(九)
- java开发的游戏测试_用java编写。模拟一个简单的测试打字速度的游戏
热门文章
- WEB前端 开始学习 7.29
- 7-2 删除重复字符 (20 分)
- [CTSC1999]家园 - 网络流24题
- 检测四川麻将是否胡牌算法的实现
- python consul_Consul 使用手册
- python 分类变量转为哑变量_超级详细的特征哑变量处理
- Misc(buuoj)
- 爱立信助力辽宁开通首条“5G地铁”;凯悦旗下厦门安达仕酒店正式启幕 | 美通企业日报...
- 天梯赛 L2-027 名人堂与代金券 (25 分)
- ANT+ 之【心率传感器设计】【下】