Golang中使用container/list创建邻接表。

huangapple go评论140阅读模式
英文:

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 (
	&quot;container/list&quot;
	&quot;fmt&quot;
)

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 &lt;= g.V; i++ {
		fmt.Printf(&quot;the neighbor of %d includes %v \n&quot;, 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)
}

huangapple
  • 本文由 发表于 2023年4月2日 21:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75912316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定