Professional Documents
Culture Documents
In this example we will implement stack with the help of c++ class and object, in this
example (code snippet) stack will be implemented with following operations
1. Stack Initialization
2. Push Operation
3. Pop Operations
4. Check Empty
5. Check Full
6. Stack Traversing to Display Stack Items
#define SIZE 5
class STACK
{
private:
int num[SIZE];
int top;
public:
STACK(); //defualt constructor
int push(int);
int pop();
int isEmpty();
int isFull();
void displayItems();
};
STACK::STACK(){
top=-1;
}
int STACK::isEmpty(){
if(top==-1)
return 1;
else
return 0;
}
int STACK::isFull(){
if(top==(SIZE-1))
return 1;
else
return 0;
}
int STACK::pop(){
//to store and print which number
//is deleted
int temp;
//check for empty
if(isEmpty())
return 0;
temp=num[top];
--top;
return temp;
void STACK::displayItems(){
int i; //for loop
cout<<"STACK is: ";
for(i=(top); i>=0; i--)
cout<<num[i]<<" ";
cout<<endl;
}
int main(){
//declare object
STACK stk;
int choice, n,temp;
do
{
cout<<endl;
cout<<"0 - Exit."<<endl;
cout<<"1 - Push Item."<<endl;
cout<<"2 - Pop Item."<<endl;
cout<<"3 - Display Items (Print STACK)."<<endl;
switch(choice){
case 0: break;
case 1:
cout<<"Enter item to insert: ";
cin>>n;
temp=stk.push(n);
if(temp==0)
cout<<"STACK is FULL."<<endl;
else
cout<<temp<<" inserted."<<endl;
break;
case 2:
temp=stk.pop();
if(temp==0)
cout<<"STACK IS EMPTY."<<endl;
else
cout<<temp<<" is removed (popped)."<<endl;
break;
case 3:
stk.displayItems();
break;
default:
cout<<"An Invalid choice."<<endl;
}
}while(choice!=0);
return 0;
C++ Stack
In this tutorial, you will learn about the STL stack with the help of examples.
Create a Stack
In order to create a stack in C++, we first need to include the stack header file.
#include <stack>
Once we import this file, we can create a stack using the following syntax:
stack<type> st;
Here, type indicates the data type we want to store in the stack. For instance,
// create a stack of integers
stack<int> integer_stack;
int main() {
// create a stack of strings
stack<string> languages;
return 0;
}
Run Code
Output
Python
Stack Methods
In C++, the stack class provides various methods to perform different
operations on a stack.
Operation Description
int main() {
return 0;
}
Run Code
Output
Stack: Orange, Red,
Instead of directly printing the contents of the stack, we have used a while loop
and various stack methods.
while(!colors.empty()) {
cout << colors.top() << ", ";
colors.pop();
}
int main() {
return 0;
}
while(!st.empty()) {
cout << st.top() << ", ";
st.pop();
}
Output
Initial Stack: Blue, Orange, Red,
Final Stack: Orange, Red,
In the above example, we have used the pop() method to remove an element
from the stack.
Initially, the contents of the stack are {"Blue", "Orange", "Red"}.
Then we have used the pop() method to remove the element.
// removes top element
colors.pop()
This removes the element at the top of the stack i.e. the element inserted last,
which is "Blue".
Hence, the final stack becomes {"Orange", "Red"}.
int main() {
return 0;
}
Run Code
Output
Top Element: Blue
int main() {
return 0;
}
Run Code
Output
Size of the stack: 3
In the above example, we have created a stack of integers
called prime_nums and added three elements to it.
Then we have used the size() method to find the number of elements in the
stack:
prime_nums.size();
int main() {
return 0;
}
Run Code
Output
Is the stack empty? Yes
Pushing elements...
Is the stack empty? No