Java 數(shù)據(jù)結(jié)構(gòu)鏈表操作實現(xiàn)代碼
鏈表是一種復(fù)雜的數(shù)據(jù)結(jié)構(gòu),其數(shù)據(jù)之間的相互關(guān)系使鏈表分成三種:單鏈表、循環(huán)鏈表、雙向鏈表,下面將逐一介紹。鏈表在數(shù)據(jù)結(jié)構(gòu)中是基礎(chǔ),也是重要的知識點,這里講下Java 中鏈表的實現(xiàn),
JAVA 鏈表操作:單鏈表和雙鏈表
主要講述幾點:
一、鏈表的簡介
二、鏈表實現(xiàn)原理和必要性
三、單鏈表示例
四、雙鏈表示例
一、鏈表的簡介
鏈表是一種比較常用的數(shù)據(jù)結(jié)構(gòu),鏈表雖然保存比較復(fù)雜,但是在查詢時候比較便捷,在多種計算機語言都相應(yīng)的應(yīng)用,鏈表有多種類別,文章針對單鏈表和雙鏈表進行分析。鏈表中數(shù)據(jù)就像被一個鏈條串聯(lián)一起,輕易的可以實現(xiàn)數(shù)據(jù)的訪問。
二、鏈表實現(xiàn)原理和必要性
這里只分析單鏈表和雙鏈表。鏈表的實現(xiàn)過程是有些許復(fù)雜的,但是會帶來許多好處。比如現(xiàn)在網(wǎng)購時代到來,商家發(fā)快遞一般會將商品包裝在盒子里并寫上地址信息,快遞公司就可以通過盒子上的信息找到買家,商品完整到達。如果沒有盒子的保護,有可能在途中商品受損。而鏈表就好比那個寫了地址信息的盒子,既保護了商品信息,同時又寫好了物流信息。鏈表之中存在一個HEAD節(jié)點,類似“火車頭”,只要找到相應(yīng)HEAD節(jié)點,就可以對鏈表進行操作。此次分析中,HEAD節(jié)點只是做數(shù)據(jù)頭,不保存有效數(shù)據(jù)。
單鏈表的實現(xiàn)原理如圖:
雙鏈表實現(xiàn)原理:
三、單鏈表示例
ICommOperate<T> 接口操作類:
package LinkListTest; import java.util.Map; public interface ICommOperate<T> { public boolean insertNode(T node) ; public boolean insertPosNode(int pos, T node) ; public boolean deleteNode(int pos) ; public boolean updateNode(int pos, Map<String, Object> map) ; public T getNode(int pos, Map<String, Object> map) ; public void printLink() ; }
單鏈表節(jié)點:
package LinkListTest; // 單連表節(jié)點 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<SNode>{ 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{ // 鏈表內(nèi)節(jié)點 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<pos ){ // pos位置大于鏈表長度,插入末端 insertNode(node) ; }else if( pos>0 && pos<=this.size) { // 鏈表內(nèi)節(jié)點 // 1、找到要插入pos位置節(jié)點和前節(jié)點 int find = 0; SNode preNode = this.head; // 前節(jié)點 SNode currentNode = current; // 當前節(jié)點 while( find<pos-1 && currentNode.getNextNode()!=null ){ preNode = current ; // 前節(jié)點后移 currentNode = currentNode.getNextNode() ; // 當前節(jié)點后移 find++ ; } // System.out.println(preNode); // System.out.println(currentNode); // 2、插入節(jié)點 preNode.setNextNode(node); node.setNextNode(currentNode); this.size++ ; System.out.println("節(jié)點已經(jīng)插入鏈表中"); }else{ System.out.println("位置信息錯誤"); flag = false ; } return flag; } /* * 指定鏈表的節(jié)點pos,刪除對應(yīng)節(jié)點。方式:找到要刪除節(jié)點的前后節(jié)點,進行刪除。從1開始 * */ @Override public boolean deleteNode(int pos) { boolean flag = false; SNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==null ){ System.out.println("位置信息錯誤或鏈表無信息"); }else{ // 1、找到要刪除節(jié)點的前后節(jié)點 int find = 0; SNode preNode = this.head; // 前節(jié)點 SNode nextNode = current.getNextNode(); // 后節(jié)點 while( find<pos-1 && nextNode.getNextNode()!=null ){ preNode = current ; // 前節(jié)點后移 nextNode = nextNode.getNextNode() ; // 后節(jié)點后移 find++ ; } // System.out.println(preNode); // System.out.println(nextNode); // 2、刪除節(jié)點 preNode.setNextNode(nextNode); System.gc(); this.size-- ; flag = true ; } return flag; } /* * 指定鏈表的節(jié)點pos,修改對應(yīng)節(jié)點。 從1開始 * */ @Override public boolean updateNode(int pos, Map<String, Object> map) { boolean flag = false ; SNode node = getNode(pos, map); // 獲得相應(yīng)位置pos的節(jié)點 if( node!=null ){ String data = (String) map.get("data") ; node.setData(data); flag = true ; } return flag; } /* * 找到指定鏈表的節(jié)點pos,從1開始 * */ @Override public SNode getNode(int pos, Map<String, Object> map) { SNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==null ){ System.out.println("位置信息錯誤或鏈表不存在"); return null; } int find = 0 ; while( find<pos-1 && current!=null ){ 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() ; int find = 0 ; System.out.println("總共有節(jié)點數(shù): " + length +" 個"); while( current!=null ){ System.out.println("第 " + (++find) + " 個節(jié)點 :" + current); current=current.getNextNode() ; } } public static void main(String[] args) { SingleLinkList sll = new SingleLinkList() ; SNode node1 = new SNode("節(jié)點1"); SNode node2 = new SNode("節(jié)點2"); SNode node3 = new SNode("節(jié)點3"); SNode node4 = new SNode("節(jié)點4"); SNode node5 = new SNode("節(jié)點5"); SNode node6 = new SNode("插入指定位置"); sll.insertPosNode(sll.getSize()+1, node1) ; sll.insertPosNode(sll.getSize()+1, node2) ; sll.insertPosNode(sll.getSize()+1, node3) ; sll.insertPosNode(sll.getSize()+1, node4) ; sll.insertPosNode(sll.getSize()+1, node5) ; // sll.insertNode(node1); // sll.insertNode(node2); // sll.insertNode(node3); // sll.insertNode(node4); // sll.insertNode(node5); System.out.println("*******************輸出鏈表*******************"); sll.printLink(); System.out.println("*******************獲得指定鏈表節(jié)點*******************"); int pos = 2 ; System.out.println("獲取鏈表第 "+pos+" 個位置數(shù)據(jù) :"+sll.getNode(pos, null)); System.out.println("*******************向鏈表指定位置插入節(jié)點*******************"); int pos1 = 2 ; System.out.println("將數(shù)據(jù)插入第 "+pos1+" 個節(jié)點:"); sll.insertPosNode(pos1, node6) ; sll.printLink(); System.out.println("*******************刪除鏈表指定位置節(jié)點*******************"); int pos2 = 2 ; System.out.println("刪除第 "+pos2+" 個節(jié)點:"); sll.deleteNode(pos2) ; sll.printLink(); System.out.println("*******************修改鏈表指定位置節(jié)點*******************"); int pos3 = 2 ; System.out.println("修改第 "+pos3+" 個節(jié)點:"); Map<String, Object> map = new HashMap<>() ; map.put("data", "this is a test") ; sll.updateNode(pos3, map) ; sll.printLink(); } }
四、雙鏈表示例
ICommOperate<T> 接口操作類:
package LinkListTest; import java.util.Map; public interface ICommOperate<T> { public boolean insertNode(T node) ; public boolean insertPosNode(int pos, T node) ; public boolean deleteNode(int pos) ; public boolean updateNode(int pos, Map<String, Object> map) ; public T getNode(int pos, Map<String, Object> map) ; public void printLink() ; }
雙鏈表節(jié)點:
package LinkListTest; // 雙連表節(jié)點 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 + "]"; } }
雙鏈表實現(xiàn)類:
package LinkListTest; import java.util.HashMap; import java.util.Map; public class DoubleLinkList implements ICommOperate<DNode>{ 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{ // 鏈表內(nèi)節(jié)點 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 ){ // 鏈表內(nèi)節(jié)點 // 1、找到要插入位置pos節(jié)點,插入pos節(jié)點當前位置 int find = 0; while( find<pos-1 && current.getNextNode()!=null ){ current = current.getNextNode() ; find++ ; } // 2、插入節(jié)點 if( current.getNextNode()==null ){ // 尾節(jié)點 node.setPriorNode(current); node.setNextNode(null); current.setNextNode(node); } else if( current.getNextNode()!=null ) { //中間節(jié)點 node.setPriorNode(current.getPriorNode()); node.setNextNode(current); current.getPriorNode().setNextNode(node); current.setPriorNode(node); } this.size++ ; }else{ System.out.println("位置信息錯誤"); flag = false ; } return flag; } /* * 指定鏈表的節(jié)點pos,刪除對應(yīng)節(jié)點,從1開始 * */ @Override public boolean deleteNode(int pos) { boolean flag = false; DNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==null ){ System.out.println("位置信息錯誤或鏈表不存在"); }else{ // 1、找到要刪除位置pos節(jié)點 int find = 0; while( find<pos-1 && current.getNextNode()!=null ){ current = current.getNextNode() ; find++ ; } // 2、刪除節(jié)點 if( current.getNextNode()==null ){ // 尾節(jié)點 current.getPriorNode().setNextNode(null) ; } else if( current.getNextNode()!=null ) { //中間節(jié)點 current.getPriorNode().setNextNode(current.getNextNode()) ; current.getNextNode().setPriorNode(current.getPriorNode()) ; } System.gc(); this.size-- ; flag = true ; } return flag; } /* * 指定鏈表的節(jié)點pos,修改對應(yīng)節(jié)點。 從1開始 * */ @Override public boolean updateNode(int pos, Map<String, Object> 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; } /* * 找到指定鏈表的節(jié)點pos,從1開始 * */ @Override public DNode getNode(int pos, Map<String, Object> map) { DNode current = this.head.getNextNode() ; if( pos<=0 || pos>this.size || current==null ){ System.out.println("位置信息錯誤或鏈表不存在"); return null; } int find = 0 ; while( find<pos-1 && current!=null ){ current = current.getNextNode() ; find++ ; } return current; } /* * 打印鏈表 * */ @Override public void printLink() { int length = this.size ; if( length==0 ){ System.out.println("鏈表為空!"); return ; } DNode current = this.head.getNextNode() ; int find = 0 ; System.out.println("總共有節(jié)點數(shù): " + length +" 個"); while( current!=null ){ System.out.println("第 " + (++find) + " 個節(jié)點 :" + current); current=current.getNextNode() ; } } public static void main(String[] args) { DoubleLinkList dll = new DoubleLinkList() ; DNode node1 = new DNode("節(jié)點1"); DNode node2 = new DNode("節(jié)點2"); DNode node3 = new DNode("節(jié)點3"); DNode node4 = new DNode("節(jié)點4"); DNode node5 = new DNode("節(jié)點5"); DNode node6 = new DNode("插入指定位置"); dll.insertPosNode(10, node1) ; dll.insertPosNode(10, node2) ; dll.insertPosNode(10, node3) ; dll.insertPosNode(10, node4) ; dll.insertPosNode(10, node5) ; // dll.insertNode(node1); // dll.insertNode(node2); // dll.insertNode(node3); // dll.insertNode(node4); // dll.insertNode(node5); System.out.println("*******************輸出鏈表*******************"); dll.printLink(); System.out.println("*******************獲得指定鏈表節(jié)點*******************"); int pos = 2 ; System.out.println("獲取鏈表第 "+pos+" 個位置數(shù)據(jù) :"+dll.getNode(pos, null)); System.out.println("*******************向鏈表指定位置插入節(jié)點*******************"); int pos1 = 2 ; System.out.println("將數(shù)據(jù)插入第"+pos1+"個節(jié)點:"); dll.insertPosNode(pos1, node6) ; dll.printLink(); System.out.println("*******************刪除鏈表指定位置節(jié)點*******************"); int pos2 = 7 ; System.out.println("刪除第"+pos2+"個節(jié)點:"); dll.deleteNode(pos2) ; dll.printLink(); System.out.println("*******************修改鏈表指定位置節(jié)點*******************"); int pos3 = 2 ; System.out.println("修改第"+pos3+"個節(jié)點:"); Map<String, Object> map = new HashMap<>() ; map.put("data", "this is a test") ; dll.updateNode(pos3, map) ; dll.printLink(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- java數(shù)據(jù)結(jié)構(gòu)之實現(xiàn)雙向鏈表的示例
- java實現(xiàn)數(shù)據(jù)結(jié)構(gòu)單鏈表示例(java單鏈表)
- Java數(shù)據(jù)結(jié)構(gòu)之簡單鏈表的定義與實現(xiàn)方法示例
- Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實例講解
- JAVA 數(shù)據(jù)結(jié)構(gòu)鏈表操作循環(huán)鏈表
- java 數(shù)據(jù)結(jié)構(gòu)之刪除鏈表中的元素實例代碼
- Java模擬有序鏈表數(shù)據(jù)結(jié)構(gòu)的示例
- 詳解java數(shù)據(jù)結(jié)構(gòu)與算法之雙鏈表設(shè)計與實現(xiàn)
- Java描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之鏈表的增刪改查詳解
- Java數(shù)據(jù)結(jié)構(gòu)之鏈表實現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))
相關(guān)文章
Java并發(fā)編程之詳解ConcurrentHashMap類
在之前的文章中已經(jīng)為大家介紹了java并發(fā)編程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue、BlockingDeque接口,本文為系列文章第八篇.需要的朋友可以參考下2021-06-06MybatisPlus3.3.0沒有MybatisPlusInterceptor類問題的解決方法
項目使用的是mybatis-plus-extension3.3.0依賴,然后在我使用分頁插件的時候,發(fā)現(xiàn)無法導(dǎo)入MybatisPlusInterceptor類所以本文給大家介紹了MybatisPlus3.3.0沒有MybatisPlusInterceptor類問題的解決方法,需要的朋友可以參考下2023-12-12Spring?Boot?Actuator?漏洞利用小結(jié)
spring對應(yīng)兩個版本,分別是Spring Boot 2.x和Spring Boot 1.x,因此后面漏洞利用的payload也會有所不同,這篇文章主要介紹了Spring?Boot?Actuator?漏洞利用小結(jié),需要的朋友可以參考下2023-11-11淺談Java代理(jdk靜態(tài)代理、動態(tài)代理和cglib動態(tài)代理)
下面小編就為大家?guī)硪黄獪\談Java代理(jdk靜態(tài)代理、動態(tài)代理和cglib動態(tài)代理)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Future與FutureTask接口實現(xiàn)示例詳解
這篇文章主要為大家介紹了Future與FutureTask接口實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10