Stacks

DSA

In my last post, I talked about dynamic arrays. One use of a dynamic array is to implement a stack. Stacks are LIFO, dynamic in size, and supports three operations — push, pop, and peek. Pushing pushes an element to the top of the stack. Say we push items from an array into a stack

Figure 1

As we push objects to the top, we follow the LIFO principle. Likewise, we can pop elements as long as we follow the same principle.

Figure 2

And finally peek is where we return the top element without removing it from the stack.

Previous
Previous

Baseball Game

Next
Next

Concatenation of Array