1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import "sync"
type Value int
type Queue struct {
d []Value
lock sync.RWMutex
}
func NewQueue() *Queue {
q := &Queue{}
q.d = []Value{}
return q
}
func (q *Queue) Push(t Value) {
q.lock.Lock()
q.d = append(q.d, t)
q.lock.Unlock()
}
func (q *Queue) Pop() Value {
q.lock.Lock()
ret := q.d[0]
q.d = q.d[:len(q.d)-1]
q.lock.Unlock()
return ret
}
func (q *Queue) Front() Value {
q.lock.Lock()
ret := q.d[0]
q.lock.Unlock()
return ret
}
func (q *Queue) IsEmpty() bool {
return len(q.d) == 0
}
func (q *Queue) Size() int {
return len(q.d)
}
|