From 9252cbb998cfe25738a4120485674c75ff94ab55 Mon Sep 17 00:00:00 2001 From: Kyle Lobo Date: Wed, 12 Jun 2019 07:14:42 +0530 Subject: [PATCH] Added example for stack using C++ STL in "Stacks" (#28814) * Added example for stack using C++ STL in "Stacks" * fix: changed c++ to cpp --- .../data-structures/stacks/index.md | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/guide/english/computer-science/data-structures/stacks/index.md b/guide/english/computer-science/data-structures/stacks/index.md index 5fa5b45803..bb9a8f8147 100644 --- a/guide/english/computer-science/data-structures/stacks/index.md +++ b/guide/english/computer-science/data-structures/stacks/index.md @@ -16,9 +16,8 @@ Some basics operations of stack are: Implementation of a stack is possible using either arrays or linked lists. The following is a simple array implementation of the stack data structure with its most common operations. -```C++ -//Stack implementation using array in C++ -//You can also include and then use the C++ STL Library stack class. +### Stack implementation using array in C++ +```cpp #include @@ -65,6 +64,29 @@ int main() { return 0; } ``` +### Stack implementation using STL in C++ +```cpp +#include +#include + +using namespace std; + +int main() { + stack s; + s.push(10); + s.push(20); + s.push(30); + cout << "The size of the stack is: " << s.size() << endl; + cout << "Top of the stack is: " << s.top() << endl; + cout << "The stack is: " << endl; + while (!s.empty()) { + cout << s.top() << " "; + s.pop(); + } + cout << endl; +} +``` + #### Using Arrays as Stacks