Skip to content

Commit b59be09

Browse files
authored
GH-266: Stack in C++ (#267)
1 parent c4bec38 commit b59be09

File tree

4 files changed

+292
-2
lines changed

4 files changed

+292
-2
lines changed

concepts/cpp/stack/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Stack in C++
2+
3+
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.
4+
5+
## Stack implementation in C++
6+
7+
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.
8+
9+
10+
```c++
11+
#include <iostream>
12+
#include <stack>
13+
14+
using namespace std;
15+
```
16+
17+
**`push(item)`**: Adds an item to the top of the stack.
18+
19+
20+
```c++
21+
stack<int> st;
22+
st.push(5);
23+
st.push(10);
24+
st.push(7);
25+
```
26+
27+
**`pop()`**: Removes the top item from the stack.
28+
29+
30+
```c++
31+
st.pop();
32+
```
33+
34+
**`top()`**: Returns the top of the stack.
35+
36+
37+
```c++
38+
st.top()
39+
```
40+
41+
42+
43+
44+
10
45+
46+
47+
48+
**`empty()`**: Returns true if and only if the stack is empty.
49+
50+
51+
```c++
52+
st.empty()
53+
```
54+
55+
56+
57+
58+
false
59+
60+
61+
62+
**`size()`**: Returns the number of items in the stack.
63+
64+
65+
```c++
66+
st.size()
67+
```
68+
69+
70+
71+
72+
2
73+
74+
75+
76+
## 🔗 Further Reading
77+
78+
* ▶️ [Stack Introduction](https://youtu.be/L3ud3rXpIxA?list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu), Data structures playlist, WilliamFiset, 2017
79+
* ▶️ [Stack Implementation](https://www.youtube.com/watch?v=RAMqDLI6_1c&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=9&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017
80+
* ▶️ [Stack Code](https://www.youtube.com/watch?v=oiZssCfk4_U&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=10&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017
81+
es the most recently added element that was not yet removed.

concepts/cpp/stack/notebook.ipynb

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Stack in C++\n",
8+
"\n",
9+
"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."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"## Stack implementation in C++"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"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."
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 1,
29+
"metadata": {
30+
"vscode": {
31+
"languageId": "cpp"
32+
}
33+
},
34+
"outputs": [],
35+
"source": [
36+
"#include <iostream>\n",
37+
"#include <stack>\n",
38+
"\n",
39+
"using namespace std;"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"**`push(item)`**: Adds an item to the top of the stack."
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 5,
52+
"metadata": {
53+
"vscode": {
54+
"languageId": "cpp"
55+
}
56+
},
57+
"outputs": [],
58+
"source": [
59+
"stack<int> st;\n",
60+
"st.push(5);\n",
61+
"st.push(10);\n",
62+
"st.push(7);"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"**`pop()`**: Removes the top item from the stack."
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 6,
75+
"metadata": {
76+
"vscode": {
77+
"languageId": "cpp"
78+
}
79+
},
80+
"outputs": [],
81+
"source": [
82+
"st.pop();"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"**`top()`**: Returns the top of the stack."
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 7,
95+
"metadata": {
96+
"vscode": {
97+
"languageId": "cpp"
98+
}
99+
},
100+
"outputs": [
101+
{
102+
"data": {
103+
"text/plain": [
104+
"10"
105+
]
106+
},
107+
"execution_count": 7,
108+
"metadata": {},
109+
"output_type": "execute_result"
110+
}
111+
],
112+
"source": [
113+
"st.top()"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"**`empty()`**: Returns true if and only if the stack is empty."
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 8,
126+
"metadata": {
127+
"vscode": {
128+
"languageId": "cpp"
129+
}
130+
},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"false"
136+
]
137+
},
138+
"execution_count": 8,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"st.empty()"
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {},
150+
"source": [
151+
"**`size()`**: Returns the number of items in the stack."
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 9,
157+
"metadata": {
158+
"vscode": {
159+
"languageId": "cpp"
160+
}
161+
},
162+
"outputs": [
163+
{
164+
"data": {
165+
"text/plain": [
166+
"2"
167+
]
168+
},
169+
"execution_count": 9,
170+
"metadata": {},
171+
"output_type": "execute_result"
172+
}
173+
],
174+
"source": [
175+
"st.size()"
176+
]
177+
},
178+
{
179+
"cell_type": "markdown",
180+
"metadata": {},
181+
"source": [
182+
"## 🔗 Further Reading\n",
183+
"\n",
184+
"* ▶️ [Stack Introduction](https://youtu.be/L3ud3rXpIxA?list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu), Data structures playlist, WilliamFiset, 2017\n",
185+
"* ▶️ [Stack Implementation](https://www.youtube.com/watch?v=RAMqDLI6_1c&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=9&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017\n",
186+
"* ▶️ [Stack Code](https://www.youtube.com/watch?v=oiZssCfk4_U&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=10&ab_channel=WilliamFiset), Data structures playlist, WilliamFiset, 2017\n",
187+
"es the most recently added element that was not yet removed."
188+
]
189+
}
190+
],
191+
"metadata": {
192+
"kernelspec": {
193+
"display_name": "C++17",
194+
"language": "C++17",
195+
"name": "xcpp17"
196+
},
197+
"language_info": {
198+
"codemirror_mode": "text/x-c++src",
199+
"file_extension": ".cpp",
200+
"mimetype": "text/x-c++src",
201+
"name": "c++",
202+
"version": "17"
203+
},
204+
"orig_nbformat": 4
205+
},
206+
"nbformat": 4,
207+
"nbformat_minor": 2
208+
}

concepts/general/stack/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Stack
22

33
*See implementation in*
4-
C++,
4+
[C++](/concepts/cpp/stack/README.md),
55
Java,
66
[Python](/concepts/python/stack.md),
77
[Typescript](/concepts/typescript/stack.md)

readme/cpp/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ A data structure is a data organization, management, and storage format that is
3030
<a href="/concepts/cpp/linked-list/README.md">Linked List</a>
3131
</li>
3232
<li>
33-
<code>B</code> Stack
33+
<code>B</code>
34+
<a href="/concepts/cpp/stack/README.md">Stack</
3435
</li>
3536
<li>
3637
<code>B</code>

0 commit comments

Comments
 (0)