Stack (Implementation)

In this lesson, we are going to look at how Stacks are implemented in C++ and how the main Stack functions actually work.

Introduction #

Most programming languages come with the Stack data structure built-in. In C++, you can use the pre-built Stack class by importing it into your program. However, implementing a stack from scratch will allow you to truly master the ins and outs of the data structure.

Implementation #

Stacks can be implemented using Arrays or Linked Lists. Each implementation has its own advantages and disadvantages. Here, however, we will show an implementation of stacks using arrays.

As mentioned in the previous lesson, a typical Stack must contain the following functions:

  • push(value)
  • pop()
  • isEmpty()
  • getTop()

We will take a close look at these functions individually but before we do, let’s start with constructing a Stack class.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.