我試圖做一個堆棧,將存儲一系列霍夫曼樹結構。目前我正在使用我在github上發現的實現。在Go中實現堆棧的正確方法是什麼,以便它可以存儲結構?
package util
type item struct {
value interface{}
next *item
}
//Stack the implementation of stack
//this stack is not thread safe!
type Stack struct {
top *item
size int
}
// Basic stack methods...
的問題是,當我存儲我的哈夫曼樹結構中堆棧我不能使用任何哈夫曼樹的領域,比如左/右孩子。
package huffmantree
type HuffmanTree struct {
freq int
value byte
isLeaf bool
left *HuffmanTree
right *HuffmanTree
code []bool
depth int
}
我應該如何實現在圍棋這將正確地存儲結構堆棧,並允許訪問他們的田地?
編輯: 我試着更換interface {}
部分與huffmantree.HuffmanTree
(huffmantree結構),並得到該錯誤消息:
can't load package: import cycle not allowed
package github.com/inondle/huffman/util
imports github.com/inondle/huffman/huffmantree
imports github.com/inondle/huffman/util
import cycle not allowed
我的猜測是,huffmantree類進口util包和堆棧有進口huffmantree包,所以有某種衝突?任何人都知道問題出在哪裏
通過不使用空接口{},但實際的結構。所以在你的示例中:value HuffmanTree。 – 0x434D53
go中的通用版本是不可能的。 – 0x434D53
@ 0x434D53我嘗試用我的huffmantree結構替換接口{},但它導致了一個錯誤,它說'不能加載包:導入循環不允許'。 – Inondle