链表是一种复杂的数据结构,其数据之间的相互关系使链表分成三种:单链表、循环链表、双向链表,下面将逐一介绍。链表在数据结构中是基础,也是重要的知识点,这里讲下java 中链表的实现,
JAVA 链表操作:单链表和双链表
主要讲述几点:
一、链表的简介
二、链表实现原理和必要性
立即学习“Java免费学习笔记(深入)”;
三、单链表示例
四、双链表示例
一、链表的简介
链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都相应的应用,链表有多种类别,文章针对单链表和双链表进行分析。链表中数据就像被一个链条串联一起,轻易的可以实现数据的访问。
二、链表实现原理和必要性
立即学习“Java免费学习笔记(深入)”;
这里只分析单链表和双链表。链表的实现过程是有些许复杂的,但是会带来许多好处。比如现在网购时代到来,商家发快递一般会将商品包装在盒子里并写上地址信息,快递公司就可以通过盒子上的信息找到买家,商品完整到达。如果没有盒子的保护,有可能在途中商品受损。而链表就好比那个写了地址信息的盒子,既保护了商品信息,同时又写好了物流信息。链表之中存在一个HEAD节点,类似“火车头”,只要找到相应HEAD节点,就可以对链表进行操作。此次分析中,HEAD节点只是做数据头,不保存有效数据。
单链表的实现原理如图:

双链表实现原理:

三、单链表示例
ICommOperate
使用模板与程序分离的方式构建,依靠专门设计的数据库操作类实现数据库存取,具有专有错误处理模块,通过 Email 实时报告数据库错误,除具有满足购物需要的全部功能外,成新商城购物系统还对购物系统体系做了丰富的扩展,全新设计的搜索功能,自定义成新商城购物系统代码功能代码已经全面优化,杜绝SQL注入漏洞前台测试用户名:admin密码:admin888后台管理员名:admin密码:admin888
package LinkListTest; import java.util.Map; public interface ICommOperate{ public boolean insertNode(T node) ; public boolean insertPosNode(int pos, T node) ; public boolean deleteNode(int pos) ; public boolean updateNode(int pos, Map map) ; public T getNode(int pos, Map map) ; public void printLink() ; }
单链表节点:
package LinkListTest;
// 单连表节点
public class SNode {
private String data;
private SNode nextNode;
public SNode() {
}
public SNode(String data) {
this.data = data;
this.nextNode = new SNode();
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public SNode getNextNode() {
return nextNode;
}
public void setNextNode(SNode nextNode) {
this.nextNode = nextNode;
}
@Override
public String toString() {
return "SNode [data=" + data + "]";
}
}
单链接操作类:
package LinkListTest; import java.util.HashMap; import java.util.Map; public class SingleLinkList implements ICommOperate{ private SNode head = new SNode("HEAD") ; // 公共头指针,声明之后不变 private int size = 0 ; public int getSize() { return this.size; } /* * 链表插入,每次往末端插入 * */ @Override public boolean insertNode(SNode node) { boolean flag = false ; SNode current = this.head ; if( this.size==0 ){ // 空链表 this.head.setNextNode(node) ; node.setNextNode(null) ; }else{ // 链表内节点 while( current.getNextNode()!=null ){ current = current.getNextNode() ; } current.setNextNode(node) ; node.setNextNode(null) ; } this.size++ ; flag = true ; return flag; } /* * 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端 * */ @Override public boolean insertPosNode(int pos, SNode node){ boolean flag = true; SNode current = this.head.getNextNode() ; if( this.size==0 ){ // 空链表 this.head.setNextNode(node) ; node.setNextNode(null) ; this.size++ ; }else if( this.size 0 && pos<=this.size) { // 链表内节点 // 1、找到要插入pos位置节点和前节点 int find = 0; SNode preNode = this.head; // 前节点 SNode currentNode = current; // 当前节点 while( find this.size || current==null ){ System.out.println("位置信息错误或链表无信息"); }else{ // 1、找到要删除节点的前后节点 int find = 0; SNode preNode = this.head; // 前节点 SNode nextNode = current.getNextNode(); // 后节点 while( find map) { boolean flag = false ; SNode node = getNode(pos, map); // 获得相应位置pos的节点 if( node!=null ){ String data = (String) map.get("data") ; node.setData(data); flag = true ; } return flag; } /* * 找到指定链表的节点pos,从1开始 * */ @Override public SNode getNode(int pos, Map map) { SNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==null ){ System.out.println("位置信息错误或链表不存在"); return null; } int find = 0 ; while( find map = new HashMap<>() ; map.put("data", "this is a test") ; sll.updateNode(pos3, map) ; sll.printLink(); } }
四、双链表示例
ICommOperate
package LinkListTest; import java.util.Map; public interface ICommOperate{ public boolean insertNode(T node) ; public boolean insertPosNode(int pos, T node) ; public boolean deleteNode(int pos) ; public boolean updateNode(int pos, Map map) ; public T getNode(int pos, Map map) ; public void printLink() ; }
双链表节点:
package LinkListTest;
// 双连表节点
public class DNode {
private DNode priorNode;
private String data;
private DNode nextNode;
public DNode(){
}
public DNode(String data) {
this.priorNode = new DNode() ;
this.data = data ;
this.nextNode = new DNode() ;
}
public DNode getPriorNode() {
return priorNode;
}
public void setPriorNode(DNode priorNode) {
this.priorNode = priorNode;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public DNode getNextNode() {
return nextNode;
}
public void setNextNode(DNode nextNode) {
this.nextNode = nextNode;
}
@Override
public String toString() {
return "DNode [data=" + data + "]";
}
}
双链表实现类:
package LinkListTest; import java.util.HashMap; import java.util.Map; public class DoubleLinkList implements ICommOperate{ private DNode head = new DNode("HEAD"); private int size = 0 ; public int getSize() { return this.size; } /* * 链表插入,每次往末端插入 * */ @Override public boolean insertNode(DNode node) { boolean flag = false; DNode current = this.head ; if( this.size==0 ){ // 空链表 this.head.setNextNode(node) ; node.setPriorNode(this.head); node.setNextNode(null) ; }else{ // 链表内节点 while( current.getNextNode()!=null ){ current = current.getNextNode() ; } current.setNextNode(node); node.setNextNode(null); node.setPriorNode(current); } this.size++ ; flag = true ; return flag; } /* * 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端 * */ @Override public boolean insertPosNode(int pos, DNode node) { boolean flag = true; DNode current = this.head.getNextNode() ; if( this.size==0){ // 链表为空 this.head.setNextNode(node) ; node.setNextNode(null) ; node.setPriorNode(this.head); this.size++ ; }else if( pos>this.size ){ // pos位置大于链表长度,插入末端 insertNode(node) ; }else if( pos>0 && pos<=this.size ){ // 链表内节点 // 1、找到要插入位置pos节点,插入pos节点当前位置 int find = 0; while( find this.size || current==null ){ System.out.println("位置信息错误或链表不存在"); }else{ // 1、找到要删除位置pos节点 int find = 0; while( find map) { boolean flag = false ; DNode node = getNode(pos, map); if( node!=null ){ String data = (String) map.get("data") ; node.setData(data); flag = true ; } return flag; } /* * 找到指定链表的节点pos,从1开始 * */ @Override public DNode getNode(int pos, Map map) { DNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==null ){ System.out.println("位置信息错误或链表不存在"); return null; } int find = 0 ; while( find map = new HashMap<>() ; map.put("data", "this is a test") ; dll.updateNode(pos3, map) ; dll.printLink(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
更多Java 数据结构链表操作实现代码相关文章请关注PHP中文网!










