fix test, add string unit test, refactor stack
All checks were successful
/ test (push) Successful in 56s
All checks were successful
/ test (push) Successful in 56s
This commit is contained in:
parent
b95d2954ba
commit
beaa325c7b
3 changed files with 37 additions and 20 deletions
|
|
@ -17,18 +17,23 @@ func NewStack[T any](capacity Pos) *Stack[T] {
|
|||
|
||||
func (s *Stack[T]) Push(items ...T) {
|
||||
for _, item := range items {
|
||||
if s.Current >= s.Capacity {
|
||||
panic("stack overflow")
|
||||
}
|
||||
if int(s.Current) == len(s.items) {
|
||||
s.items = append(s.items, item)
|
||||
}
|
||||
|
||||
s.items[s.Current] = item
|
||||
s.Current++
|
||||
s.pushItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack[T]) pushItem(item T) {
|
||||
if s.Current >= s.Capacity {
|
||||
panic("stack overflow")
|
||||
}
|
||||
if int(s.Current) == len(s.items) {
|
||||
s.items = append(s.items, item)
|
||||
} else {
|
||||
s.items[s.Current] = item
|
||||
}
|
||||
|
||||
s.Current++
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Pop() T {
|
||||
if s.Current <= 0 {
|
||||
panic("stack underflow")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue