Is there a queue implementation?

后端 未结 14 1053
时光取名叫无心
时光取名叫无心 2020-12-23 02:14

Can anyone suggest Go container for simple and fast FIF/queue, Go has 3 different containers: heap, list and vector. Which one is more

相关标签:
14条回答
  • 2020-12-23 03:03

    To expand on the implementation side, Moraes proposes in his gist some struct from queue and stack:

    // Stack is a basic LIFO stack that resizes as needed.
    type Stack struct {
        nodes   []*Node
        count   int
    }
    // Queue is a basic FIFO queue based on a circular list that resizes as needed.
    type Queue struct {
        nodes   []*Node
        head    int
        tail    int
        count   int
    }
    

    You can see it in action in this playground example.

    0 讨论(0)
  • 2020-12-23 03:04

    Just embed a "container/list" inside a simple implementation and expose the interface

    package queue
    
    import "container/list"
    
    // Queue is a queue
    type Queue interface {
        Front() *list.Element
        Len() int
        Add(interface{})
        Remove()
    }
    
    type queueImpl struct {
        *list.List
    }
    
    func (q *queueImpl) Add(v interface{}) {
        q.PushBack(v)
    }
    
    func (q *queueImpl) Remove() {
        e := q.Front()
        q.List.Remove(e)
    }
    
    // New is a new instance of a Queue
    func New() Queue {
        return &queueImpl{list.New()}
    }
    
    0 讨论(0)
提交回复
热议问题