- Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathqueue.go
45 lines (37 loc) · 654 Bytes
/
queue.go
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
44
45
package queue
import"sync"
typeQueuestruct {
queue []interface{}
lenint
lock*sync.Mutex
}
funcNew() *Queue {
q:=&Queue{}
q.queue=make([]interface{}, 0)
q.len=0
q.lock=new(sync.Mutex)
returnq
}
func (q*Queue) Len() int {
returnq.len
}
func (q*Queue) isEmpty() bool {
q.lock.Lock()
deferq.lock.Unlock()
returnq.len==0
}
func (q*Queue) Shift() (elinterface{}) {
el, q.queue=q.queue[0], q.queue[1:]
q.len--
return
}
func (q*Queue) Push(elinterface{}) {
q.queue=append(q.queue, el)
q.len++
return
}
func (q*Queue) Peek() (elinterface{}) {
q.lock.Lock()
deferq.lock.Unlock()
returnq.queue[0]
}