Skip to content

GH-266: Stack in C++ #267

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

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions concepts/cpp/stack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Stack in C++

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: `Push`, which adds an element to the collection, and `Pop`, which removes the most recently added element that was not yet removed.

## Stack implementation in C++

C++ provides a standard template library (STL) `stack` class that implements the stack data structure. The `stack` class is defined in the `stack` header file. The `stack` class is a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO (last-in first-out) data structure.


```c++
#include <iostream>
#include <stack>

using namespace std;
```

**`push(item)`**: Adds an item to the top of the stack.


```c++
stack<int> st;
st.push(5);
st.push(10);
st.push(7);
```

**`pop()`**: Removes the top item from the stack.


```c++
st.pop();
```

**`top()`**: Returns the top of the stack.


```c++
st.top()
```




10



**`empty()`**: Returns true if and only if the stack is empty.


```c++
st.empty()
```




false



**`size()`**: Returns the number of items in the stack.


```c++
st.size()
```




2



## 🔗 Further Reading

* ▶️ [Stack Introduction](https://youtu.be/L3ud3rXpIxA?list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu), Data structures playlist, WilliamFiset, 2017
* ▶️ [Stack Implementation](https://www.youtube.com/watch?v=RAMqDLI6_1c&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=9&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017
* ▶️ [Stack Code](https://www.youtube.com/watch?v=oiZssCfk4_U&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=10&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017
es the most recently added element that was not yet removed.
208 changes: 208 additions & 0 deletions concepts/cpp/stack/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Stack in C++\n",
"\n",
"In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: `Push`, which adds an element to the collection, and `Pop`, which removes the most recently added element that was not yet removed."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stack implementation in C++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C++ provides a standard template library (STL) `stack` class that implements the stack data structure. The `stack` class is defined in the `stack` header file. The `stack` class is a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO (last-in first-out) data structure."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <stack>\n",
"\n",
"using namespace std;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`push(item)`**: Adds an item to the top of the stack."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"stack<int> st;\n",
"st.push(5);\n",
"st.push(10);\n",
"st.push(7);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`pop()`**: Removes the top item from the stack."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"st.pop();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`top()`**: Returns the top of the stack."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st.top()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`empty()`**: Returns true if and only if the stack is empty."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st.empty()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`size()`**: Returns the number of items in the stack."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st.size()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔗 Further Reading\n",
"\n",
"* ▶️ [Stack Introduction](https://youtu.be/L3ud3rXpIxA?list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu), Data structures playlist, WilliamFiset, 2017\n",
"* ▶️ [Stack Implementation](https://www.youtube.com/watch?v=RAMqDLI6_1c&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=9&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017\n",
"* ▶️ [Stack Code](https://www.youtube.com/watch?v=oiZssCfk4_U&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=10&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017\n",
"es the most recently added element that was not yet removed."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion concepts/general/stack/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Stack

*See implementation in*
C++,
[C++](/concepts/cpp/stack/README.md),
Java,
[Python](/concepts/python/stack.md),
[Typescript](/concepts/typescript/stack.md)
Expand Down
3 changes: 2 additions & 1 deletion readme/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ A data structure is a data organization, management, and storage format that is
<a href="/concepts/cpp/linked-list/README.md">Linked List</a>
</li>
<li>
<code>B</code> Stack
<code>B</code>
<a href="/concepts/cpp/stack/README.md">Stack</
</li>
<li>
<code>B</code>
Expand Down