Peek returns the top element of the stack without removing it.
()
| 30 | |
| 31 | // Peek returns the top element of the stack without removing it. |
| 32 | func (s *Array[T]) Peek() T { |
| 33 | if s.IsEmpty() { |
| 34 | var zeroValue T |
| 35 | return zeroValue // Stack is empty |
| 36 | } |
| 37 | return s.elements[len(s.elements)-1] |
| 38 | } |
| 39 | |
| 40 | // IsEmpty returns true if the stack is empty, false otherwise. |
| 41 | func (s *Array[T]) IsEmpty() bool { |