直接代码吧,都是我自己写的测试代码
package main import "fmt" type NodeList struct { data int next *NodeList } func ShowNode(node *NodeList) { for node !=nil { //fmt.Printf("type :%T ,value:%v\n",*node,*node) //fmt.Printf("data:%v,next:%v--->",node.data,node.next) fmt.Printf("%v--->",node.data) node = node.next //移动指针 } } func InsertHeadData(){ var head = new(NodeList) head.data = 0 var tail *NodeList tail = head for i :=1;i<10;i++{ var node = NodeList{data: i} node.next = tail tail = &node } ShowNode(tail) } func InsertTailData() { var head = new(NodeList) head.data = 0 var newList *NodeList newList = head for i :=1;i<10;i++{ var node = NodeList{data: i} (*newList).next = &node newList = &node } ShowNode(head) } func main() { //node1 := new(NodeList) //node2 := new(NodeList) //node1.data = 1 //node2.data = 2 // //node1.next = node2 // //node3 := new(NodeList) //node2.next = node3 // //ShowNode(node1) //InsertHeadData() InsertTailData() }