Skip to content

Instantly share code, notes, and snippets.

@Lucas-Severo
Created December 26, 2019 13:34
Show Gist options
  • Save Lucas-Severo/eae3564358fcefa828f85b0e3e165b27 to your computer and use it in GitHub Desktop.
Save Lucas-Severo/eae3564358fcefa828f85b0e3e165b27 to your computer and use it in GitHub Desktop.
STL Stack C++
#include <iostream>
#include <stack>
using namespace std;
void showBooks(stack<string> books)
{
if(!books.empty())
{
cout << "\n\tBooks in the stack:" << endl;
while(!books.empty())
{
cout << "\t" << books.top() << endl;
books.pop();
}
}
else
{
cout << "\n\tEmpty stack" << endl;
}
}
int main()
{
stack<string> books;
int opt;
string name;
do
{
cout << "\nWhat do you want to do?" << endl;
cout << "1 - Add new book" << endl;
cout << "2 - Remove a book from the stack" << endl;
cout << "3 - Show stack size" << endl;
cout << "4 - Show stack" << endl;
cout << "5 - Exit" << endl;
cout << ">>> ";
cin >> opt;
switch(opt)
{
case 1:
cin.get();
cout << "\nName: ";
getline(cin, name);
books.push(name);
break;
case 2:
if(!books.empty())
{
cout << "\n\tBook Removed: " << books.top() << endl;
books.pop();
}
break;
case 3:
cout << "\n\tSize: " << books.size() << endl;
break;
case 4:
showBooks(books);
break;
case 5:
break;
default:
cout << "\n\tUnknown option" << endl;
break;
}
}
while(opt != 5);
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> st;
st.push(10);
st.push(20);
st.push(30);
st.push(40);
st.push(50);
int stackSize = st.size();
cout << "Size: " << stackSize << endl;
while(!st.empty()){
cout << st.top() << endl;
st.pop();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment