-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.go
72 lines (56 loc) · 855 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// +build
package Queue
import "fmt"
type Node struct {
Data int
Next *Node
}
type Queue struct {
Head *Node
}
func (q *Queue) Enqueue(value int) {
var n *Node = new(Node)
n.Data = value
if q.Head == nil {
n.Next = nil
q.Head = n
return
}
n.Next = q.Head
q.Head = n
return
}
func (q *Queue) Dequeue() *Node {
current := q.Head
previous := q.Head
for current.Next != nil {
previous = current
current = current.Next
}
previous.Next = nil
return current
}
func (q *Queue) GetSize() int {
n := q.Head
i := 1
if n == nil { return 0 }
for n.Next != nil {
i++
n = n.Next
}
return i
}
func (q *Queue) IsEmpty() bool {
if q.GetSize() == 0 {
return true
}
return false
}
func (q *Queue) Print() {
n := q.Head
for n.Next != nil {
fmt.Printf("%d -> ", n.Data)
n = n.Next
}
fmt.Printf("%d\n", n.Data)
}