亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:52000
  • 待審:37
  • 小程序:12
  • 文章:1037587
  • 會員:756

Go語言編程指南:單鏈表實現詳解

在Go語言中,單鏈表是一種常見的數據結構,用于存儲一系列元素并按順序訪問。本文將詳細介紹單鏈表的實現原理,并給出具體的Go語言代碼示例。

單鏈表的定義

單鏈表是一種線性表的數據結構,其中的每個元素(節點)包含兩部分:數據域和指針域。數據域用于存儲元素的值,指針域則指向下一個節點。最后一個節點的指針域通常為空,表示鏈表的結束。

單鏈表的節點定義

首先,我們定義一個單鏈表的節點類型:

type Node struct {
    data int
    next *Node
}

登錄后復制

其中,data字段存儲節點的值,next字段存儲指向下一個節點的指針。

單鏈表的初始化

接下來,我們定義單鏈表的初始化函數:

type LinkedList struct {
    head *Node
}

func NewLinkedList() *LinkedList {
    return &LinkedList{}
}

登錄后復制

在初始化函數中,我們創建一個空的鏈表,并將頭節點指針初始化為空。

單鏈表的插入操作

實現單鏈表的插入操作可以分為兩種情況:在鏈表頭部插入節點和在鏈表尾部插入節點。

首先是在鏈表頭部插入節點的函數:

func (list *LinkedList) InsertAtBeginning(value int) {
    newNode := &Node{data: value}
    newNode.next = list.head
    list.head = newNode
}

登錄后復制

在這個函數中,我們首先創建一個新節點并將其值初始化為傳入的數值。然后將新節點的指針指向鏈表頭部,最后更新鏈表的頭節點為新節點。

接下來是在鏈表尾部插入節點的函數:

func (list *LinkedList) InsertAtEnd(value int) {
    newNode := &Node{data: value}
    if list.head == nil {
        list.head = newNode
        return
    }

    current := list.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

登錄后復制

這個函數首先創建一個新節點,并判斷鏈表是否為空。如果為空,則直接將新節點設為頭節點;否則,遍歷鏈表直到找到最后一個節點,然后將新節點插入到最后一個節點的后面。

單鏈表的刪除操作

刪除操作分為兩種情況:刪除頭節點和刪除指定數值的節點。

首先是刪除頭節點的函數:

func (list *LinkedList) DeleteAtBeginning() {
    if list.head == nil {
        return
    }
    list.head = list.head.next
}

登錄后復制

這個函數直接將頭節點指針指向下一個節點,從而刪除了頭節點。

接下來是刪除指定數值的節點的函數:

func (list *LinkedList) DeleteByValue(value int) {
    if list.head == nil {
        return
    }
    if list.head.data == value {
        list.head = list.head.next
        return
    }

    prev := list.head
    current := list.head.next
    for current != nil {
        if current.data == value {
            prev.next = current.next
            return
        }
        prev = current
        current = current.next
    }
}

登錄后復制

這個函數中,我們需要先判斷鏈表是否為空。然后從頭節點開始遍歷鏈表,找到目標數值所在的節點并刪除。

單鏈表的遍歷操作

最后是單鏈表的遍歷操作:

func (list *LinkedList) Print() {
    current := list.head
    for current != nil {
        fmt.Print(current.data, " ")
        current = current.next
    }
    fmt.Println()
}

登錄后復制

這個函數從頭節點開始逐個打印節點的值,直到鏈表結束。

示例代碼

下面是一個完整的示例代碼,演示了如何使用單鏈表:

package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func NewLinkedList() *LinkedList {
    return &LinkedList{}
}

func (list *LinkedList) InsertAtBeginning(value int) {
    newNode := &Node{data: value}
    newNode.next = list.head
    list.head = newNode
}

func (list *LinkedList) InsertAtEnd(value int) {
    newNode := &Node{data: value}
    if list.head == nil {
        list.head = newNode
        return
    }

    current := list.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (list *LinkedList) DeleteAtBeginning() {
    if list.head == nil {
        return
    }
    list.head = list.head.next
}

func (list *LinkedList) DeleteByValue(value int) {
    if list.head == nil {
        return
    }
    if list.head.data == value {
        list.head = list.head.next
        return
    }

    prev := list.head
    current := list.head.next
    for current != nil {
        if current.data == value {
            prev.next = current.next
            return
        }
        prev = current
        current = current.next
    }
}

func (list *LinkedList) Print() {
    current := list.head
    for current != nil {
        fmt.Print(current.data, " ")
        current = current.next
    }
    fmt.Println()
}

func main() {
    list := NewLinkedList()

    list.InsertAtEnd(1)
    list.InsertAtEnd(2)
    list.InsertAtEnd(3)
    list.Print()

    list.DeleteByValue(2)
    list.Print()

    list.DeleteAtBeginning()
    list.Print()
}

登錄后復制

分享到:
標簽:Go語言 單鏈表 實現
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 52000

    網站

  • 12

    小程序

  • 1037587

    文章

  • 756

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定