A stack is a data structure that follows LIFO, i.e. last in first out manner. Stack has only one end from which we can add elements and remove them. Also, we can perform various operations on the stack. The memory allocation in the stack is contiguous.
Table of Contents
How to create stack in C++?
In C++, we can create a linked list and convert it into the stack. Or C++ provides a stack header file in which all the functions are predefined. We just need to call them and use them properly.
Operations on the stack
- Push – This operation is used to store the new element in the stack.
- Pop – Pop is used to remove an element from the top of the stack
Predefined functions in the stack header file
There are five predefined functions is the stack header file.
- empty() – This function returns true if the stack is empty and false if the stack contains any element.
- top() – This function will return the last element present on the top of the stack without removing it from the stack.
- push(e) – This function is used to add the element in the stack. Pass the element as an argument to this function.
- pop() – This function is used to remove an element from the stack. It will return the element present at the top of the stack.
- size() – This function will return the count of elements present in the stack.
How to use stack in C++
In C++, the stack header file is already present. To do all the operations like push, pop, etc., the predefined functions are already there. You just need to call them and perform operations on it.
If you are not using the C++ stack header file, you need to declare functions for pop and push operations. The example, in this case, is given below.
C++ stack examples are given below
1. Program to implement a stack without using C++ stack library.
In this program, We are going to declare stack named class in C++ and all the functions manually which will perform all the operations of the stack in it. Let’s understand it with code.
Example
#include <iostream>
using namespace std;
class stack
{
int stack[100], n=100, top=-1;
public:
void isempty()
{
if(top==-1)
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
}
void size()
{
int cnt = 0;
for(int i=top; i>=0 ; i--)
cnt++;
cout<<"Size of stack is:"<<cnt<<endl;
}
void push(int val)
{
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else
{
top++;
stack[top]=val;
}
}
void pop()
{
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<"Stack is empty";
}
};
int main() {
stack st;
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Check stack is empty or not"<<endl;
cout<<"5) Size of stack"<<endl;
cout<<"6) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
st.push(val);
break;
}
case 2: {
st.pop();
break;
}
case 3: {
st.display();
break;
}
case 4: {
st.isempty();
break;
}
case 5: {
st.size();
break;
}
case 6: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=6);
return 0;
}
Output
1) Push in stack
2) Pop from stack
3) Display stack
4) Check stack is empty or not
5) Size of stack
6) Exit
Enter choice:
1
Enter value to be pushed:
8
Enter choice:
1
Enter value to be pushed:
7
Enter choice:
1
Enter value to be pushed:
5
Enter choice:
1
Enter value to be pushed:
9
Enter choice:
5
Size of stack is:4
Enter choice:
2
The popped element is 9
Enter choice:
2
The popped element is 5
Enter choice:
2
The popped element is 7
Enter choice:
4
Stack is not empty
Enter choice:
6
Exit
2. Program to implement a stack using C++ stack library.
In this program, we have used the stack header file. Using the switch case we are performing all the operations on the stack. The stack pop in C++ header file does not return any value that’s why we have just called the function. The only function that returns a value is stack pop in C++ header file. Let’s see what is the output of the below example.
Example
#include <iostream>
#include <stack>
using namespace std;
class stack1
{
stack<int> st;
public:
void isemt()
{
if(st.empty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
}
void stksize()
{
cout<<"Size of stack is:"<<st.size()<<endl;
}
void stkpush(int val)
{
st.push(val);
}
void stkpop()
{
st.pop();
cout<<"The element is popped "<<endl;
}
void display()
{
while (!st.empty())
{
cout << ' ' << st.top();
st.pop();
}
cout << endl;
}
};
int main() {
stack1 stk;
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Check stack is empty or not"<<endl;
cout<<"5) Size of stack"<<endl;
cout<<"6) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
stk.stkpush(val);
break;
}
case 2: {
stk.stkpop();
break;
}
case 3: {
stk.display();
break;
}
case 4: {
stk.isemt();
break;
}
case 5: {
stk.stksize();
break;
}
case 6: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=6);
return 0;
}
Output
1) Push in stack
2) Pop from stack
3) Display stack
4) Check stack is empty or not
5) Size of stack
6) Exit
Enter choice:
1
Enter value to be pushed:
4
Enter choice:
1
Enter value to be pushed:
7
Enter choice:
1
Enter value to be pushed:
8
Enter choice:
5
Size of stack is:3
Enter choice:
3
8 7 4
Enter choice:
4
Stack is empty
Enter choice:
6
Exit
Using the stack header file, create an object of the temple.
stack<type> obj_name;
If you are not using a header file, then create an array and treat it as a stack.
To initialize a stack, add an element in the stack, It will automatically initialize.
Did you know? |
---|
1. For Loop In C++ |
2. Vectors In C++ |
3. Difference Between C and C++ |
4. C++ Swap |
5. C++ List |
6. C++ Hello World |
7. C++ Map |
8. Converting String To Int C++ |
9. Stack C++ |
10. Queue C++ |