package main
import (
"fmt"
)//定義結構體 注意點:go不支援型別別名,type與c語言中的typedef並不一樣
/*var a int
a = 3
type int int
var b int
b = 3
fmt.println(a == b)
報錯:invalid operation: a == b (mismatched types int and int)
*/type node struct
//測試鍊錶是否為空
func isempty(list *node) bool
//測試是否為最後節點
func islast(position *node, list *node) bool
func find(value int, list *node) *node
return p
}//插入節點
func insert(value int, list *node, position *node)
tempcell.value = value
tempcell.next = position.next
position.next = tempcell
}//刪除節點
func delete(value int, list *node)
func findprevious(value int, list *node) *node
return p
}//刪除鍊錶 注意點:golang中函式傳遞指標變數,其實是傳遞實參
func deletelist(list **node)
}//列印列表
func printlist(list *node)
fmt.println()
}func main()
list := headnode
insert(1, list, headnode)
printlist(list)
fmt.println(isempty(list))
fmt.println(islast(headnode, list))
p := find(0, list)
insert(2, list, p)
printlist(list)
delete(1, list)
printlist(list)
deletelist(&list)
printlist(list)
}
golang實現單 雙鏈表
go實現乙個單鏈表 儲存內容為int 該鍊錶提供以下方法 1 建立鍊錶 2 在鍊錶頭部插入乙個節點 3 返回鍊錶元素個數 4 列印鍊錶所有的元素 type node struct type list struct func newlist list func this list add data i...
golang單鏈表反向
反轉乙個單鏈表 輸入 1 2 3 4 5 null 輸出 5 4 3 2 1 nullfunc reverselist head listnode listnode if head nil head.next nil next head.next head.next nil newhead reve...
單鏈表逆置(golang)
反轉乙個單鏈表。示例 輸入 1 2 3 4 5 null 輸出 5 4 3 2 1 null type listnode struct 遞迴解法 func reverselist head listnode listnode 1 2 3 4 5 nil 根據上面的遞迴終止條件,這裡第一次返回的時候,...