ds/impl_stack_list.go

48 lines
826 B
Go
Raw Normal View History

2023-01-20 21:48:46 +01:00
package ds
type ListStack[T any, L List[T]] struct {
list L
2023-01-20 21:48:46 +01:00
}
var _ Stack[int] = &ListStack[int, List[int]]{}
2023-01-20 21:48:46 +01:00
func NewListStack[T any, L List[T]](list L) *ListStack[T, L] {
return &ListStack[T, L]{list}
2023-01-20 21:48:46 +01:00
}
func (s *ListStack[T, L]) Push(value T) {
2023-01-20 21:48:46 +01:00
s.list.Add(value)
}
func (s *ListStack[T, L]) Add(value T) {
2023-01-20 21:48:46 +01:00
s.Push(value)
}
func (s *ListStack[T, L]) AddAll(values Iterable[T]) {
2023-01-20 21:48:46 +01:00
values.Each(s.Add)
}
func (s *ListStack[T, L]) Pop() T {
2023-01-20 21:48:46 +01:00
return s.list.RemoveAt(s.list.Size() - 1)
}
func (s *ListStack[T, L]) Peek() T {
2023-01-20 21:48:46 +01:00
return s.PeekAt(s.Size() - 1)
}
func (s *ListStack[T, L]) PeekAt(index int) T {
2023-01-20 21:48:46 +01:00
return s.list.Get(index)
}
func (s *ListStack[T, L]) Size() int {
2023-01-20 21:48:46 +01:00
return s.list.Size()
}
func (s *ListStack[T, L]) Empty() bool {
2023-01-20 21:48:46 +01:00
return s.Size() == 0
}
func (s *ListStack[T, L]) Clear() {
s.list.Clear()
}