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.
/*
* C++ Program to Illustrate Stack Adaptor
*/
#include <iostream>
#include <stack>
int main()
{
std::stack< char > s;
// Pushing elements into stack
for (char c = 'a'; c <= 'f'; c++)
{
s.push(c);
}
while (!s.empty())
{
std::cout << "Top element of stack \'" << s.top()
<< "\'" << std::endl;
s.pop();
}
std::cout << "Stack is empty!" << std::endl;
}
$ ./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.
Related Posts:
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Check C++ Books
- Apply for Computer Science Internship