What is the correct way to implement a stack in Go so that it will store structs?

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

What is the correct way to implement a stack in Go so that it will store structs?

问题

我正在尝试创建一个可以存储一系列Huffman树结构的堆栈。目前我正在使用在GitHub上找到的一个实现。

  1. package util
  2. type item struct {
  3. value interface{}
  4. next *item
  5. }
  6. // Stack the implementation of stack
  7. // this stack is not thread safe!
  8. type Stack struct {
  9. top *item
  10. size int
  11. }
  12. // Basic stack methods...

问题是,当我将Huffman树结构存储在堆栈中时,无法使用Huffman树的任何字段,例如左/右子节点。

  1. package huffmantree
  2. type HuffmanTree struct {
  3. freq int
  4. value byte
  5. isLeaf bool
  6. left *HuffmanTree
  7. right *HuffmanTree
  8. code []bool
  9. depth int
  10. }

我应该如何在Go中实现一个堆栈,可以正确存储结构体并允许访问它们的字段?

编辑:
我尝试将interface{}部分替换为huffmantree.HuffmanTree(huffmantree结构体),并得到以下错误消息:

  1. can't load package: import cycle not allowed
  2. package github.com/inondle/huffman/util
  3. imports github.com/inondle/huffman/huffmantree
  4. imports github.com/inondle/huffman/util
  5. import cycle not allowed

我猜测huffmantree类导入了util包,而堆栈必须导入huffmantree包,所以出现了某种冲突。有人知道出了什么问题吗?

英文:

I'm trying to make a stack that will store a series of Huffman Tree structs. Currently I'm using an implementation that I found on github.

  1. package util
  2. type item struct {
  3. value interface{}
  4. next *item
  5. }
  6. //Stack the implementation of stack
  7. //this stack is not thread safe!
  8. type Stack struct {
  9. top *item
  10. size int
  11. }
  12. // Basic stack methods...

The problem is that when I store my Huffman tree structs in the stack I can't use any of the fields of the Huffman tree such as left/right child.

  1. package huffmantree
  2. type HuffmanTree struct {
  3. freq int
  4. value byte
  5. isLeaf bool
  6. left *HuffmanTree
  7. right *HuffmanTree
  8. code []bool
  9. depth int
  10. }

How am I supposed to implement a stack in Go which will correctly store structs and allow access to their fields?

Edit:
I tried replacing the interface {} part with huffmantree.HuffmanTree (huffmantree struct) and got this error message:

  1. can't load package: import cycle not allowed
  2. package github.com/inondle/huffman/util
  3. imports github.com/inondle/huffman/huffmantree
  4. imports github.com/inondle/huffman/util
  5. import cycle not allowed

My guess would be that the huffmantree class imports the util package and the stack has to import the huffmantree package so there is some sort of conflict? Anyone know what went wrong?

答案1

得分: 7

在Go语言中实现栈的正确方法是简单地使用切片。

  1. stack := []*HuffmanTree{}

你可以使用append将元素推入栈中,使用以下方式将元素弹出栈:

  1. v, stack := stack[len(stack)-1], stack[:len(stack)-1]

如果你愿意,你可以将其封装成自己的类型,但是使用切片更容易理解。

  1. type Stack []*HuffmanTree
  2. func NewStack() *Stack {
  3. var s []*HuffmanTree
  4. return (*Stack)(&s)
  5. }
  6. func (s *Stack) Pop() *HuffmanTree {
  7. if len(*s) == 0 {
  8. return nil
  9. }
  10. v := (*s)[len(*s)-1]
  11. *s = (*s)[:len(*s)-1]
  12. return v
  13. }
  14. func (s *Stack) Push(h *HuffmanTree) {
  15. *s = append(*s, h)
  16. }

正如icza所观察到的,如果栈的生命周期比HuffmanTree对象长,你可能希望将刚弹出的条目清零,以允许垃圾收集器收集无引用的对象。

英文:

The right way in go to implement a stack is simply to use a slice.

  1. stack := []*HuffmanTree{}

You can push to the stack using append, and pop by writing:

  1. v, stack := stack[len(stack)-1], stack[:len(stack)-1]

You could encapsulate it into its own type if you prefer, but a slice is easier to understand.

  1. type Stack []*HuffmanTree{}
  2. func NewStack() *Stack {
  3. var s []*HuffmanTree
  4. return (*Stack)(&s)
  5. }
  6. func (s *Stack) Pop() *HuffmanTree {
  7. if len(*s) == 0 {
  8. return nil
  9. }
  10. v = (*s)[len(*s)-1]
  11. *s = (*s)[:len(*s)-1]
  12. return v
  13. }
  14. func (s *Stack) Push(h *HuffmanTree) {
  15. *s = append(*s, h)
  16. }

As icza observes, if the stacks live longer than the HuffmanTree objects, you may wish to zero the just-popped entry in your stack to allow the garbage collector to collect unreferenced objects.

huangapple
  • 本文由 发表于 2015年5月19日 09:29:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/30315334.html
匿名

发表评论

匿名网友

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

确定