forked from Focinfi/gcircularqueue
/
gcircularqueue.go
60 lines (52 loc) · 1.39 KB
/
gcircularqueue.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package gcircularqueue
type CircularQueue struct {
capacity int
elements []interface{}
len int
head int
tail int
}
// NewCircularQueue creates a new CircularQueue passing an integer as its size
// and returns its pointer
func NewCircularQueue(size int) *CircularQueue {
cq := CircularQueue{capacity: size + 1, head: 0, tail: 0, len: 0}
cq.elements = make([]interface{}, cq.capacity)
return &cq
}
// IsEmpty returns true if the queue is empty
func (c CircularQueue) IsEmpty() bool {
return c.head == c.tail
}
// IsFull returns true if the queue is full
func (c CircularQueue) IsFull() bool {
return c.head == (c.tail+1)%c.capacity
}
// Push pushes an element to the queue
// note: if pushing into a full queue, it will panic
func (c *CircularQueue) Push(e interface{}) {
if c.IsFull() {
panic("Queue is full")
}
c.elements[c.tail] = e
c.tail = (c.tail + 1) % c.capacity
c.len++
}
// Shift shifts an element which was pushed the earliest
// note: it will return nil if this queue is empty
func (c *CircularQueue) Shift() (e interface{}) {
if c.IsEmpty() {
return nil
}
e = c.elements[c.head]
c.head = (c.head + 1) % c.capacity
c.len--
return
}
// Elements returns an underlying buffer's content
func (c *CircularQueue) Elements() []interface{} {
return c.elements[:c.len]
}
// Len returns a number of elements stored in the queue
func (c *CircularQueue) Len() int {
return c.len
}