We've already talked about how stacks store elements using the LIFO method. Now, we need to look at how elements are accessed in a familiar but different collection type – by peeking and popping:
- The Peek method returns the next item on the stack without removing it, letting you "peek" at it without changing anything.
- The Pop method returns and removes the next item on the stack, essentially "popping" it off and handing it to you.
Both of these methods can be used by themselves or together depending on what you need. You'll get hands-on experience with both methods in the following section.