C++ Program to Illustrate Usage of Stack Adaptor

This C++ program illustrates usage of a adaptor called stack. An adapter is a data type that adapts a container to provide specific interface. Stack provides facility to push elements into it and pop them.

Here is the source code of the C++ program illustrates usage of stack. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Illustrate Stack Adaptor
  3.  */
  4.  
  5. #include <iostream>
  6. #include <stack>
  7.  
  8. int main()
  9. {
  10.     std::stack< char > s;
  11.  
  12.     // Pushing elements into stack
  13.     for (char c = 'a'; c <= 'f'; c++)
  14.     {
  15.         s.push(c);
  16.     }
  17.     while (!s.empty())
  18.     {
  19.         std::cout << "Top element of stack \'" << s.top()
  20.                   << "\'" << std::endl;
  21.         s.pop();
  22.     }
  23.     std::cout << "Stack is empty!" << std::endl;
  24. }

$ ./a.out
Top element of stack 'f'
Top element of stack 'e'
Top element of stack 'd'
Top element of stack 'c'
Top element of stack 'b'
Top element of stack 'a'
Stack is empty!

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.