Go語言單鏈表

package main

import "fmt"

type Node struct {
   data  int
   next  *Node
}

func Shownode(p *Node){   //遍歷
   for p != nil{
      fmt.Println(*p)
      p=p.next  //移動指針
   }
}

func main() {
   var head = new(Node)
   head.data = 0
   var tail *Node
   tail = head   //tail用於記錄最末尾的結點的地址,剛開始tail的的指針指向頭結點
   for i :=1 ;i<10;i++{
      var node = Node{data:i}
      (*tail).next = &node
      tail = &node
   }

   Shownode(head) //遍歷結果
}
相關文章
相關標籤/搜索