MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Peek

Method Peek

structure/stack/stackarray.go:32–38  ·  view source on GitHub ↗

Peek returns the top element of the stack without removing it.

()

Source from the content-addressed store, hash-verified

30
31// Peek returns the top element of the stack without removing it.
32func (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.
41func (s *Array[T]) IsEmpty() bool {

Callers

nothing calls this directly

Calls 1

IsEmptyMethod · 0.95

Tested by

no test coverage detected