2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-11 19:10:39 +00:00

go fmt for stack

This commit is contained in:
Alexander Kuleshov 2017-02-20 17:18:03 +06:00
parent a4d6be93c7
commit 307fadc6e2
No known key found for this signature in database
GPG Key ID: 11DB55FD14934DB6
2 changed files with 52 additions and 52 deletions

View File

@ -1,61 +1,61 @@
package stack
type Stack struct {
st []interface{}
len int
st []interface{}
len int
}
func New() *Stack {
stack := &Stack{}
stack.st = make([]interface{}, 1)
stack.len = 0
return stack
stack := &Stack{}
stack.st = make([]interface{}, 1)
stack.len = 0
return stack
}
func (stack *Stack) Length() int {
return stack.len
return stack.len
}
func (stack *Stack) Pop() {
stack.st = stack.st[1:]
stack.len -= 1
stack.st = stack.st[1:]
stack.len -= 1
}
func (stack *Stack) Peek() interface{} {
return stack.st[0]
return stack.st[0]
}
func (stack *Stack) IsEmpty() bool {
return (stack.len == 0)
return (stack.len == 0)
}
func (stack *Stack) Push(value interface{}) {
add(stack, value)
}
add(stack, value)
}
func add(slice *Stack, value interface{}) {
slice.len += 1
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]
}
slice.len += 1
var tmpSlice []interface{} = make([]interface{}, slice.len)
if slice.len == 0 {
slice.st[0] = value
return
}
if i == slice.len - 1 {
break
}
}
slice.st = tmpSlice
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
}

View File

@ -3,23 +3,23 @@ package stack
import "testing"
func Test_Stack(t *testing.T) {
stack := New()
stack := New()
stack.Push(5)
stack.Push(6)
stack.Push(7)
if stack.Length() != 3 {
t.Error("[Error] stack length is wrong")
}
stack.Push(5)
stack.Push(6)
stack.Push(7)
stack.Pop()
if stack.Length() != 3 {
t.Error("[Error] stack length is wrong")
}
if stack.Length() != 2 {
t.Error("[Error] stack length is wrong after pop")
}
if stack.Peek() != 6 {
t.Error("[Error] stack Peek is wrong")
}
stack.Pop()
if stack.Length() != 2 {
t.Error("[Error] stack length is wrong after pop")
}
if stack.Peek() != 6 {
t.Error("[Error] stack Peek is wrong")
}
}