主要分析示例:一、单链表循环链表二、双链表循环链表 其中单链表节点和双链表节点类和接口ICommOperate<T>与上篇一致,这里不在赘述。参考:JAVA链表操作:单链表和双链表http://www.cnblogs.com/xiaoxing/p/5969133. ...
主要分析示例:
一、单链表循环链表
二、双链表循环链表
其中单链表节点和双链表节点类和接口ICommOperate<T>与上篇一致,这里不在赘述。参考:JAVA链表操作:单链表和双链表http://www.cnblogs.com/xiaoxing/p/5969133.html
一、单链表循环链表
package LinkListTest;import java.util.HashMap;import java.util.Map;public class SingleCycleLinkList implements ICommOperate<SNode> { private SNode head = new SNode("HEAD") ; // 公共头指针,声明之后不变 private int size = 0 ; public int getSize() { return this.size; } /* * 链表插入,每次往末端插入,判定末端的标准为next是否指向head * */ @Override public boolean insertNode(SNode node) { boolean flag = false ; initLinkList() ; // 初始化链表 if( this.size==0 ){ // 空链表 this.head.setNextNode(node) ; node.setNextNode(this.head) ; }else{ SNode current = this.head ; while( current.getNextNode()!=this.head ){ // 找到末端节点 current = current.getNextNode() ; } current.setNextNode(node) ; node.setNextNode(this.head) ; // 循坏链表,尾节点指向head } 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() ; initLinkList() ;// 初始化链表 if( this.size==0 ){ // 链表为空 this.head.setNextNode(node) ; node.setNextNode(this.head) ;// 循坏链表,尾节点指向head this.size++ ; }else if( this.size<pos ){ // pos位置大于链表长度,插入末端 insertNode(node) ; }else if( pos>0 && pos<=this.size ){ // 链表内节点 // 1、找到要插入pos位置节点和前节点,node将插入两个节点之间 int find = 0; SNode preNode = this.head; // 前节点 SNode currentNode = current; // 当前节点 while( find<pos-1 && currentNode!=this.head ){ preNode = current ; // 前节点后移 currentNode = currentNode.getNextNode() ; // 当前节点后移 find++ ; if( find<pos-1 && currentNode!=this.head ){ // 未结束寻找节点前,后移前节点 current = current.getNextNode() ; } }// System.out.println(preNode);// System.out.println(currentNode); // 2、插入节点 preNode.setNextNode(node); node.setNextNode(currentNode); this.size++ ; }else { System.out.println("位置信息错误"); flag = false ; } return flag; } private void initLinkList(){ if( size==0 ){ this.head.setNextNode(this.head); } } /* * 指定链表的节点pos,删除对应节点。方式:找到要删除节点的前后节点,进行删除,下标从1开始 * */ @Override public boolean deleteNode(int pos) { boolean flag = false; SNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==this.head ){ System.out.println("位置信息错误或链表无信息"); }else{ // 1、找到要删除节点的前后节点 int find = 0; SNode preNode = this.head; // 前节点 SNode nextNode = current.getNextNode(); // 后节点 while( find<pos-1 && nextNode!=this.head ){ preNode = current ; // 前节点后移 nextNode = nextNode.getNextNode() ; // 后节点后移 find++ ; if( find<pos-1 && nextNode!=this.head ){ // 未结束找节点前,后移"前节点" current = current.getNextNode() ; } }// System.out.println(preNode);// System.out.println(nextNode); // 2、删除节点 preNode.setNextNode(nextNode); System.gc(); // 回收删除节点 this.size-- ; flag = true ; } return flag; } /* * 指定链表的节点pos,修改对应节点,下标从1开始 * */ @Override public boolean updateNode(int pos, Map<String, Object> 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<String, Object> map) { SNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==this.head ){ System.out.println("位置信息错误或链表不存在"); return null; } int find = 0 ; while( find<pos-1 && current!=this.head ){ current = current.getNextNode() ; find++ ; } return current; } /* * 打印链表 * */ @Override public void printLink() { int length = this.size ; if( length==0 ){ System.out.println("链表为空!"); return ; } SNode current = this.head.getNextNode() ; System.out.println("总共有节点数: " + length +" 个"); int find = 0 ; while( current!=this.head ){ System.out.println("第 " + (++find) + " 个节点 :" + current); current=current.getNextNode() ; } } public static void main(String[] args) { SingleCycleLinkList scll = new SingleCycleLinkList() ; SNode node1 = new SNode("节点1"); SNode node2 = new SNode("节点2"); SNode node3 = new SNode("节点3"); SNode node4 = new SNode("节点4"); SNode node5 = new SNode("节点5"); SNode node6 = new SNode("插入指定位置");// scll.insertPosNode(scll.getSize()+1, node1) ;// scll.insertPosNode(scll.getSize()+1, node2) ;// scll.insertPosNode(scll.getSize()+1, node3) ;// scll.insertPosNode(scll.getSize()+1, node4) ;// scll.insertPosNode(scll.getSize()+1, node5) ; scll.insertNode(node1); scll.insertNode(node2); scll.insertNode(node3); scll.insertNode(node4); scll.insertNode(node5); System.out.println("*******************输出链表*******************"); scll.printLink(); System.out.println("*******************获得指定链表节点*******************"); int pos = 2 ; System.out.println("获取链表第 "+pos+" 个位置数据 :"+scll.getNode(pos, null)); System.out.println("*******************向链表指定位置插入节点*******************"); int pos1 = 3 ; System.out.println("将数据插入第"+pos1+"个节点:"); scll.insertPosNode(pos1, node6) ; scll.printLink(); System.out.println("*******************删除链表指定位置节点*******************"); int pos2 = 3 ; System.out.println("删除第"+pos2+"个节点:"); scll.deleteNode(pos2) ; scll.printLink(); System.out.println("*******************修改链表指定位置节点*******************"); int pos3 = 3 ; System.out.println("修改第"+pos3+"个节点:"); Map<String, Object> map = new HashMap<>() ; map.put("data", "this is a test") ; scll.updateNode(pos3, map) ; scll.printLink(); }}
原标题:JAVA 链表操作:循环链表
关键词:JAVA
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。