Stack data structure with C++

A stack is a data structure based on the concept of LIFO (Last In, First Out), which is a simple yet potent idea in computing. With it's two operations pop and push we are given the opportunity to control our stack and the elements we want to place in it. For adding new elements we can say that they're being pushed onto the top of the stack. Whilst if we want to remove something, the first element is popped from the stack.

Let's see some code for this in C++ with integers and try to break it up.

We can start out by creating a class for our stack.

class Stack {
public:
       void push(int n);
       void pop();
       void top();
       void size();
       bool is_empty();
private:
       int stack[100];
       int stack_size = 0;
};

From there we can start breaking up each function and variable inside the class. Going into the private section of the class we can see that we have two variables, one being an array and the other an integer. We have set the array to contain a size of 10 integers, while the variable being 0.

This means that we're creating a stack that can contain 10 integers. Furthermore, we're going to use the stack_size variable to measure the size of the stack. So if we add the integers 1, 2 and 4 onto the stack, our stack_size should be equal to 3.

Now that we have that sorted we can start with the creation of the functions for the stack. Let's start out with the push function.


void push(int n) {
       if (stack_size == 100) {
             std::cout << "Can't push to stack. Stack is full." << std::endl;
       }
       else {
             stack[stack_size] = n;
             stack_size++;
       }
}

What happens here is that we are first taking in a parameter n through our function. This will be the element we want to add to the stack. From there, we need to check if the stack is full. If it is, we will not be able to add anything but rather sending out a message about the stack being full. However, if the stack is not full we will have to add an element on the space of stack_size. From there we can increase the size of stack_size, which makes us able to repeat the process.

So what is really happening here?

Let's imagine it with stack_size being equal to 0 and that we want to push the integer 5 onto the stack. Now, since the stack_size isn't equal to 10, we will just move to the else statement directly. From there we will set 5 equal to the index 0 of the array called stack. We will then move on by increasing the stack_size by one.

Now that we have all that sorted, let's move on to the pop function.

void pop() {
       if (is_empty()) {
             std::cout << "Cannot pop from an empty stack." << std::endl;
       }
       else {
             int element = stack[stack_size - 1];
             stack_size--;
             std::cout << "Popped " << element << " from stack." << std::endl;
       }
}

Here we can see that we're first checking if the stack is empty. This is from a boolean function that we haven't gotten to yet, but we can in the meanwhile imagine that it's what it does. From there, we go into an else-statement where we will create a local element variable out of the element we want to pop. For the actual popping, we will be using stack_size-- which decreases the size of the stack by one, and therefor removing the first element that was pushed into the stack. Now, we can send a message about the element being popped.

From there, let's take a look that is_empty() function.


bool is_empty() {
       if (stack_size == 0) {
             return true;
       }
       else {
             return false;
       }
}

This one is pretty straight forward. If the stack_size is equal to 0, then the stack must be empty. If not, then it must have some elements in it.

We can now create a function that will display the top of the stack.


void top() {
       if (is_empty()) {
             std::cout << "Can't display top from empty stack." << std::endl;
       }
       else {
             int element = stack[stack_size - 1];
             std::cout << "Top of the stack is " << element << std::endl;
       }
}
 

With this one, we can create a local variable called element, from there we can set it equal to stack[stack_size]. This will let us send out a message with the element being the top of the stack.

Lastly, we can create a function for displaying the size of the stack.


void size() {
       std::cout << "Size is " << stack_size << std::endl;
}


This one is straight forward. It'll just send out a message about the stack_size we've been working with so far.

As the end of this little post, let's take a look at how this would look in main.


int main(){
       Stack lifo;

       lifo.push(5); // Adds 5
       lifo.push(4); // Adds 4
       lifo.push(3); // Adds 3
       lifo.push(2); // Adds 2
       lifo.push(1); // Adds 1

       lifo.size(); // The size shoule be 5

       lifo.top(); // The top should be 1

       lifo.pop(); // Removes 1
       lifo.pop(); // Removes 2
       lifo.pop(); // Removes 3

       lifo.top(); // The top should now be 4

       return 0;
}

Comments

Popular posts from this blog

Vigenère cipher encryption with C++

Minimum Spanning Trees (MST)