Create Stack in C++

A stack is a basic data structure that acts as a linear list containing its elements. In this case, the item is added at one end of the list, known as the top, and items are removed from the same side. This means that the element entered in the first position will be removed at the end. We can create, delete, or update the elements.

New stack creation

To create a new stack, we must include the stack library first to execute all the functions applied to the stack.

Template <class Type, class Container = deque <Type> > class stack

The values present in the syntax are ‘type’ that shows the type of element present in the stack. It can be any type like integer, float, etc. The second one is the ‘container’ that is the type of object of container currently under-used.

Operations of stack

The primary operations of the stack are explained below:

  • Push: The push() function enters the elements in the stack. It checks first if the stack is already full, then this condition is known as an overflow condition.
  • Pop: This pop() function removes the element from the stack. At a time, only a single item is removed from the stack. The items are removed in the reverse order in which they were entered by the Push() function. The situation of being an empty stack is known to be an underflow stack.
  • Peek or Top: This function returns the element that is the top element in the stack.
  • isEmpty: It is a Boolean expression that returns true if the stack is already empty, but if it is not empty, this function returns false.

Stack applications

Redo-undo feature is very common among text editors or photo editors like Photoshop, and MS Word is an example of the stack.

While using a web browser, we see the forward and the backward options for the recently closed pages.

Stack is also used as memory management; modern computers can use the stack as primary management for the running programs.

Working techniques / Algorithm of Stack

  • A pointer that is called TOP is used to take the record of the element that is at the top of the stack.
  • We have an empty stack at the initial stage, so the top is set at a -1 position. The reason for doing this is that the emptiness of the stack is checked easily. This is done by comparing it with TOP == 1.
  • The next step is to push the item, so at that point, we increase the TOP value and then place the new item in the position pointed by the TOP.
  • In the case of applying the POP() function, we return the element that is pointed by the TOP, and then the current value of the TOP is reduced.
  • Two things should be checked at the time of pushing and popping elements. Similarly, before popping, we checked if the stack was empty or not.

Implementation of Stack

Example 1

As described above, before starting the main program, we need to add the stack library in the header file of our program.

#include <stack>

This library contains all the operations and associated functions, so it should be used. We have used the namespace std to use all the classes without calling. In the main program, we applied a simple logic to demonstrate each stack operation in a single line.

We have created a stack to store the values of integer data types.

Stack <int> st.

To enter the values in the stack, we have manually used the push() function. Every time this function will be called by the object that we create. We use push () to enter the values starting from 50 to 80. After insertion, we need to pop out the value by using the pop(). By using this function, the top element from the stack that is 80 will be removed, and now 70 will become the top element. Using the pop() function again, we will remove the 70 number, and now the top element is 60. In the end, we use the while loop to ensure that the stack is full. If it is true, then the pop() function is applied. The while loop body is ended.

Utilize G++ compiler to compile and execute the source code. “Stack.c” is the name of a file.

$ g++ -o stack stack.c.

$ ./stack

You can see that when the program is executed, both the values that were entered at the end are removed from the stack by working on the LIFO technique.

Example 2

Moving forward towards the second example, this involves user interaction. All the stack operations are applied separately in this program. We also display all the elements of the stack. In the main program, each function is called according to the value that the user enters during the execution. Now starting from the first operation of the stack by using namespace std, the function gets started. Here we have declared the stack globally with the integer data type of 100 lengths of elements. The push function receives the value from the main program that the user will enter. Inside the function, the if-else statement is used to check if the stack is not full. If the stack is not empty, a message is displayed to the user; else, the value is inserted. And the top value is increased.

Similarly, in the case of the pop() function, the top value is checked if it is below -1 location means the stack is empty, so the message is displayed else, the value is pop out.

We use a ‘FOR’ loop to show all the elements inserted by push() in the stack to display all the elements.

A user-friendly menu is created in the main program to get the user option.

4 options are displayed. If the user selects 1st, then this will be the push function. For this purpose, we have used a switch statement. The compiler passes the entered choice, and the program is executed.

After that, execute the code; now, you will see a menu that appears on successful code execution. First, we will select 1st option to insert values. The values will be inserted the first four times, and then we will display all the values by selecting option number 3.

All the values will be displayed here. Now we need to pop out the last value we have entered. So select option 2. This will remove the top value. Again selecting the pop option will again remove the topmost value.


The article ‘Create stack in C++’ involves the Linux operating system to implement the program in the C++ programming language. The current guide contains the basic usage and declaration of the stack in C++. We have used two examples that involve the operation of the stack. Some daily routine examples of the stack are also mentioned in this article.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.