Skip to content

Commit e9d76b8

Browse files
committed
链式栈完成
1 parent 86d28aa commit e9d76b8

File tree

5 files changed

+38
-72
lines changed

5 files changed

+38
-72
lines changed

02-数据结构/02-栈3-链栈.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,42 @@
99
## 二 链栈的Go实现
1010
```go
1111

12+
type Node struct {
13+
data interface{}
14+
next *Node
15+
}
16+
17+
type LinkedStack struct {
18+
Top *Node // 栈顶元素
19+
Length int
20+
}
21+
22+
func New() *LinkedStack {
23+
return &LinkedStack{
24+
nil,
25+
0,
26+
}
27+
}
28+
29+
// 压栈
30+
func (ls *LinkedStack) Push(data interface{}) {
31+
node := new(Node)
32+
node.data = data
33+
node.next = ls.Top
34+
ls.Top = node
35+
ls.Length++
36+
}
37+
38+
// 出栈
39+
func (ls *LinkedStack) Pop() interface{} {
40+
if ls.Length == 0 {
41+
fmt.Println("stack is empty")
42+
return nil
43+
}
44+
value := ls.Top.data
45+
node := ls.Top
46+
ls.Top = node.next
47+
ls.Length--
48+
return value
49+
}
1250
```

02-数据结构/02-栈4-总结.md

Lines changed: 0 additions & 72 deletions
This file was deleted.

images/Algorithm/stack-2.png

-26.2 KB
Binary file not shown.

images/Algorithm/stack-3.png

-125 KB
Binary file not shown.

images/Algorithm/stack-4.png

-188 KB
Binary file not shown.

0 commit comments

Comments
 (0)