標籤:first i++ == get ret 長度 port int data
package mainimport "fmt"//定義節點type Node struct {Data intNext *Node}/** 返回第一個節點* h 頭結點 */func GetFirst(h *Node) *Node {if h.Next == nil {return nil}return h.Next}/** 返回最後一個節點* h 頭結點 */func GetLast(h *Node) *Node {if h.Next == nil {return nil}i := hfor i.Next != nil {i = i.Nextif i.Next == nil {return i}}return nil}//取長度func GetLength(h *Node) int {var i int = 0n := hfor n.Next != nil {i++n = n.Next}return i}//插入一個節點//h: 頭結點//d:要插入的節點//p:要插入的位置func Insert(h, d *Node, p int) bool {if h.Next == nil {h.Next = dreturn true}i := 0n := hfor n.Next != nil {i++if i == p {if n.Next.Next == nil {n.Next = dreturn true} else {d.Next = n.Nextn.Next = d.Nextreturn true}}n = n.Nextif n.Next == nil {n.Next = dreturn true}}return false}//取出指定節點func GetLoc(h *Node, p int) *Node {if p < 0 || p > GetLength(h) {return nil}var i int = 0n := hfor n.Next != nil {i++n = n.Nextif i == p {return n}}return nil}func main() {var h Nodeli := []int{2, 4, 3}for _, l := range li {var d Noded.Data = lInsert(&h, &d, l)}var i Nodels := []int{5, 6, 4}for _, s := range ls {var dd Nodedd.Data = sInsert(&i, &dd, s)}sum := 0sum = GetLoc(&h, 3).Data*100 + GetLoc(&h, 2).Data*10 + GetLoc(&h, 1).Data + GetLoc(&i, 3).Data*100 + GetLoc(&i, 2).Data*10 + GetLoc(&i, 1).Datafmt.Println(sum)}
go語言 鏈表練習