Java如何实现无头双向链表操作
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要介绍了Java如何实现无头双向链表操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下无头双向链表的结构:代码分
千家信息网最后更新 2025年11月07日Java如何实现无头双向链表操作
这篇文章主要介绍了Java如何实现无头双向链表操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
无头双向链表的结构:

代码分析
节点结构
class Node { private int data; private Node next; private Node prev; public Node(int data) { this.data = data; this.prev = null; this.next = null; }} private Node head; // 头节点 private Node last; // 尾节点 public DoubleLinked() { this.head = null; this.last = null;}1. 头插法
/*** 1.头插法* @param data*/public void addFirst(int data) { Node node = new Node(data); if (this.head == null) { this.head = node; this.last = node; } else { node.next = this.head; this.head.prev = node; this.head = node; }}先判断链表是否为空,若为空,则直接插入,头节点和尾节点都直接指向新插入的元素;
若链表不为空,则把要插入节点的 next 指向链表头节点,头节点的 prev 指向新插入的节点,最后更新头节点为新插入节点,插入过程如下图所示:

2. 尾插法
/*** 2.尾插法* @param data*/public void addLast(int data) { Node node = new Node(data); if (this.head == null) { this.head = node; this.last = node; } else { this.last.next = node; node.prev = this.last; this.last = node; }}若链表为空,同头插法;
若链表不为空,则把链表尾节点的 next 指向要插入节点,要插入节点的 prev 指向链表尾节点,最后更新尾节点为新插入节点,插入过程如下图所示:
3. 查找是否包含关键字 key 在单链表中
// 查找 private Node searchIndex(int index) { checkIndex(index); int count = 0; Node cur = this.head; while (count != index) { cur = cur.next; count++; } return cur; } // 合法性检查 private void checkIndex(int index) { if (index < 0 || index > getLength()) { throw new IndexOutOfBoundsException("下标不合法!"); } } /** * 3.任意位置插入,第一个数据节点为0号下标 * @param index 插入位置 * @param data 插入的值 * @return true/false */ @Override public boolean addIndex(int index, int data) { if (index ==0) { addFirst(data); return true; } if (index == getLength()) { addLast(data); return true; } // cur 指向index位置的节点 Node cur = searchIndex(index); Node node = new Node(data); node.next = cur; cur.prev.next = node; node.prev = cur.prev; cur.prev = node; return true; }4. 查找是否包含关键字 key 在单链表中
/** * 4.查找是否包含关键字 key 在单链表中 * @param key 要查找的关键字 * @return true/false */@Overridepublic boolean contains(int key) { Node cur = this.head; while (cur != null) { if (cur.data == key) { return true; } cur = cur.next; } return false;}5. 删除第一次出现关键字为 key 的节点
/** * 5.删除第一次出现关键字为 key 的节点 * @param key * @return */@Overridepublic int remove(int key) { Node cur = this.head; int oldData = 0; while (cur != null) { if (cur.data == key) { oldData = cur.data; // 头节点 if (cur == this.head) { this.head = this.head.next; this.head.prev = null; } else { // cur.next != null --->不是尾节点 if (cur.next != null) { cur.next.prev = cur.prev; } else { this.last = cur.prev; } } return oldData; } cur = cur.next; } return -1;}6. 删除所有值为 key 的节点
/** * 6.删除所有值为 key 的节点 * @param key */@Overridepublic void removeAllKey(int key) { Node cur = this.head; while (cur != null) { if (cur.data == key) { // 头节点 if (cur == this.head) { this.head = this.head.next; this.head.prev = null; } else { cur.prev.next = cur.next; // cur.next != null --->不是尾节点 if (cur.next != null) { cur.next.prev = cur.prev; } else { this.last = cur.prev; } } } cur = cur.next; }}7. 得到单链表的长度
/** * 7.得到单链表的长度 * @return */@Overridepublic int getLength() { int count = 0; Node cur = this.head; while (cur != null) { count++; cur = cur.next; } return count;}8. 打印链表
/** * 8.打印链表 */@Overridepublic void display() { if (this.head == null) { return ; } Node cur = this.head; while (cur != null) { System.out.print(cur.data + " "); cur = cur.next; } System.out.println();}9. 清空顺序表以防内存泄漏
/** * 9.清空顺序表以防内存泄漏 */@Overridepublic void clear() { while(this.head != null) { Node cur = this.head.next; this.head.next = null; this.head.prev = null; this.head = cur; }}接口、实现方法、测试
1. 接口
package com.github.doubly;// 不带头节点单链表的实现public interface IDoubleLinked { // 1.头插法 void addFirst(int data); // 2.尾插法 void addLast(int data); // 3.任意位置插入,第一个数据节点为0号下标 boolean addIndex(int index, int data); // 4.查找是否包含关键字 key 在单链表中 boolean contains(int key); // 5.删除第一次出现关键字为 key 的节点 int remove(int key); // 6.删除所有值为 key 的节点 void removeAllKey(int key); // 7.得到单链表的长度 int getLength(); // 8.打印链表 void display(); // 9.清空顺序表以防内存泄漏 void clear();}2. 实现方法
package com.github.doubly;public class DoubleLinked implements IDoubleLinked { class Node { private int data; private Node next; private Node prev; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } private Node head; // 头节点 private Node last; // 尾节点 public DoubleLinked() { this.head = null; this.last = null; } /** * 1.头插法 * @param data */ @Override public void addFirst(int data) { Node node = new Node(data); if (this.head == null) { this.head = node; this.last = node; } else { node.next = this.head; this.head.prev = node; this.head = node; } } /** * 2.尾插法 * @param data */ @Override public void addLast(int data) { Node node = new Node(data); if (this.head == null) { this.head = node; this.last = node; } else { this.last.next = node; node.prev = this.last; this.last = node; } } // 查找 private Node searchIndex(int index) { checkIndex(index); int count = 0; Node cur = this.head; while (count != index) { cur = cur.next; count++; } return cur; } // 合法性检查 private void checkIndex(int index) { if (index < 0 || index > getLength()) { throw new IndexOutOfBoundsException("下标不合法!"); } } /** * 3.任意位置插入,第一个数据节点为0号下标 * @param index 插入位置 * @param data 插入的值 * @return true/false */ @Override public boolean addIndex(int index, int data) { if (index ==0) { addFirst(data); return true; } if (index == getLength()) { addLast(data); return true; } // cur 指向index位置的节点 Node cur = searchIndex(index); Node node = new Node(data); node.next = cur; cur.prev.next = node; node.prev = cur.prev; cur.prev = node; return true; } /** * 4.查找是否包含关键字 key 在单链表中 * @param key 要查找的关键字 * @return true/false */ @Override public boolean contains(int key) { Node cur = this.head; while (cur != null) { if (cur.data == key) { return true; } cur = cur.next; } return false; } /** * 5.删除第一次出现关键字为 key 的节点 * @param key * @return */ @Override public int remove(int key) { Node cur = this.head; int oldData = 0; while (cur != null) { if (cur.data == key) { oldData = cur.data; // 头节点 if (cur == this.head) { this.head = this.head.next; this.head.prev = null; } else { // cur.next != null --->不是尾节点 if (cur.next != null) { cur.next.prev = cur.prev; } else { this.last = cur.prev; } } return oldData; } cur = cur.next; } return -1; } /** * 6.删除所有值为 key 的节点 * @param key */ @Override public void removeAllKey(int key) { Node cur = this.head; while (cur != null) { if (cur.data == key) { // 头节点 if (cur == this.head) { this.head = this.head.next; this.head.prev = null; } else { cur.prev.next = cur.next; // cur.next != null --->不是尾节点 if (cur.next != null) { cur.next.prev = cur.prev; } else { this.last = cur.prev; } } } cur = cur.next; } } /** * 7.得到单链表的长度 * @return */ @Override public int getLength() { int count = 0; Node cur = this.head; while (cur != null) { count++; cur = cur.next; } return count; } /** * 8.打印链表 */ @Override public void display() { if (this.head == null) { return ; } Node cur = this.head; while (cur != null) { System.out.print(cur.data + " "); cur = cur.next; } System.out.println(); } /** * 9.清空顺序表以防内存泄漏 */ @Override public void clear() { while(this.head != null) { Node cur = this.head.next; this.head.next = null; this.head.prev = null; this.head = cur; } }}3. 测试
package com.github.doubly;public class TestDemo { public static void main(String[] args) { DoubleLinked doubleLinked = new DoubleLinked(); doubleLinked.addFirst(10); doubleLinked.addFirst(20); doubleLinked.addFirst(30); doubleLinked.addFirst(40); doubleLinked.addFirst(50); doubleLinked.display(); doubleLinked.addIndex(0,100); doubleLinked.addIndex(1,200); doubleLinked.addIndex(0,300); doubleLinked.addLast(40); doubleLinked.addLast(50); doubleLinked.display(); doubleLinked.remove(300); doubleLinked.display(); doubleLinked.removeAllKey(50); doubleLinked.display(); }}感谢你能够认真阅读完这篇文章,希望小编分享的"Java如何实现无头双向链表操作"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
节点
关键
关键字
位置
指向
下标
内存
第一次
篇文章
长度
顺序
双向
无头
数据
合法
合法性
接口
方法
结构
过程
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
谷歌商城与服务器通信时出现问题
西安软件开发好找吗
七格互联网络科技有限公司
魔兽世界怀旧服老数据库
软件开发的系统设计分为
在线视频播放服务器
应用软件开发商精装修
数据库码的英文
企业网络安全自查工作计划
珠海程序员软件开发平均工资
泰国美国网络安全计划
怎么部署python服务器
高级软件开发工程师的能力
数据库和模式是
中国网络安全法第24条
泰拉瑞亚著名服务器IP
ntp服务器搭建
在交换机上做数据库镜像
安卓驱动软件开发是什么
数据库系统基础教程 英文版
闵行区网络技术咨询创新服务
上海曲茂网络技术有限公司
我的世界开服服务器
软件开发什么赚钱
php服务器安全设置
网络安全相关面试题
网络安全进校园讲座
网络安全专业高职学校排名
营销数据库建立
联想万全服务器t168