-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (68 loc) · 1.04 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
package main
import "fmt"
type Node struct {
Val int
Next *Node
}
type Queue struct {
Node *Node
Size uint
}
func (q *Queue) Push(val int) bool {
if q.Node == nil {
q.Node = &Node{Val: val}
q.Size++
return true
}
tmp := &Node{val, nil}
tmp.Next = q.Node
q.Node = tmp
q.Size++
return true
}
func (q *Queue) Pop() (int, bool) {
if q.Size == 0 {
return 0, false
}
var tmp *Node = q.Node
if q.Size == 1 {
q.Node = nil
q.Size = 0
return tmp.Val, true
}
for t := q.Node; t.Next != nil; t = t.Next {
tmp = t
}
v := tmp.Next.Val
tmp.Next = nil
q.Size--
return v, true
}
func (q *Queue) Traverse() {
t := q.Node
if t == nil {
fmt.Println("-> Empty list!")
}
for ; t != nil; t = t.Next {
fmt.Printf("%d ->", t.Val)
}
fmt.Println()
}
func main() {
q := new(Queue)
q.Push(00)
q.Push(10)
q.Push(20)
q.Push(30)
q.Push(40)
q.Push(50)
fmt.Println("Size:", q.Size)
q.Traverse()
for i := 0; i < 6; i++ {
v, _ := q.Pop()
fmt.Printf("-> %d ", v)
}
fmt.Println()
fmt.Println("Size:", q.Size)
q.Traverse()
}