Golang 程序来定义单链表。
示例

此问题的求解方法
步骤 1 - 我们定义一个节点结构。
步骤 2 - 构建链表,使得前一个节点存储下一个节点的地址。
示例
package main
import "fmt"
type Node struct {
value int
next *Node
}
func NewNode(value int) *Node{
var n Node
n.value = value
n.next = nil
return &n
}
func TraverseLinkedList(head *Node){
fmt.Printf("Linked List: ")
temp := head
for temp != nil {
fmt.Printf("%d ", temp.value)
temp = temp.next
}
}
func main(){
head := NewNode(30)
head.next = NewNode(10)
head.next.next = NewNode(40)
head.next.next.next = NewNode(40)
TraverseLinkedList(head)
}输出
Linked List: 30 10 40 40
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP