Java實現(xiàn)單鏈表基礎(chǔ)操作
關(guān)于鏈表
鏈表是有序的列表鏈表是以節(jié)點的方式來存儲每個節(jié)點包含data域,next域(指向下一個節(jié)點)分帶頭節(jié)點的鏈表和沒有頭節(jié)點的鏈表
定義一個節(jié)點:
package linkedQueue; public class HeroNode { public int no; public String name; public String nickname; public HeroNode next;//指向下一個節(jié)點 public HeroNode(int no, String name, String nickname) { this.no = no; this.name = name; this.nickname = nickname; } @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nickname='" + nickname + '\'' + '}'; } }
定義一個鏈表:
實現(xiàn)以下功能:
添加節(jié)點
按序添加節(jié)點
刪除節(jié)點
修改節(jié)點
遍歷節(jié)點
反向打印節(jié)點
鏈表反轉(zhuǎn)
統(tǒng)計節(jié)點個數(shù)
打印倒數(shù)第n個節(jié)點
程序:
package linkedQueue; import java.util.Stack; public class SingleLinkedList { private HeroNode head = new HeroNode(0, "", ""); public HeroNode getHead() { return head; } //反向打印節(jié)點 public static void reversePrint(HeroNode head) { if (head.next == null) { return; } // 創(chuàng)建一個棧 Stack<HeroNode> heroNodes = new Stack<>(); HeroNode cur = head.next; while (cur != null) { heroNodes.push(cur); //入棧 cur = cur.next; } // 出棧打印 while (heroNodes.size() > 0) { System.out.println(heroNodes.pop()); } } /** * 添加節(jié)點 * @param heroNode */ public void add(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; } /** * 有序添加節(jié)點 * @param herNode */ public void addByOrder(HeroNode herNode) { HeroNode temp = head; boolean flag = false; while (true) { if (temp.next == null) { break; } if (temp.next.no > herNode.no) { break; } else if (temp.next.no==herNode.no) { flag = true; break; } temp = temp.next; } if (flag) { System.out.println("你要插入的節(jié)點的編號已經(jīng)存在?。。?!"); } else { herNode.next = temp.next; temp.next = herNode; } } /** * 更新節(jié)點數(shù)據(jù) * @param newHeroNode */ public void update(HeroNode newHeroNode) { if (head.next == null) { System.out.println("鏈表為空!?。?!"); return; } HeroNode temp = head.next; boolean flag = false; while (true) { if (temp == null) { break; } if (temp.no == newHeroNode.no) { // 找到 flag = true; break; } temp = temp.next; } if (flag) { temp.name = newHeroNode.name; temp.nickname = newHeroNode.nickname; } else { // 沒有找到 System.out.println("沒有找到編號" + newHeroNode.no + "的節(jié)點,不能修改"); } } /** * 刪除節(jié)點 * @param no */ public void del(int no) { HeroNode temp = head; boolean flag = false; while (true) { if (temp.next == null) { break; } if (temp.next.no == no) { flag = true; break; } temp=temp.next; } if (flag) { temp.next = temp.next.next; } } /** * 遍歷節(jié)點 */ public void showList() { if (head.next == null) { System.out.println("鏈表為空"); return; } HeroNode temp = head.next; while (true) { if (temp == null) { break; } System.out.println(temp); temp = temp.next; } } /** * 統(tǒng)計有效節(jié)點的個數(shù) */ public static int getLength(HeroNode head) { if (head.next == null) { //空鏈表 return 0; } int length = 0; HeroNode herNode = head.next; while (herNode != null) { length++; // 計數(shù) herNode=herNode.next; // 指針后移 } return length; } /** * 求倒數(shù)第index個節(jié)點 * @param head 頭節(jié)點 * @param index 倒數(shù)第k個 * @return */ public static HeroNode findLastIndexNode(HeroNode head, int index) { // 判斷是否是空鏈表 if (head.next == null) { return null; } // 拿到鏈表的長度 int size = getLength(head); // index校驗,看是否在范圍內(nèi) if (index <= 0 || index > size) { return null; } // 定位倒數(shù)第index個節(jié)點 HeroNode herNode = head.next; for (int i = 0; i < size - index; i++) { herNode = herNode.next; } return herNode; } //單鏈表反轉(zhuǎn) public static void reverseList(HeroNode head) { // 如果當(dāng)前鏈表為空或者只有一個節(jié)點,直接返回 if (head.next == null || head.next.next == null) { return; } // 定義輔助變量來遍歷鏈表 HeroNode cur = head.next; HeroNode next = null;//指向當(dāng)前節(jié)點[cur]的下一個節(jié)點 HeroNode reverseHead = new HeroNode(0, "", ""); // 遍歷原來節(jié)點,每遍歷一個節(jié)點,將其取出,并放在新的鏈表的最前端 while (cur != null) { next = cur.next;//暫時保存當(dāng)前節(jié)點的下一個節(jié)點 cur.next = reverseHead.next;//將cur的下一個節(jié)點指向新的鏈表的最前端 reverseHead.next=cur;//將cur鏈接到新的鏈表 cur = next; } head.next = reverseHead.next; } }
到此這篇關(guān)于Java實現(xiàn)單鏈表基礎(chǔ)操作的文章就介紹到這了,更多相關(guān)Java單鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+jwt實現(xiàn)token登陸權(quán)限認(rèn)證的實現(xiàn)
這篇文章主要介紹了springboot+jwt實現(xiàn)token登陸權(quán)限認(rèn)證的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解
在這篇文章中,小編將帶大家了解一下Java數(shù)據(jù)結(jié)構(gòu)中鏈表的增刪查改(以下結(jié)果均在IDEA中編譯)希望在方便自己復(fù)習(xí)的同時也能幫助到大家2022-09-09SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處
這篇文章主要介紹了SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java 十進(jìn)制轉(zhuǎn)二、八、十六進(jìn)制的字符串
本文主要介紹了十進(jìn)制轉(zhuǎn)二進(jìn)制;十進(jìn)制轉(zhuǎn)八進(jìn)制;十進(jìn)制轉(zhuǎn)十六進(jìn)制的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02