全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

java 实现双向链表实例详解

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 


相关文章: 建站之星如何助力网站排名飙升?揭秘高效技巧  Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  学校建站服务器如何选型才能满足性能需求?  如何配置支付宝与微信支付功能?  内部网站制作流程,如何建立公司内部网站?  如何快速重置建站主机并恢复默认配置?  建站之星安装后如何配置SEO及设计样式?  建站上市公司网站建设方案与SEO优化服务定制指南  如何通过老薛主机一键快速建站?  高配服务器限时抢购:企业级配置与回收服务一站式优惠方案  建站与域名管理如何高效结合?  php json中文编码为null的解决办法  如何快速生成高效建站系统源代码?  高性能网站服务器部署指南:稳定运行与安全配置优化方案  广州营销型建站服务商推荐:技术优势与SEO优化解析  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  建站之星免费版是否永久可用?  重庆市网站制作公司,重庆招聘网站哪个好?  C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换  c# 在ASP.NET Core中管理和取消后台任务  建站之星ASP如何实现CMS高效搭建与安全管理?  西安专业网站制作公司有哪些,陕西省建行官方网站?  如何配置IIS站点权限与局域网访问?  大型企业网站制作流程,做网站需要注册公司吗?  建站主机选择指南:服务器配置与SEO优化实战技巧  如何实现建站之星域名转发设置?  c# 服务器GC和工作站GC的区别和设置  C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)  微网站制作教程,我微信里的网站怎么才能复制到浏览器里?  如何用PHP快速搭建高效网站?分步指南  北京专业网站制作设计师招聘,北京白云观官方网站?  昆明网站制作哪家好,昆明公租房申请网上登录入口?  如何撰写建站申请书?关键要点有哪些?  如何通过cPanel快速搭建网站?  南宁网站建设制作定制,南宁网站建设可以定制吗?  如何快速使用云服务器搭建个人网站?  盐城做公司网站,江苏电子版退休证办理流程?  建站之星上传入口如何快速找到?  如何在云服务器上快速搭建个人网站?  如何通过VPS建站实现广告与增值服务盈利?  外贸公司网站制作,外贸网站建设一般有哪些步骤?  如何用虚拟主机快速搭建网站?详细步骤解析  如何快速搭建虚拟主机网站?新手必看指南  网站制作新手教程,新手建设一个网站需要注意些什么?  独立制作一个网站多少钱,建立网站需要花多少钱?  微信小程序制作网站有哪些,微信小程序需要做网站吗?  ppt在线制作免费网站推荐,有什么下载免费的ppt模板网站?  如何选择CMS系统实现快速建站与SEO优化? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。