Menu
Coddy logo textTech

What is Stack?

Lesson 2 of 13 in Coddy's Stack - Data Structures Series #1 course.

Think of a stack like a stack of books. You can add a book to the top of the stack, or remove the top book from the stack.

This is similar to how a stack data structure works in computer science. You can add an item to the top of the stack, and then remove that item from the top of the stack. This type of data structure follows a "last in, first out" (LIFO) order, which means that the last item added to the stack will be the first one removed. Stacks are often used in computer programs to temporarily store information, such as function calls or memory addresses.

 

The five main operations in a stack are:

  1. Push: Add an element to the top of the stack.
  2. Pop: Remove the top element from the stack.
  3. Peek/Top: Return the value of the top element without removing it.
  4. Size: Return the number of elements in the stack.
  5. IsEmpty: Check if the stack is empty.

 

Let's create a Stack class!

Try it yourself

This lesson doesn't include a code challenge.

All lessons in Stack - Data Structures Series #1