英文:
Golang container/list create an adjacency list
问题
我正在尝试使用Go语言构建邻接表。
package main
import (
"container/list"
"fmt"
)
type SocialGraph struct {
V int
Arr map[int]*list.List
}
func AddEdge(g SocialGraph, i, j int, undir bool) {
g.Arr[i].PushBack(j)
if undir {
g.Arr[j].PushBack(i)
}
fmt.Println(g)
}
func PrintEdgeList(g SocialGraph) {
for i := 0; i <= g.V; i++ {
fmt.Printf("节点 %d 的邻居包括 %v \n", i, g.Arr[i])
}
}
func main() {
graph := SocialGraph{V: 6, Arr: make(map[int]*list.List, 7)}
fmt.Println(graph)
AddEdge(graph, 1, 3, true)
AddEdge(graph, 2, 3, true)
AddEdge(graph, 2, 4, true)
AddEdge(graph, 1, 5, true)
AddEdge(graph, 1, 6, true)
PrintEdgeList(graph)
}
我使用list.List
来定义邻居列表,但是我总是遇到以下错误:
container/list.(*List).lazyInit(...)
E:/Go/src/container/list/list.go:86
container/list.(*List).PushBack(...)
E:/Go/src/container/list/list.go:151
main.AddEdge({0xc00002e000?, 0xc00007feb0?}, 0x9b5cca?, 0xc000062000?, 0x1)
我不确定是否应该使用map[int][]list.List
还是map[int]*list.List
来定义映射值。
英文:
I was trying to build an adjacency list with golang.
package main
import (
"container/list"
"fmt"
)
type SocialGraph struct {
// define the size
V int
// define the type of array of int list, row is the list order, column is the list of neighbors
// whether to create array from make or just define?
Arr map[int]*list.List
}
func AddEdge(g SocialGraph, i, j int, undir bool) {
/*
param g: instance of SocialGraph
param i: edge start
param j: edge end
param undir: whether directed
*/
g.Arr[i].PushBack(j)
if undir {
g.Arr[j].PushBack(i)
}
fmt.Println(g)
}
func PrintEdgeList(g SocialGraph) {
for i := 0; i <= g.V; i++ {
fmt.Printf("the neighbor of %d includes %v \n", i, g.Arr[i])
}
}
func main() {
graph := SocialGraph{V: 6, Arr: make(map[int]*list.List, 7)}
fmt.Println(graph)
AddEdge(graph, 1, 3, true)
AddEdge(graph, 2, 3, true)
AddEdge(graph, 2, 4, true)
AddEdge(graph, 1, 5, true)
AddEdge(graph, 1, 6, true)
PrintEdgeList(graph)
}
I was using list.List to define the list of neighbors, but I always got error with:
container/list.(*List).lazyInit(...)
E:/Go/src/container/list/list.go:86
container/list.(*List).PushBack(...)
E:/Go/src/container/list/list.go:151
main.AddEdge({0xc00002e000?, 0xc00007feb0?}, 0x9b5cca?, 0xc000062000?, 0x1)
I am not sure whether I should define the value in map with map[int][]list.List or map[int]*list.List
答案1
得分: 1
这个 SocialGraph 的 Arr 没有被初始化,我认为你应该初始化 Arr,就像这样,或者使用其他的初始化方法
func AddEdge(g SocialGraph, i, j int, undir bool) {
/*
param g: SocialGraph 的实例
param i: 边的起点
param j: 边的终点
param undir: 是否是有向边
*/
if g.Arr[i] == nil {
g.Arr[i] = list.New()
}
g.Arr[i].PushBack(j)
if undir {
if g.Arr[j] == nil {
g.Arr[j] = list.New()
}
g.Arr[j].PushBack(i)
}
fmt.Println(g)
}
英文:
This SocialGraph's Arr Not initialized, I think you shoud initialized Arr, just like this, or other initialized method
func AddEdge(g SocialGraph, i, j int, undir bool) {
/*
param g: instance of SocialGraph
param i: edge start
param j: edge end
param undir: whether directed
*/
if g.Arr[i] == nil {
g.Arr[i] = list.New()
}
g.Arr[i].PushBack(j)
if undir {
if g.Arr[j] == nil {
g.Arr[j] = list.New()
}
g.Arr[j].PushBack(i)
}
fmt.Println(g)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论