package main import "fmt" // 功能:实现一个单链表(给一个切片,将其写入到一个单链表中) type Node struct { Value int Next *Node } func create(slice []int) *Node { if len(slice) == 0 { return nil } node := new(Node) node.Value = slice[0] head := node //声明链表头指针 for _, v := range slice[1:] { tempNode := Node{Value: v, Next: nil} node.Next = &tempNode //指向下一个结点 node = node.Next //下一个结点赋值给临时变量 } return head } func showList(nowNode *Node) { for nowNode != nil { fmt.Printf("%v \n", nowNode.Value) nowNode = nowNode.Next } } func main() { slice := []int{1, 2, 3, 4} head := create(slice) fmt.Printf("%v", head) showList(head) }
标签:Node,node,head,单链,Next,slice,表中,go,nowNode From: https://www.cnblogs.com/zxqblogrecord/p/17553179.html