Skip to content

Commit e05bbc7

Browse files
committed
go stack
1 parent 28c14d7 commit e05bbc7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

go-note/algorithms.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33
Go 算法和数据结构
44
=====================================================================
55

6+
Stack
7+
--------------------------------------------------
8+
9+
.. code-block:: go
10+
11+
type Stack struct {
12+
st []int
13+
}
14+
15+
func NewStack() *Stack {
16+
return &Stack{st:make([]int,0)}
17+
}
18+
func (s *Stack) Push(x int) {
19+
s.st = append(s.st, x)
20+
}
21+
22+
func (s *Stack) Peek() int{
23+
return s.st[len(s.st)-1]
24+
}
25+
26+
func (s *Stack) Pop() int{
27+
n := len(s.st)
28+
top := s.st[n-1]
29+
s.st = s.st[:n-1]
30+
return top
31+
}
32+
33+
func (s *Stack) Empty() bool{
34+
return len(s.st) == 0
35+
}
36+
637
Queue
738
--------------------------------------------------
839

0 commit comments

Comments
 (0)