Data structure: Stack

June Kang
3 min readAug 21, 2021

While furthering my journey to be a Software Engineer, there are many data structures and algorithms that I need to learn. One of the most important data structure is stack.

What is a stack? Stack is an abstract data type that’s commonly used in programming languages. The reason why it’s called stack is because it acts like a real-world stack. Stack only allows operations only at one end of the data. Just imagine a deck of cards where you can only see the top of the deck and do operations with the cards only at the top. This type of data structure is represented as LIFO(Last In First Out) data structure.

Stacks can be used in arrays or linked list, but the most optimal way to use stacks is linked list because arrays have a fixed size, which means it isn’t dynamic. The runtime it takes for arrays depends on the specific location of the element in the index of the array hence, the data cannot be overwritten unlike linked list.

There are few operations that can be done on a stack. Some of these operations are: pop(), push(), isEmpty(), isFull(), and peek(). These operations allow us to manipulate the data or examine the data. Some operations are faster in linked list (inserting/deleting) and some are faster in arrays (modifying elements) due to memory allocations in the data structures.

Pop

The pop() operation allows us to remove the first element of the data. You won’t need to put in any value in the parenthesis because it will know to remove the very top element of the stack.

Push

The push() operation enables us to add an element to the very top of the data. Again, nothing goes in the parenthesis because it knows to put the element on top of the stack.

IsEmpty & isFull

isFull function
isEmpty function

isEmpty and isFull operations will let us know if the data structure is empty or maxed. Using this function will yield either true or false.

Peek

The peek operation will return the first element in the data structure without removing it. This function will let us check the most recent input of our data within the array/linked list.

These functions make sure that we complete whatever actions we need to before moving on to the next element. They are type of recursions (process of breaking down complex problems into smaller ones) that can be used to grow or shrink the data. Stack is best used as a memory management, making sure you’re not overwhelming the system with duplicate data or too much data in the storage.

I will be posting other posts on data structures and algorithms so stay tuned for more!

--

--