Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example for stack using C++ STL in "Stacks" #28814

Merged
merged 2 commits into from Jun 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions guide/english/computer-science/data-structures/stacks/index.md
Expand Up @@ -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<stack> and then use the C++ STL Library stack class.
### Stack implementation using array in C++
```cpp

#include <bits/stdc++.h>

Expand Down Expand Up @@ -65,6 +64,29 @@ int main() {
return 0;
}
```
### Stack implementation using STL in C++
```cpp
#include <iostream>
#include <stack>

using namespace std;

int main() {
stack<int> 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

Expand Down