Skip to content

Commit fff09ce

Browse files
committed
add go queue
1 parent 6a2f3f6 commit fff09ce

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

go-note/algorithms.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.. _go_algorithms:
2+
3+
Go 算法和数据结构
4+
=====================================================================
5+
6+
Queue
7+
--------------------------------------------------
8+
9+
.. code-block:: go
10+
11+
type Item struct {
12+
Num int
13+
Order int
14+
}
15+
type Queue struct {
16+
items []Item
17+
}
18+
19+
func NewQueue() *Queue {
20+
return &Queue{
21+
items: make([]Item, 0),
22+
}
23+
}
24+
func (q *Queue) Push(x Item) {
25+
q.items = append(q.items, x)
26+
}
27+
28+
func (q *Queue) Pop() Item {
29+
x := q.items[0]
30+
q.items = q.items[1:]
31+
return x
32+
}
33+
34+
func (q *Queue) Front() Item {
35+
return q.items[0]
36+
}
37+
38+
func (q *Queue) End() Item {
39+
return q.items[len(q.items)-1]
40+
}
41+
42+
func (q *Queue) Empty() bool {
43+
return len(q.items) == 0
44+
}

go-note/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Golang 快速入门 Go For Pythonisa
99

1010
web
1111
tricks
12+
algorithms

0 commit comments

Comments
 (0)