Skip to content

Commit b847f32

Browse files
committed
add
1 parent be78aef commit b847f32

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

jiuzhangsuanfa/stack/satck.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package stack
2+
3+
type Stack struct {
4+
array []interface{}
5+
}
6+
7+
func NewStack() *Stack {
8+
s := make([]interface{}, 0)
9+
return &Stack{s}
10+
}
11+
12+
func (s *Stack) Size() int {
13+
return len(s.array)
14+
}
15+
16+
func (s *Stack) Empty() bool {
17+
return len(s.array) == 0
18+
}
19+
20+
func (s *Stack) Push(value interface{}) {
21+
s.array = append(s.array, value)
22+
}
23+
func (s *Stack) Swap(i int, j int) bool {
24+
if i > s.Size()-1 || i < 0 {
25+
return false
26+
}
27+
if j > s.Size()-1 || j < 0 {
28+
return false
29+
}
30+
s.array[i], s.array[j] = s.array[j], s.array[i]
31+
32+
return true
33+
34+
}
35+
func (s *Stack) Pop() interface{} {
36+
stackLen := len(s.array)
37+
if stackLen == 0 {
38+
return nil
39+
}
40+
e := s.array[stackLen-1]
41+
s.array = s.array[:stackLen-1]
42+
43+
return e
44+
}
45+
func (s *Stack) RemoveAt(index int) bool {
46+
if index > s.Size()-1 || index < 0 {
47+
return false
48+
}
49+
if index < s.Size()-1 {
50+
s.array = append(s.array[:index], s.array[index+1:]...)
51+
} else {
52+
s.array = s.array[:index]
53+
}
54+
return true
55+
}
56+
57+
func (s *Stack) Erase(begin int, end int) bool {
58+
size := s.Size()
59+
if begin < 0 || end < 0 || begin >= end || begin > size || end > size {
60+
return false
61+
}
62+
for i := begin; i < end; i++ {
63+
if !s.RemoveAt(begin) {
64+
return false
65+
}
66+
}
67+
return true
68+
}
69+
70+
func (s *Stack) Insert(index int, value interface{}) bool {
71+
if index > s.Size()-1 || index < 0 {
72+
return false
73+
}
74+
75+
lastArray := make([]interface{}, 0)
76+
lastArray = append(lastArray, s.array[index:]...)
77+
s.array = s.array[:index]
78+
s.array = append(s.array, value)
79+
s.array = append(s.array, lastArray...)
80+
return true
81+
}
82+
83+
func (s *Stack) Top(i int) interface{} {
84+
stackLen := s.Size()
85+
if stackLen+i > stackLen-1 || stackLen+i < 0 {
86+
return nil
87+
}
88+
return s.array[stackLen+i]
89+
}
90+
91+
func (s *Stack) SetTop(i int, value interface{}) bool {
92+
stackLen := s.Size()
93+
if stackLen+i > stackLen-1 || stackLen+i < 0 {
94+
return false
95+
}
96+
s.array[stackLen+i] = value
97+
return true
98+
}
99+
100+
func (s *Stack) CountBool(val bool) int {
101+
var count int
102+
for _, e := range s.array {
103+
if v, ok := e.(bool); ok && v == val {
104+
count++
105+
}
106+
}
107+
return count
108+
}
109+
110+
func Swap(s *Stack, other *Stack) {
111+
if s.Size() == 0 && other.Size() == 0 {
112+
return
113+
}
114+
if other.Size() == 0 {
115+
other.array = s.array[:s.Size()]
116+
s.array = make([]interface{}, 0)
117+
}
118+
if s.Size() == 0 {
119+
s.array = other.array[:other.Size()]
120+
other.array = make([]interface{}, 0)
121+
}
122+
s.array, other.array = other.array, s.array
123+
124+
}
125+
126+
func (s *Stack) Copy() *Stack {
127+
bak := make([]interface{}, s.Size())
128+
copy(bak, s.array)
129+
130+
return &Stack{
131+
array: bak,
132+
}
133+
}
134+
135+
func (s *Stack) Equal(other *Stack) bool {
136+
if s.Size() != other.Size() {
137+
return false
138+
}
139+
for i := 0; i < s.Size(); i++ {
140+
if s.array[i] != other.array[i] {
141+
return false
142+
}
143+
}
144+
return true
145+
}

0 commit comments

Comments
 (0)