数据结构之单项链表的操作
生活随笔
收集整理的這篇文章主要介紹了
数据结构之单项链表的操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以下是自己敲的小demo,方便日后復(fù)習(xí)時(shí)候用.主要實(shí)現(xiàn)了對(duì)有頭結(jié)點(diǎn)的單向鏈表的
增加,刪除,修改,更新以及反轉(zhuǎn)鏈表,倒敘輸出,查找有效節(jié)點(diǎn)數(shù),查找倒數(shù)第K個(gè)節(jié)點(diǎn)的
一些操作,如有冗余,歡迎指正.
package com.ebiz.list;import java.util.Stack;/*** @author YHj* @create 2019-07-14 16:57*/ public class SingleListDemo {public static void main(String[] args) {//創(chuàng)建節(jié)點(diǎn)HeroNode heroNode01 = new HeroNode(1, "宋江", "及時(shí)雨");HeroNode heroNode02 = new HeroNode(2, "盧俊義", "玉麒麟");HeroNode heroNode03 = new HeroNode(3, "無用", "智多星");HeroNode heroNode04 = new HeroNode(4, "林沖", "豹子頭");//創(chuàng)建鏈表SingleList singleList = new SingleList();//添加節(jié)點(diǎn)并按照英雄編號(hào)排序 singleList.addById(heroNode01);singleList.addById(heroNode04);singleList.addById(heroNode02);singleList.addById(heroNode03);//遍歷節(jié)點(diǎn) singleList.list(singleList);System.out.println("修改節(jié)點(diǎn)----------------------------------------------------");//修改節(jié)點(diǎn)singleList.update(new HeroNode(2, "盧俊義111", "玉麒麟111"));//遍歷節(jié)點(diǎn) singleList.list(singleList);//System.out.println("------------------------------------------------------");//刪除節(jié)點(diǎn)//singleList.delete(5);//遍歷節(jié)點(diǎn)//singleList.list(); System.out.println("倒數(shù)第k個(gè)節(jié)點(diǎn)---------------------------------------------------");System.out.println(SingleList.getDaoHeroNode(singleList,4));System.out.println("鏈表倒敘----------------------------------------------------------");singleList.reverseList(singleList);System.out.println("從尾到頭打印單鏈表 不是反轉(zhuǎn),會(huì)破壞數(shù)據(jù)結(jié)構(gòu) 采用stack--------------");singleList.reverseStack(singleList);} }//管理節(jié)點(diǎn)的類 即鏈表 class SingleList {//初始化頭結(jié)點(diǎn)private static HeroNode headhero = new HeroNode(0, "", "");//新增一個(gè)節(jié)點(diǎn)public void add(HeroNode heroNode) {//臨時(shí)節(jié)點(diǎn)HeroNode temp = headhero;//找到最后一個(gè)節(jié)點(diǎn)while (true) {if (temp.next == null) {break;}temp = temp.next;}//while循環(huán)退出,找到最后一個(gè)節(jié)點(diǎn)temp.next = heroNode;}//遍歷節(jié)點(diǎn)public void list(SingleList singleList) {//判斷是否為空if (null == singleList.headhero.next) {System.out.println("鏈表為空");}//臨時(shí)節(jié)點(diǎn)HeroNode temp = headhero.next;while (true) {if (null == temp) {break;}System.out.println("temp = " + temp);temp = temp.next;}}//按照英雄編號(hào)來插入 從小到大public void addById(HeroNode heroNode) {//臨時(shí)節(jié)點(diǎn)HeroNode temp = headhero;//boolean 判斷鏈表中是否已經(jīng)存在boolean isExist = false;while (true) {if (null == temp.next) {break;}if (temp.next.no > heroNode.no) {break;}if (temp.next.no == heroNode.no) {isExist = true;break;}temp = temp.next;}if (isExist) {System.out.println("該英雄已存在!");} else {heroNode.next = temp.next;temp.next = heroNode;}}//修改對(duì)應(yīng)節(jié)點(diǎn)public void update(HeroNode heroNode) {//臨時(shí)節(jié)點(diǎn)HeroNode temp = headhero;//boolean 判斷鏈表中是否存在對(duì)應(yīng)節(jié)點(diǎn)boolean isExist = false;while (true) {if (null == temp) {break;}if (temp.no == heroNode.no) {isExist = true;break;}temp = temp.next;}if (isExist) {temp.name = heroNode.name;temp.nickname = heroNode.nickname;} else {System.out.println("沒有對(duì)應(yīng)英雄!!!");}}//刪除節(jié)點(diǎn)public void delete(int no) {//臨時(shí)節(jié)點(diǎn)HeroNode temp = headhero;//boolean 判斷鏈表中是否存在對(duì)應(yīng)節(jié)點(diǎn)boolean isExist = false;while (true) {if (null == temp) {break;}if (temp.next.no == no) {isExist = true;break;}temp = temp.next;}if (isExist) {temp.next = temp.next.next;} else {System.out.println("沒有該節(jié)點(diǎn)!");}}//獲取節(jié)點(diǎn)個(gè)數(shù),不包含頭結(jié)點(diǎn)public static int getCount(SingleList singleList){if (null == singleList.headhero.next){return 0;}//臨時(shí)節(jié)點(diǎn)HeroNode temp=singleList.headhero.next;int count=0;while (temp != null){count++;temp=temp.next;}return count;}//獲取倒數(shù)第k個(gè)節(jié)點(diǎn)public static HeroNode getDaoHeroNode(SingleList singleList,int k){//判斷是否為空if (null == singleList.headhero.next) {System.out.println("鏈表為空");}//得到鏈表有效節(jié)點(diǎn)int len=getCount(singleList);if (k<0 || k>len){return null;}//倒數(shù)第k個(gè)節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)int s=len-k;if (s ==0 ){return singleList.headhero.next;}//計(jì)數(shù)器,找到第s個(gè)節(jié)點(diǎn)int count =0;HeroNode temp=singleList.headhero.next;while (true){count++;if (count ==s){break;}temp=temp.next;}return temp.next;}//鏈表反轉(zhuǎn)并輸出 頭插法public void reverseList(SingleList singleList){if (0 == this.getCount(singleList) || 1 == this.getCount(singleList)){System.out.println("該鏈表沒有節(jié)點(diǎn)!");return;}//臨時(shí)節(jié)點(diǎn) 遍歷原來的鏈表HeroNode cur=singleList.headhero.next;//臨時(shí)節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)HeroNode cur_next=null;//新鏈表的頭結(jié)點(diǎn)HeroNode newHead = new HeroNode(0, "", "");while (cur != null){cur_next=cur.next; //找到當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)cur.next=newHead.next; //將頭節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)給要插入的節(jié)點(diǎn)newHead.next=cur; //要插入的節(jié)點(diǎn)給頭結(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)cur=cur_next; //將原來當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)在賦值給臨時(shí)節(jié)點(diǎn),因?yàn)楝F(xiàn)在的臨時(shí)節(jié)點(diǎn)在新的頭結(jié)點(diǎn)上 }singleList.headhero.next=newHead.next;this.list(singleList);}//從尾到頭打印單鏈表 不是反轉(zhuǎn),會(huì)破壞數(shù)據(jù)結(jié)構(gòu) 采用stackpublic void reverseStack(SingleList singleList){if (null == singleList.headhero.next){System.out.println("鏈表為空");return;}//定義臨時(shí)節(jié)點(diǎn)HeroNode temp=headhero.next;//定義棧Stack<HeroNode> stack = new Stack<>();while (null != temp){stack.push(temp);temp=temp.next;}while (stack.size() > 0){System.out.println(stack.pop());}}}//節(jié)點(diǎn)類 class HeroNode {public int no;//英雄編號(hào)public String name;//英雄的名字public String nickname;//英雄的名稱public HeroNode next;//下一個(gè)英雄節(jié)點(diǎn)//構(gòu)造器public HeroNode(int no, String name, String nickname) {this.no = no;this.name = name;this.nickname = nickname;}@Overridepublic String toString() {return "HeroNode{" +"no=" + no +", name='" + name + '\'' +", nickname='" + nickname + '\'' +'}';} }?
轉(zhuǎn)載于:https://www.cnblogs.com/jiushixihuandaqingtian/p/11203944.html
總結(jié)
以上是生活随笔為你收集整理的数据结构之单项链表的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线段树 区间加 gcd 差分
- 下一篇: Unity性能优化-遮挡剔除