AdventOfCode2021/shared/Stack.go

34 lines
424 B
Go
Raw Normal View History

2021-12-16 21:11:39 +00:00
package shared
type Stack struct {
head *Node
}
func (s *Stack) Push(item string) {
if s.head == nil {
s.head = &Node{
Value: item,
Next: nil,
}
} else {
s.head = &Node{
Value: item,
Next: s.head,
}
}
}
func (s *Stack) Pop() string {
node := s.Peek()
s.head = node.Next
return node.Value
}
func (s *Stack) Peek() (node *Node) {
node = s.head
if node == nil {
node = &Node{}
}
return
}