2023-01-20 21:48:46 +01:00
|
|
|
package ds
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
type ListStack[T any, L List[T]] struct {
|
|
|
|
list L
|
2023-01-20 21:48:46 +01:00
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
var _ Stack[int] = &ListStack[int, List[int]]{}
|
2023-01-20 21:48:46 +01:00
|
|
|
|
2023-07-10 17:16:41 +02: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
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Push(value T) {
|
2023-01-20 21:48:46 +01:00
|
|
|
s.list.Add(value)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Add(value T) {
|
2023-01-20 21:48:46 +01:00
|
|
|
s.Push(value)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) AddAll(values Iterable[T]) {
|
2023-01-20 21:48:46 +01:00
|
|
|
values.Each(s.Add)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Pop() T {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.list.RemoveAt(s.list.Size() - 1)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Peek() T {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.PeekAt(s.Size() - 1)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) PeekAt(index int) T {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.list.Get(index)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Size() int {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.list.Size()
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Empty() bool {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.Size() == 0
|
|
|
|
}
|
2023-01-20 22:12:17 +01:00
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListStack[T, L]) Clear() {
|
2023-01-20 22:12:17 +01:00
|
|
|
s.list.Clear()
|
|
|
|
}
|