数据结构--环形链表
生活随笔
收集整理的這篇文章主要介紹了
数据结构--环形链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
環形鏈表的一種Go語言實現
package mainimport "fmt"//定義一個環形鏈表的節點結構體 type circleSingleLink struct {id intname stringnext *circleSingleLink }//插入節點到環形鏈表 func Insert(head, newnode *circleSingleLink) {//判斷是否為空鏈表if head.next == nil{//如果為空則把添加的第一個元素給頭節點,這里和其他鏈表有些區別,其他鏈表頭結點是空的head.id = newnode.idhead.name = newnode.name//重點是要讓一個節點也形成一個環形,即收尾相接head.next = headreturn}//添加非頭結點到環形鏈表temp := headfor{if temp.next == head {//尾部添加temp.next = newnodenewnode.next = headbreak}temp = temp.next} }//顯示環形鏈表的所有節點信息 func CircleList(head *circleSingleLink){//判斷鏈表是否為空if head.next == nil {fmt.Println("鏈表為空")return}temp := headfor {fmt.Printf("[%d %s] -> ", temp.id, temp.name)//注意這里是這樣判斷終止條件的if temp.next == head{break}temp = temp.next} }//刪除環形鏈表的一個節點(難點) func CircleDelete(head, node *circleSingleLink) *circleSingleLink { //之所以有返回值是因為頭結點的值和指向要發生變化//刪除思路://先讓temp 指向head//再讓helper指向環形鏈表的最后//讓temp和要刪除的節點比較,如果相同,則讓helper完成刪除節點,重點是要考慮是否為頭結點,因為環形鏈表的頭結點有值temp := headif temp.next == nil { //鏈表為空的時候fmt.Println("鏈表為空")return head}if temp.next == head { //只有一個頭結點的環形鏈表temp.next = nilreturn head}helper := headfor {if helper.next == head {break}helper = helper.next //將指針定位到環形鏈表的尾節點}//如果有兩個及以上的節點for {if temp.next == head {//說明到最后一個并且還沒判斷是否是要刪除的節點if temp.id == node.id {helper.next = temp.next}else {fmt.Println("沒有該節點")}break}if temp.id == node.id { //找到了要刪除的節點if temp == head { //如果刪除的是頭結點head = head.next}//刪除非頭結點helper.next = temp.next //helper始終在temp的后一個break}temp = temp.next //用作比較helper = helper.next //用作操作}return head }func main(){//定義一個鏈表的頭head := &circleSingleLink{}//定義第一個節點node1 := &circleSingleLink{id: 1,name : "number1",}node2 := &circleSingleLink{id: 2,name : "number2",}node3 := &circleSingleLink{id: 3,name : "number3",}Insert(head, node1)Insert(head, node2)Insert(head, node3)head = CircleDelete(head, node1)CircleList(head) }約瑟夫問題:
package mainimport "fmt"type Boy struct {id intnext *Boy }//創建一個環形鏈表,并返回頭指針 func CreateLink(num int) *Boy {first := &Boy{} //頭指針不能動,因此需要一個輔助指針進行循環創建curBoy := &Boy{}if num < 1 {return first}for i := 1; i <= num; i++ {boy := &Boy{id: i,next: nil,}if i == 1 { //因為第一個小孩比較特殊first = boycurBoy = boycurBoy.next = first}else {curBoy.next = boycurBoy = boycurBoy.next = first //構成環形鏈表}}return first }//顯示環形鏈表 func ShowBoy (first *Boy) {if first.next == nil {fmt.Println("鏈表為空")return}curBoy := firstfor {fmt.Printf("小孩%d -> ", curBoy.id)if curBoy.next == first {break}curBoy = curBoy.next} }//使用環形鏈表實現約瑟夫問題 func PlayGame(first *Boy, startNo int, countNum int) {//空鏈表的情況if first.next == nil {fmt.Println("鏈表為空,游戲結束")return}//定義兩個指針循環,其中first負責比較,tail負責刪除tail := first//將tail指正定位到鏈表的最后for {if tail.next == first {break}tail = tail.next}//開始移動startNo-1步for i := 0; i < startNo -1; i++ {first = first.nexttail = tail.next}fmt.Println()//開始循環刪除環形鏈表中第countNum-1個節點for {for i := 0; i < countNum - 1; i++ {first = first.nexttail = tail.next}//打印即將出圈的節點信息fmt.Printf("編號為%d的節點出圈\n", first.id)//刪除此時first指向的當前節點first = first.nexttail.next = first//確定結束條件if tail == first {break}}//打印最后一個出圈的節點fmt.Printf("編號為%d的節點出圈\n", first.id) }func main(){first := CreateLink(51)ShowBoy(first)PlayGame(first, 2, 3) }總結
以上是生活随笔為你收集整理的数据结构--环形链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构--队列(链表实现)
- 下一篇: 数据结构--选择排序