java 实现双向链表实例详解

双向链表是一个基本的数据结构,在Java中LinkedList已经实现了这种结构,不过作为开发者,也要拥有自己显示这种结构的能力。话不多说,上代码:
首先是链表的节点类:
/**
* 链表节点
* @author Administrator
*
* @param <T>
*/
public class ChainNode<T> {
private T data;
//对象编号
private int dataNo;
public ChainNode<T> nextChainNode;
public ChainNode<T> preChainNode;
public ChainNode(T data, ChainNode<T> nextChainNode,
ChainNode<T> preChainNode) {
this.data = data;
this.nextChainNode = nextChainNode;
this.preChainNode = preChainNode;
}
public ChainNode(T data) {
this.data = data;
}
public int getDataNo() {
return dataNo;
}
public void setDataNo(int dataNo) {
this.dataNo = dataNo;
}
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
然后是链表:
<pre name="code" class="java">/**
* 链表实现类
* @author Administrator
*
* @param <T>
*/
public class Chain<T> {
//头节点
private ChainNode<T> headNode;
//末尾节点指针
private ChainNode<T> lastNode;
private int size;
//创建链表就创建头节点
public Chain() {
this.headNode = new ChainNode<T>(null);
this.lastNode = headNode;
}
//增加一个节点
public void addNode(T data) {
ChainNode<T> node = new ChainNode<T>(data);
if(lastNode != null){
lastNode.nextChainNode = node;
node.preChainNode = node;
node.setDataNo(size);
lastNode = node;
size++;
}
}
//删除指定索引的节点
public void deleteNode(int dataNo) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
node.preChainNode.nextChainNode = node.nextChainNode;
if(node.nextChainNode != null){
node.nextChainNode.preChainNode = node.preChainNode;
}
node.nextChainNode = null;
node.preChainNode = null;
size--;
//重新设置节点的编号
for (ChainNode<T> chainNode = node.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
chainNode.setDataNo(chainNode.getDataNo()-1);
}
return;
}
}
throw new Exception("the corresponding data that can not be found");
}
//获取指定索引的节点
public T get(int dataNo) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
return node.getData();
}
}
throw new Exception("the corresponding data that can not be found");
}
//修改对应索引的节点
public void set(int dataNo,T data) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
node.setData(data);
return;
}
}
throw new Exception("the data that is to be modified can not be found");
}
//是否包含对应数据的节点
public boolean isContains(T data) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> chainNode = headNode.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
if(chainNode.getData() == data){
return true;
}
}
return false;
}
//获取节点数量(不含头节点)
public int getSize() {
return size;
}
}
测试一下:
public class ChainTest {
public static void main(String[] args) throws Exception{
String oneString = "one";
String twoString = "two";
String threeString = "three";
String fourString = "four";
Chain<String> chain = new Chain<String>();
chain.addNode(oneString);
chain.addNode(twoString);
chain.addNode(threeString);
chain.addNode(fourString);
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
try {
String string = chain.get(2);
System.out.println("the data of the second node is"+string);
System.out.println(chain.isContains(fourString));
chain.set(1, "haha");
System.out.println("modify chain");
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
chain.deleteNode(3);
System.out.println("delete one node");
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
one two three four the data of the second node isthree true modify chain one haha three four delete one node one haha three
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# java双向链
# java双向链详解
# java双向链实例
# java数据结构之实现双向链表的示例
# Java中双向链表详解及实例
# Java实现双向链表(两个版本)
# Java语言中链表和双向链表
# JAVA实现双向链表的增删功能的方法
# java中使用双向链表实现贪吃蛇程序源码分享
# java实现单链表、双向链表
# Java双向链表按照顺序添加节点的方法实例
# java数据结构基础:单链表与双向链表
# 基于Java实现双向链表
# 链表
# 是一个
# 也要
# 数据结构
# 希望能
# 不含
# 谢谢大家
# 多说
# 测试一下
# 实现了
# nextChainNode
# dataNo
# getDataNo
# preChainNode
# size
# getData
# int
# data
# headNode
# lastNode
相关文章:
佛山企业网站制作公司有哪些,沟通100网上服务官网?
装修招标网站设计制作流程,装修招标流程?
如何在云虚拟主机上快速搭建个人网站?
如何快速搭建安全的FTP站点?
香港网站服务器数量如何影响SEO优化效果?
GML (Geography Markup Language)是什么,它如何用XML来表示地理空间信息?
如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?
潍坊网站制作公司有哪些,潍坊哪家招聘网站好?
武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?
家庭建站与云服务器建站,如何选择更优?
C++中引用和指针有什么区别?(代码说明)
如何在橙子建站中快速调整背景颜色?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
如何快速搭建FTP站点实现文件共享?
武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?
如何制作一个表白网站视频,关于勇敢表白的小标题?
英语简历制作免费网站推荐,如何将简历翻译成英文?
建站之星如何实现五合一智能建站与营销推广?
在线教育网站制作平台,山西立德教育官网?
高端建站三要素:定制模板、企业官网与响应式设计优化
大同网页,大同瑞慈医院官网?
宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何高效利用200m空间完成建站?
江苏网站制作公司有哪些,江苏书法考级官方网站?
湖北网站制作公司有哪些,湖北清能集团官网?
如何通过虚拟主机快速搭建个人网站?
C++用Dijkstra(迪杰斯特拉)算法求最短路径
jQuery 常见小例汇总
北京建设网站制作公司,北京古代建筑博物馆预约官网?
网页制作模板网站推荐,网页设计海报之类的素材哪里好?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
如何用已有域名快速搭建网站?
专业商城网站制作公司有哪些,pi商城官网是哪个?
建站主机SSH密钥生成步骤及常见问题解答?
网站制作员失业,怎样查看自己网站的注册者?
如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本
如何通过西部建站助手安装IIS服务器?
定制建站流程步骤详解:一站式方案设计与开发指南
建站之家VIP精选网站模板与SEO优化教程整合指南
如何在建站宝盒中设置产品搜索功能?
音响网站制作视频教程,隆霸音响官方网站?
惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?
如何快速搭建高效香港服务器网站?
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
建站之星后台密码如何安全设置与找回?
建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略
网站制作公司,橙子建站是合法的吗?
*请认真填写需求信息,我们会在24小时内与您取得联系。