链表简单实现(增删查改)
生活随笔
收集整理的這篇文章主要介紹了
链表简单实现(增删查改)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈表
關于鏈表的原理已經有一篇鏈表文章寫的很詳細了,這篇文章主要側重于代碼的實現,主要使用go實現。
單鏈表實現
package Listtype listNode struct {val intnext *listNode }func newNode(val int) *listNode {node := new(listNode)node.val = valnode.next = nilreturn node }// NewList 初始化一個不帶頭結點的鏈表 func NewList(vals []int) *listNode {var fistNode *listNodevar curNode *listNodefor _, v := range vals {node := newNode(v)if curNode == nil {fistNode = nodecurNode = fistNodecontinue}curNode.next = nodecurNode = curNode.next}return fistNode }// FistAdd 頭插 func FistAdd(fistNode *listNode, val int) *listNode{if fistNode == nil {return fistNode}node := newNode(val)node.next = fistNodereturn node }// LastAdd 尾插 func LastAdd(fistNode *listNode, val int) {if fistNode == nil {return}curNode := fistNodefor curNode.next != nil {curNode = curNode.next}node := newNode(val)curNode.next = nodereturn }// IndexValAdd 在第一個指定值后插入,若沒有,在鏈表尾部插入 // fistNode 鏈表第一個節點, indexVal 目標節點值, val 新節點值 func IndexValAdd(fistNode *listNode, indexVal, val int) {if fistNode == nil {return}curNode := fistNodefor curNode.next != nil && curNode.val != indexVal {curNode = curNode.next}node := newNode(val)nextNode := curNode.nextnode.next = nextNodecurNode.next = nodereturn }// ChangVal 更改目標節點值,若沒有,不做改動 // fistNode 鏈表第一個節點, indexVal 目標節點值, val 目標值 func ChangVal (fistNode *listNode, indexVal, tarVal int) {if fistNode == nil {return}curNode := fistNodefor curNode != nil && curNode.val != indexVal {curNode = curNode.next}// 判斷是走到最后都沒有找到對應值還是找到對應值if curNode == nil{return}curNode.val = tarValreturn}// DelNode 刪除指定節點,若沒有則不刪除 func DelNode(fistNode *listNode, indexVal int) {if fistNode == nil {return}curNode := fistNode// 查找要刪除節點的前一個節點for curNode.next != nil {nextNode := curNode.nextif nextNode.val == indexVal {break}curNode = curNode.next}if curNode.next == nil {return}nextNode := curNode.nextcurNode.next = nextNode.next}// Show 不帶頭節點查 func Show(node *listNode) []int {if node == nil {return []int{}}valSlice := make([]int, 0, 4)curNode := nodefor curNode != nil {valSlice = append(valSlice, curNode.val)curNode = curNode.next}return valSlice }驗證代碼
package mainimport ("Data/List""fmt" )func main() {// 初始化一個鏈表fistNode := List.NewList([]int{1, 2, 3, 4, 5})fmt.Println("初始化一個鏈表 :",List.Show(fistNode))// 對鏈表進行頭插fistNode = List.FistAdd(fistNode, 0)fmt.Println("對鏈表進行頭插 0 :",List.Show(fistNode))// 對鏈表進行尾插List.LastAdd(fistNode, 6)fmt.Println("對鏈表進行尾插 6 :",List.Show(fistNode))// 刪除指定節點List.DelNode(fistNode, 3)fmt.Println("刪除指定節點 3 :",List.Show(fistNode))List.DelNode(fistNode, 3)fmt.Println("刪除指定節點 3 :",List.Show(fistNode))List.DelNode(fistNode, 3)fmt.Println("刪除指定節點 7 :",List.Show(fistNode))// 在第一個指定值后插入List.IndexValAdd(fistNode, 4, 41)fmt.Println("在第一個指定值后插入,若沒有,在鏈表尾部插入 4 41 :",List.Show(fistNode))List.IndexValAdd(fistNode, 7, 42)fmt.Println("在第一個指定值后插入,若沒有,在鏈表尾部插入 7 42 :",List.Show(fistNode))// 更改目標節點值List.ChangVal(fistNode, 4, 40)fmt.Println("更改目標節點值 4 40 :",List.Show(fistNode))List.ChangVal(fistNode, 7, 43)fmt.Println("更改目標節點值 7 43 :",List.Show(fistNode)) }效果圖
循環鏈表實現
package Listimport "fmt"func newCircleNode(val int) *listNode {node := new(listNode)node.val = valnode.next = nodereturn node }func NewCircleList(vals []int) *listNode {var fistNode *listNodevar curNode *listNodefor _, v := range vals {if curNode == nil {fistNode = newCircleNode(v)curNode = fistNodecontinue}node := newCircleNode(v)node.next = fistNodecurNode.next = nodecurNode = curNode.next}return fistNode }func isLastNode(fistNode *listNode, node *listNode) bool {return node.next == fistNode }// CircleFistAdd 頭插 func CircleFistAdd(fistNode **listNode, val int) {if fistNode == nil {return}curNode := *fistNodefor !isLastNode(*fistNode, curNode) {curNode = curNode.next}node := newCircleNode(val)curNode.next = nodenode.next = *fistNode*fistNode = node }// CircleLastAdd 尾插 func CircleLastAdd(fistNode *listNode, val int) {if fistNode == nil {return}curNode := fistNodefor !isLastNode(fistNode, curNode) {curNode = curNode.next}node := newCircleNode(val)node.next = fistNodecurNode.next = node}// CircleIndexValAdd 在第一個指定值后插入,若沒有,在鏈表尾部插入 func CircleIndexValAdd(fistNode *listNode, indexVal,val int) {if fistNode == nil {return}curNode := fistNodefor !isLastNode(fistNode, curNode) && curNode.val != indexVal {curNode = curNode.next}node := newCircleNode(val)node.next = curNode.nextcurNode.next = node}// CircleChangVal 更改目標節點值,若沒有,不做改動 func CircleChangVal(fistNode *listNode, indexVal, tarVal int) {if fistNode == nil {return}curNode := fistNodefor curNode.val != indexVal && !isLastNode(fistNode, curNode) {curNode = curNode.next}if curNode.val == indexVal {curNode.val = tarValreturn}fmt.Printf("節點 %d 不存在\n", indexVal) }// CircleDelNode 刪除指定節點,若沒有則不刪除 func CircleDelNode(fistNode *listNode, indexVal int) {if fistNode == nil {return}curNode := fistNodefor curNode.next.val != indexVal && !isLastNode(fistNode, curNode) {curNode = curNode.next}if curNode.next.val == indexVal {curNode.next = curNode.next.nextreturn}fmt.Printf("沒有該節點 %d \n", indexVal)}// CircleShow 查看鏈表 func CircleShow(fistNode *listNode) {if fistNode == nil {return}curNode := fistNodefor {if isLastNode(fistNode, curNode) {fmt.Printf("val:%d next:%d", curNode.val, curNode.next.val)break}fmt.Printf("val:%d next:%d -> ", curNode.val, curNode.next.val)curNode = curNode.next}fmt.Println() }驗證代碼
package mainimport ("Data/List""fmt" )func main() {// 初始化一個環形鏈表circleFistNode := List.NewCircleList([]int{1, 2, 3})fmt.Println("初始化一個鏈表 :")List.CircleShow(circleFistNode)// 對鏈表進行頭插List.CircleFistAdd(&circleFistNode, 0)fmt.Println("對鏈表進行頭插 0:")List.CircleShow(circleFistNode)// 對鏈表進行尾插List.CircleLastAdd(circleFistNode, 4)fmt.Println("對鏈表進行尾插 4 :")List.CircleShow(circleFistNode)// 刪除指定節點fmt.Println("刪除指定節點 3 :")List.CircleDelNode(circleFistNode, 3)List.CircleShow(circleFistNode)fmt.Println("刪除指定節點 3 :")List.CircleDelNode(circleFistNode, 3)List.CircleShow(circleFistNode)fmt.Println("刪除指定節點 7 :")List.CircleDelNode(circleFistNode, 7)List.CircleShow(circleFistNode)// 在第一個指定值后插入circleFistNode = List.NewCircleList([]int{1, 2, 3})fmt.Println("初始化一個鏈表 :")List.CircleShow(circleFistNode)List.CircleIndexValAdd(circleFistNode, 2, 41)fmt.Println("在第一個指定值后插入,若沒有,在鏈表尾部插入 2 41 :")List.CircleShow(circleFistNode)List.CircleIndexValAdd(circleFistNode, 7, 42)fmt.Println("在第一個指定值后插入,若沒有,在鏈表尾部插入 7 42 :")List.CircleShow(circleFistNode)// 更改目標節點值circleFistNode = List.NewCircleList([]int{1, 2, 3, 4})fmt.Println("初始化一個鏈表 :")List.CircleShow(circleFistNode)fmt.Println("更改目標節點值 3 40 :")List.CircleChangVal(circleFistNode, 3, 40)List.CircleShow(circleFistNode)fmt.Println("更改目標節點值 7 43 :")List.CircleChangVal(circleFistNode, 7, 43)List.CircleShow(circleFistNode)// }效果圖
總結
以上是生活随笔為你收集整理的链表简单实现(增删查改)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像处理中的通信原理——冈萨雷斯读书笔记
- 下一篇: 吴恩达作业1:逻辑回归实现猫的分类