第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 自定義單鏈表的誤區(qū)

Golang 自定義單鏈表的誤區(qū)

Go
翻翻過去那場雪 2022-06-21 10:47:01
至于我,這是對第三個if語句的誤解。為什么list.tail.next = newNode也向head.next添加元素?func (list *SingleLinkedList) Add(v int) {    newNode := &SLLNode{value: v}    if list.head == nil {        list.head = newNode    } else if list.tail == list.head {        list.head.next = newNode    } else if list.tail != nil {        list.tail.next = newNode    }    list.tail = newNode}這是一個編譯的程序示例:package mainimport "fmt"// Linked List Codetype SingleLinkedList struct {    head *SLLNode    tail *SLLNode}func NewSingleLinkedList() *SingleLinkedList {    return new(SingleLinkedList)}func (list *SingleLinkedList) Add(v int) {    newNode := &SLLNode{value: v}    if list.head == nil {        list.head = newNode    } else if list.tail == list.head {        list.head.next = newNode    } else if list.tail != nil {        list.tail.next = newNode    }    list.tail = newNode}func (list *SingleLinkedList) String() string {    stringResult := ""    for n := list.head; n != nil; n = n.next {        stringResult += fmt.Sprintf(" {%d} ", n.GetValue())    }    return stringResult}// Node Codetype SLLNode struct {    next  *SLLNode    value int}func (sNode *SLLNode) SetValue(v int) {    sNode.value = v}func (sNode *SLLNode) GetValue() int {    return sNode.value}func main() {    // Linked List    list:= NewSingleLinkedList()    list.Add(4)    list.Add(6)    list.Add(3)    list.Add(3)    fmt.Println(list)}
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

list.tail.next = newNode設(shè)置的唯一原因list.head.next = newNode是,在操作時list.tail == list.head。

在您的代碼中,盡管我只看到 head 等于 tail,但前提是列表有一個元素。


查看完整回答
反對 回復(fù) 2022-06-21
  • 1 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號