44 lines
720 B
Go
44 lines
720 B
Go
|
package ds
|
||
|
|
||
|
type ListStack[T any] struct {
|
||
|
list List[T]
|
||
|
}
|
||
|
|
||
|
var _ Stack[int] = &ListStack[int]{}
|
||
|
|
||
|
func NewListStack[T any](list List[T]) *ListStack[T] {
|
||
|
return &ListStack[T]{list}
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) Push(value T) {
|
||
|
s.list.Add(value)
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) Add(value T) {
|
||
|
s.Push(value)
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) AddAll(values Iterable[T]) {
|
||
|
values.Each(s.Add)
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) Pop() T {
|
||
|
return s.list.RemoveAt(s.list.Size() - 1)
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) Peek() T {
|
||
|
return s.PeekAt(s.Size() - 1)
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) PeekAt(index int) T {
|
||
|
return s.list.Get(index)
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) Size() int {
|
||
|
return s.list.Size()
|
||
|
}
|
||
|
|
||
|
func (s *ListStack[T]) Empty() bool {
|
||
|
return s.Size() == 0
|
||
|
}
|