|
| 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 | +} |
0 commit comments