Java數(shù)據(jù)結(jié)構(gòu)之雙端鏈表原理與實(shí)現(xiàn)方法
本文實(shí)例講述了Java數(shù)據(jù)結(jié)構(gòu)之雙端鏈表原理與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一、概述:
1、什么時(shí)雙端鏈表:
鏈表中保持這對(duì)最后一個(gè)連點(diǎn)引用的鏈表
2、從頭部插入
要對(duì)鏈表進(jìn)行判斷,如果為空則設(shè)置尾節(jié)點(diǎn)為新添加的節(jié)點(diǎn)
3、從尾部進(jìn)行插入
如果鏈表為空,則直接設(shè)置頭節(jié)點(diǎn)為新添加的節(jié)點(diǎn),否則設(shè)置尾節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)為新添加的節(jié)點(diǎn)
4、從頭部刪除
判斷節(jié)點(diǎn)是否有下個(gè)節(jié)點(diǎn),如果沒有則設(shè)置節(jié)點(diǎn)為null
二、具體實(shí)現(xiàn)
/** * @描述 頭尾相接的鏈表 * @項(xiàng)目名稱 Java_DataStruct * @包名 com.struct.linklist * @類名 LinkList * @author chenlin * @date 2010年6月26日 上午8:00:28 * @version 1.0 */ public class FirstLastLinkList { //頭 private Node first; //尾 private Node last; public FirstLastLinkList(){ first = null; last = null; } /** * 插入數(shù)據(jù) * @param value */ public void insertFirst(long value){ Node newNode = new Node(value); if (first == null) { last = newNode; }else { //把first節(jié)點(diǎn)往下移動(dòng) newNode.next = first; } //把插入的節(jié)點(diǎn)作為新的節(jié)點(diǎn) first = newNode; } /** * 插入數(shù)據(jù) * @param value */ public void insertLast(long value){ Node newNode = new Node(value); if (first == null) { first = newNode; }else { last.next = newNode; } //把最后個(gè)節(jié)點(diǎn)設(shè)置為最新的節(jié)點(diǎn) last = newNode; } public boolean isEmpty(){ return first == null; } /** * 刪除頭節(jié)點(diǎn) * @param value * @return */ public Node deleteFirst(){ if (first == null) { throw new RuntimeException("鏈表數(shù)據(jù)不存在"); } if (first.next == null) { last = null; } Node temp = first; first = temp.next; return temp; } public Node deleteByKey(long key){ Node current = first; Node last = first; while(current.data != key){ if (current.next == null) { System.out.println("沒找到節(jié)點(diǎn)"); return null; } last = current; current = current.next; } if (current == first) { //return deleteFirst(); //指向下個(gè)就表示刪除第一個(gè) first = first.next; }else { last.next = current.next; } return current; } /** * 顯示所有的數(shù)據(jù) */ public void display(){ if (first == null) { //throw new RuntimeException("鏈表數(shù)據(jù)不存在"); return; } Node current = first; while(current != null){ current.display(); current = current.next; } System.out.println("---------------"); } /** * 查找節(jié)點(diǎn)1 * @param value * @return */ public Node findByValue(long value){ Node current = first; while(current != null){ if (current.data != value) { current = current.next; }else { break; } } if (current == null) { System.out.println("沒找到"); return null; } return current; } /** * 查找節(jié)點(diǎn)2 * * @param key * @return */ public Node findByKey(long key) { Node current = first; while (current.data != key) { if (current.next == null) { System.out.println("沒找到"); return null; } current = current.next; } return current; } /** * 根據(jù)索引查找對(duì)應(yīng)的值 * @param position * @return */ public Node findByPosition(int position){ Node current = first; //為什么是position - 1,因?yàn)橐褂帽闅v,讓current指向下一個(gè), 所以position - 1的下個(gè)node就是要找的值 for (int i = 0; i < position - 1 ; i++) { current = current.next; } return current; } public static void main(String[] args) { FirstLastLinkList linkList = new FirstLastLinkList(); linkList.insertFirst(21); linkList.insertFirst(22); linkList.insertFirst(23); linkList.insertLast(24); linkList.insertLast(25); linkList.insertLast(26); linkList.insertLast(27); linkList.display(); System.out.println("---查找-------------------------------------"); linkList.findByKey(25).display(); System.out.println("--刪除first-------------------------------------"); //linkList.deleteFirst().display(); ///linkList.deleteFirst().display(); //linkList.deleteFirst().display(); //linkList.deleteFirst().display(); System.out.println("-刪除指定值---------------------------------------"); linkList.deleteByKey(27).display(); linkList.deleteByKey(21).display(); System.out.println("----------------------------------------"); linkList.display(); } }
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
java中如何判斷JSONObject是否存在某個(gè)Key
這篇文章主要介紹了java中如何判斷JSONObject是否存在某個(gè)Key,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07SpringBoot發(fā)送郵件功能 驗(yàn)證碼5分鐘過期
這篇文章主要為大家詳細(xì)介紹了SpringBoot發(fā)送郵件功能,驗(yàn)證碼5分鐘過期,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03手把手教學(xué)Win10同時(shí)安裝兩個(gè)版本的JDK并隨時(shí)切換(JDK8和JDK11)
最近在學(xué)習(xí)JDK11的一些新特性,但是日常使用基本上都是基于JDK8,因此,需要在win環(huán)境下安裝多個(gè)版本的JDK,下面這篇文章主要給大家介紹了手把手教學(xué)Win10同時(shí)安裝兩個(gè)版本的JDK(JDK8和JDK11)并隨時(shí)切換的相關(guān)資料,需要的朋友可以參考下2023-03-03Mybatis plus中使用in查詢出錯(cuò)如何解決
這篇文章主要介紹了Mybatis plus中使用in查詢出錯(cuò)的問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Spring中Controller和RestController的區(qū)別詳解
這篇文章主要介紹了Spring中Controller和RestController的區(qū)別詳解,@Controller是標(biāo)識(shí)一個(gè)Spring類是Spring MVC controller處理器,@Controller類中的方法可以直接通過返回String跳轉(zhuǎn)到j(luò)sp、ftl、html等模版頁面,需要的朋友可以參考下2023-09-09Servlet和Filter之間的區(qū)別與聯(lián)系
這篇文章主要介紹了Servlet和Filter之間的區(qū)別與聯(lián)系的相關(guān)資料,需要的朋友可以參考下2016-05-05Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用
這篇文章主要介紹了Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12