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