# golang中用slice实现queue

## 实现一

``````type Queue struct {
s []*valueType
}

func (q *Queue) pushBack(v *valueType) {
s = append(s, v)
}

func (q *Queue) popFront() *valueType {
if len(s) == 0 {
return nil
}

v := s[0]
s[0] = nil
s = s[1:]
return v
}
``````

## 实现二

``````// A wantConnQueue is a queue of wantConns.
type wantConnQueue struct {
// This is a queue, not a deque.
// popFront is trivial (headPos++) on the first stage, and
// pushBack is trivial (append) on the second stage.
// If the first stage is empty, popFront can swap the
// first and second stages to remedy the situation.
//
// This two-stage split is analogous to the use of two lists
// in Okasaki's purely functional queue but without the
// overhead of reversing the list when swapping stages.
tail    []*wantConn
}

// len returns the number of items in the queue.
func (q *wantConnQueue) len() int {
}

// pushBack adds w to the back of the queue.
func (q *wantConnQueue) pushBack(w *wantConn) {
q.tail = append(q.tail, w)
}

// popFront removes and returns the wantConn at the front of the queue.
func (q *wantConnQueue) popFront() *wantConn {
if len(q.tail) == 0 {
return nil
}
// Pick up tail as new head, clear tail.
}
return w
}

// peekFront returns the wantConn at the front of the queue without removing it.
func (q *wantConnQueue) peekFront() *wantConn {
}
if len(q.tail) > 0 {
return q.tail[0]
}
return nil
}

// cleanFront pops any wantConns that are no longer waiting from the head of the
// queue, reporting whether any were popped.
func (q *wantConnQueue) cleanFront() (cleaned bool) {
for {
w := q.peekFront()
if w == nil || w.waiting() {
return cleaned
}
q.popFront()
cleaned = true
}
}
``````

## reference

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

## 实现一

``````type Queue struct {
s []*valueType
}

func (q *Queue) pushBack(v *valueType) {
s = append(s, v)
}

func (q *Queue) popFront() *valueType {
if len(s) == 0 {
return nil
}

v := s[0]
s[0] = nil
s = s[1:]
return v
}
``````

## 实现二

``````// A wantConnQueue is a queue of wantConns.
type wantConnQueue struct {
// This is a queue, not a deque.
// popFront is trivial (headPos++) on the first stage, and
// pushBack is trivial (append) on the second stage.
// If the first stage is empty, popFront can swap the
// first and second stages to remedy the situation.
//
// This two-stage split is analogous to the use of two lists
// in Okasaki's purely functional queue but without the
// overhead of reversing the list when swapping stages.
tail    []*wantConn
}

// len returns the number of items in the queue.
func (q *wantConnQueue) len() int {
}

// pushBack adds w to the back of the queue.
func (q *wantConnQueue) pushBack(w *wantConn) {
q.tail = append(q.tail, w)
}

// popFront removes and returns the wantConn at the front of the queue.
func (q *wantConnQueue) popFront() *wantConn {
if len(q.tail) == 0 {
return nil
}
// Pick up tail as new head, clear tail.
}
return w
}

// peekFront returns the wantConn at the front of the queue without removing it.
func (q *wantConnQueue) peekFront() *wantConn {
}
if len(q.tail) > 0 {
return q.tail[0]
}
return nil
}

// cleanFront pops any wantConns that are no longer waiting from the head of the
// queue, reporting whether any were popped.
func (q *wantConnQueue) cleanFront() (cleaned bool) {
for {
w := q.peekFront()
if w == nil || w.waiting() {
return cleaned
}
q.popFront()
cleaned = true
}
}
``````