Simple Generic Stack implementation in Go using linked list
s := gstack.New(1, 2, 3, 4)
fmt.Println(s.Len())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
s.Push(5)
fmt.Println(s.Peek())
// Output: 4
// 4
// 3
// 2
// 1
// 5
- minimal supported version of Go is 1.20
- initial implementation
- minimal supported version of Go is 1.23
- added
Iter
method to iterate over the stack and consume all items
s := gstack.New(1, 2, 3, 4)
for v := range s.Iter() {
fmt.Println(v)
}
fmt.Println("len:", s.Len())
// Output: 4
// 3
// 2
// 1
// len: 0
- added
IsEmpty
method to check if the stack is empty