You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-algorithms/stack/stack.go

62 lines
1.0 KiB
Go

package stack
type Stack struct {
10 years ago
st []interface{}
len int
}
func New() *Stack {
10 years ago
stack := &Stack{}
stack.st = make([]interface{}, 1)
stack.len = 0
return stack
}
func (stack *Stack) Length() int {
10 years ago
return stack.len
}
func (stack *Stack) Pop() {
10 years ago
stack.st = stack.st[1:]
stack.len -= 1
}
func (stack *Stack) Peek() interface{} {
return stack.st[0]
}
func (stack *Stack) IsEmpty() bool {
10 years ago
return (stack.len == 0)
}
func (stack *Stack) Push(value interface{}) {
10 years ago
add(stack, value)
}
func add(slice *Stack, value interface{}) {
8 years ago
slice.len += 1
10 years ago
var tmpSlice []interface{} = make([]interface{}, slice.len)
if slice.len == 0 {
slice.st[0] = value
return
}
for i:=0; i < slice.len; i++ {
tmpSlice[i] = 0
}
for i:=0; i < slice.len; i++ {
if i == 0 {
tmpSlice[0] = value
} else {
tmpSlice[i] = slice.st[i - 1]
}
if i == slice.len - 1 {
break
}
}
slice.st = tmpSlice
}