Skip to content

Commit

Permalink
fix bugs of array stack
Browse files Browse the repository at this point in the history
  • Loading branch information
scissorsfeet committed Oct 10, 2018
1 parent e2b841f commit a9dff3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion go/08_stack/StackBasedOnArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ func (this *ArrayStack) IsEmpty() bool {
}

func (this *ArrayStack) Push(v interface{}) {
this.data = append(this.data, v)
if this.top < 0 {
this.top = 0
} else {
this.top += 1
}

if this.top > len(this.data)-1 {
this.data = append(this.data, v)
} else {
this.data[this.top] = v
}
}

func (this *ArrayStack) Pop() interface{} {
Expand Down
5 changes: 5 additions & 0 deletions go/08_stack/StackBasedOnArray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ func TestArrayStack_Push(t *testing.T) {
s := NewArrayStack()
s.Push(1)
s.Push(2)
t.Log(s.Pop())
s.Push(3)
t.Log(s.Pop())
t.Log(s.Pop())
s.Push(4)
t.Log(s.Pop())
s.Print()
}

Expand Down

0 comments on commit a9dff3d

Please sign in to comment.