-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.go
55 lines (47 loc) · 928 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
46
47
48
49
50
51
52
53
54
55
package generic
// create a node that holds the graphs vertex as data
type queueNode struct {
value interface{}
next *queueNode
}
// create a queue data structure
type simpleQueue struct {
head *queueNode
tail *queueNode
length int
}
func NewQueue() Queue {
return &simpleQueue{}
}
func (q *simpleQueue) Enqueue(value interface{}) {
q.length++
n := &queueNode{value: value}
// if the queue is empty, set the head and tail as the node value
if q.tail == nil {
q.head = n
q.tail = n
return
}
q.tail.next = n
q.tail = n
}
//
func (q *simpleQueue) Dequeue() interface{} {
n := q.head
// return nil, if head is empty
if n == nil {
return nil
}
q.head = q.head.next
// if there wasn't any next node, that
// means the queue is empty, and the tail
// should be set to nil
if q.head == nil {
q.tail = nil
}
q.length--
return n.value
}
func (q *simpleQueue) Len() int {
return q.length
}