Skip to content

Instantly share code, notes, and snippets.

@Lucas-Severo
Created December 26, 2019 13:34
Show Gist options
  • Select an option

  • Save Lucas-Severo/eae3564358fcefa828f85b0e3e165b27 to your computer and use it in GitHub Desktop.

Select an option

Save Lucas-Severo/eae3564358fcefa828f85b0e3e165b27 to your computer and use it in GitHub Desktop.

Revisions

  1. Lucas-Severo created this gist Dec 26, 2019.
    72 changes: 72 additions & 0 deletions books.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #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;
    }
    25 changes: 25 additions & 0 deletions stack.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #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;
    }