生活随笔
收集整理的這篇文章主要介紹了
Java版单链表讲解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
鏈表介紹
鏈表是有序的列表,但是它在內(nèi)存中是存儲如下 1.鏈表是以節(jié)點(diǎn)的方式來存儲,是鏈?zhǔn)酱鎯?br /> 2.每個(gè)節(jié)點(diǎn)包含 data 域, next 域:指向下一個(gè)節(jié)點(diǎn). 3.如圖:發(fā)現(xiàn)鏈表的各個(gè)節(jié)點(diǎn)不一定是連續(xù)存儲. 4.鏈表分帶頭節(jié)點(diǎn)的鏈表和沒有頭節(jié)點(diǎn)的鏈表,根據(jù)實(shí)際的需求來確定
單鏈表介紹
單鏈表(帶頭節(jié)點(diǎn))邏輯結(jié)構(gòu) 示意圖如下
單鏈表應(yīng)用實(shí)例
使用帶head頭的單向鏈表實(shí)現(xiàn) –漫威英雄排行榜管理 完成對英雄人物的增刪改查操作, 注: 刪除和修改,查找
第一種方法在添加英雄時(shí),直接添加到鏈表的尾部 第二種方式在添加英雄時(shí),根據(jù)排名將英雄插入到指定位置 (如果有這個(gè)排名,則添加失敗,并給出提示)
package linkedlist
; public class SingleLinkedListDemo { public static void main ( String
[ ] args
) { HeroNode heroNode1
= new HeroNode ( 1 , "托尼·史塔克" , "鋼鐵俠" ) ; HeroNode heroNode2
= new HeroNode ( 2 , "班納博士" , "綠巨人" ) ; HeroNode heroNode3
= new HeroNode ( 3 , "斯嘉麗" , "黑寡婦" ) ; HeroNode heroNode4
= new HeroNode ( 4 , "帕克" , "蜘蛛俠" ) ; HeroNode heroNode5
= new HeroNode ( 5 , "洛基" , "神" ) ; SingleLinkedList singleLinkedList
= new SingleLinkedList ( ) ; singleLinkedList
. addByOrder ( heroNode5
) ; singleLinkedList
. addByOrder ( heroNode2
) ; singleLinkedList
. addByOrder ( heroNode1
) ; singleLinkedList
. addByOrder ( heroNode4
) ; singleLinkedList
. addByOrder ( heroNode3
) ; singleLinkedList
. addByOrder ( heroNode3
) ;
HeroNode newHeroNode
= new HeroNode ( 2 , "baihaibai" , "AHAHAHA" ) ; singleLinkedList
. update ( newHeroNode
) ; singleLinkedList
. list ( ) ; System
. out
. println ( ) ; singleLinkedList
. del ( 1 ) ; singleLinkedList
. del ( 3 ) ; singleLinkedList
. list ( ) ; }
}
class SingleLinkedList { private HeroNode head
= new HeroNode ( 0 , "" , "" ) ; public void add ( HeroNode heroNode
) { HeroNode temp
= head
; while ( true ) { if ( temp
. next
== null
) { break ; } temp
= temp
. next
; } temp
. next
= heroNode
; } public void list ( ) { 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
; } } public void addByOrder ( HeroNode heroNode
) { HeroNode temp
= head
; boolean flag
= false ; while ( true ) { if ( temp
. next
== null
) { break ; } if ( temp
. next
. no
> heroNode
. no
) { break ; } else if ( temp
. next
. no
== heroNode
. no
) { flag
= true ; break ; } temp
= temp
. next
; } if ( flag
) { System
. out
. println ( "準(zhǔn)備插入的英雄編號已經(jīng)存在" + heroNode
. no
) ; } else { heroNode
. next
= temp
. next
; temp
. next
= heroNode
; } } 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é)點(diǎn),不能修改" ) ; } } 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
; } else { System
. out
. println ( "要?jiǎng)h除的節(jié)點(diǎn)" + no
+ "不存在" ) ; } } }
class HeroNode { public int no
; public String name
; public String nickname
; public HeroNode next
; public HeroNode ( int no
, String name
, String nickname
) { super ( ) ; this . no
= no
; this . name
= name
; this . nickname
= nickname
; } @Override public String
toString ( ) { return "HeroNode [no=" + no
+ ", name=" + name
+ ", nickname=" + nickname
+ "]" ; } }
新人創(chuàng)作打卡挑戰(zhàn)賽 發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!
總結(jié)
以上是生活随笔 為你收集整理的Java版单链表讲解 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。