Q:

Stack program using C++ Standard Template Library (STL)

belongs to collection: C++ programs on various topics

0

Stack program using C++ Standard Template Library (STL)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to implement stack using C++ STL

#include <iostream>
#include <stack>

using namespace std;

//function to dispay stack
void dispStack(stack <int> st){
    //declare temp. statck
    stack <int> s = st;
    while(!s.empty()){
        cout<<s.top()<<" ";
        s.pop();
    }
    cout<<endl;
}

//Main function for stack program
int main()
{
   //declare stack variable
   stack <int> st;
   
   //insert elements
   st.push(10);
   st.push(20);
   st.push(30);
   st.push(40);
   st.push(50);
   
   //display stack coun, top element and stack elements
   cout<<"Total stack elements are: "<<st.size()<<endl;
   cout<<"Top elements is: "<<st.top()<<endl;
   cout<<"All stack elements are: "<<endl;
   dispStack(st);
   
   //removing two stack elements
   st.pop();
   st.pop();
   
   cout<<"\nAfter removing two elements...\n";
    
    //AGAIN....
   //display stack coun, top element and stack elements
   cout<<"Total stack elements are: "<<st.size()<<endl;
   cout<<"Top elements is: "<<st.top()<<endl;
   cout<<"All stack elements are: "<<endl;
   dispStack(st);   
   
   return 0;
}

Output

 
Total stack elements are: 5
Top elements is: 50
All stack elements are: 
50 40 30 20 10 

After removing two elements...
Total stack elements are: 3
Top elements is: 30
All stack elements are: 
30 20 10 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to find the integers which come odd nu... >>
<< Binary Search in C++ using Standard Template Libra...