go 單鏈表 增刪改查

2021-09-27 12:14:02 字數 1433 閱讀 9156

package main

import "fmt"

//鍊錶和資料結構

type node struct

type list struct

//鍊錶是否為空

func (this *list) isempty() bool

return false

}//鍊錶長度

func (this *list) length() int

node := this.head

count := 0

for node != nil

return count

}//從頭部新增元素

func (this *list) add(data string)

node.next = this.head

fmt.println(this.head)

this.head = node

fmt.println(this.head)

}//從尾部新增元素

//先儲存資料

node := &node

//判斷是否為空

if this.isempty() else

list.next = node }}

//插入到指定位置

func (this *list) insertnode(index int , data string) boolelse

//把pre的下個節點指標給了node

node := &node

node.next=pre.next

pre.next = node

} return true

}//刪除節點中所有相等的資料

func (this *list) removenode(data string) boolelseelse

} }return true

}//查詢

func (this *list) findnode(data string) int

list = list.next

} return count

}//迴圈鍊錶

func (this *list) foreach()

}//鍊錶反轉

func (this *list) reseve() *node

return pre

}//列印指定鍊錶

func printnode(node *node)

}func main()

list.add("data")

list.foreach()

pos := list.findnode("hello")

fmt.println(pos)

newlist := list.reseve()

printnode(newlist)

}

單鏈表增刪改查

include include include include using namespace std struct node node int x,node next null 帶參初始化 建立煉表頭結點,新增引用因為要改變指標的位址指向 void createlink node head 新增鍊...

單鏈表增刪改查

單鏈表單鍊錶 linked list 由各個記憶體結構通過乙個 next 指標鏈結在一起組成,每乙個內 存結構都存在後繼記憶體結構 鏈尾除外 記憶體結構由資料域和 next 指標域組成。單鏈表實現圖示 解析 data 資料 next 指標,組成乙個單鏈表的記憶體結構 第乙個記憶體結構稱為 鏈頭,最後...

單鏈表 增刪改查

目錄基本面試題 class heronode class singlelinkedlistpublic void add heronode heronode public void addbyorder heronode heronode if temp.next.no heronode.no el...