Skip to content

Commit b511e72

Browse files
committed
stack
1 parent dc9fd98 commit b511e72

File tree

6 files changed

+153
-2
lines changed

6 files changed

+153
-2
lines changed

data-structures/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
[算法时间复杂度的计算](http://univasity.iteye.com/blog/1164707)
88

9+
[go实现01](https://github.com/arnauddri/algorithms)
10+
11+
[go实现02](https://github.com/floyernick/Data-Structures-and-Algorithms)
12+
913
### 逻辑结构:
1014

1115
具体问题抽象出来的数学模型

data-structures/stack/stack.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

data-structures/stack/stack_array.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package stack
2+
3+
import (
4+
"errors"
5+
"sync"
6+
)
7+
8+
const ARRAY_SIZE = 10
9+
10+
//数组实现
11+
type Stack struct {
12+
data [ARRAY_SIZE]int
13+
top int
14+
lock sync.Mutex
15+
}
16+
17+
func New() *Stack {
18+
return &Stack{}
19+
}
20+
21+
func (s *Stack) Len() int {
22+
s.lock.Lock()
23+
defer s.lock.Unlock()
24+
return s.top
25+
}
26+
27+
func (s *Stack) IsEmpty() bool {
28+
s.lock.Lock()
29+
defer s.lock.Unlock()
30+
return s.top == 0
31+
}
32+
33+
func (s *Stack) Push(i int) error {
34+
s.lock.Lock()
35+
defer s.lock.Unlock()
36+
if s.top == ARRAY_SIZE {
37+
return errors.New("栈已满")
38+
}
39+
s.data[s.top] = i
40+
s.top++
41+
return nil
42+
}
43+
func (s *Stack) Pop() (int, error) {
44+
s.lock.Lock()
45+
defer s.lock.Unlock()
46+
if s.top == 0 {
47+
return 0, errors.New("栈空")
48+
}
49+
s.top--
50+
return s.data[s.top], nil
51+
}
52+
53+
func (s *Stack) Peek() (int, error) {
54+
s.lock.Lock()
55+
defer s.lock.Unlock()
56+
if s.top == 0 {
57+
return 0, errors.New("栈空")
58+
}
59+
return s.data[s.top-1], nil
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package stack
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNew(t *testing.T) {
9+
s := New()
10+
for i := 0; i < 10; i++ {
11+
err := s.Push(i)
12+
assert.NoError(t, err)
13+
}
14+
err := s.Push(11)
15+
assert.Error(t, err)
16+
for i := 9; i >= 0; i-- {
17+
k, _ := s.Pop()
18+
assert.Equal(t, i, k)
19+
}
20+
_, err = s.Pop()
21+
assert.Error(t, err)
22+
}
23+
24+
func TestNewListStack(t *testing.T) {
25+
s := NewListStack()
26+
for i := 0; i < 10; i++ {
27+
s.Push(i)
28+
}
29+
for i := 9; i >= 0; i-- {
30+
k, _ := s.Pop()
31+
assert.Equal(t, i, k)
32+
}
33+
_, err := s.Pop()
34+
assert.Error(t, err)
35+
}

data-structures/stack/stack_list.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package stack
2+
3+
import "errors"
4+
5+
//使用链表实现栈
6+
//以下是使用单向链表,使用头插法实现,也可以使用标准库的双向链表实现
7+
//节点,需要动态申请内存空间
8+
type Node struct {
9+
data int
10+
next *Node
11+
}
12+
13+
//栈
14+
type ListStack struct {
15+
top *Node
16+
}
17+
18+
func NewListStack() *ListStack {
19+
return &ListStack{}
20+
}
21+
22+
//是否为空
23+
func (ls *ListStack) IsEmpty() bool {
24+
return ls.top == nil
25+
}
26+
27+
//入栈,头插法
28+
func (ls *ListStack) Push(i int) {
29+
//新建节点
30+
n := &Node{i, nil}
31+
if ls.top != nil {
32+
n.next = ls.top
33+
}
34+
ls.top = n
35+
}
36+
37+
//出栈,从头弹出
38+
func (ls *ListStack) Pop() (int, error) {
39+
if ls.top == nil {
40+
return 0, errors.New("栈空")
41+
}
42+
i := ls.top.data
43+
ls.top = ls.top.next
44+
return i, nil
45+
46+
}
47+
48+
//查看
49+
func (ls *ListStack) Peek() (int, error) {
50+
if ls.top == nil {
51+
return 0, errors.New("栈空")
52+
}
53+
return ls.top.data, nil
54+
}

data-structures/stack/stack_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)