dimanche 29 octobre 2023

Simple Stack and Map program stuck

What is wrong with this simple program and why it is going in hang status on cpp.sh

#include <stack>
#include <map>
#include <set>
#include <iostream>

using namespace std;

int main()
{    
    string expression {")"};    
    stack<char> mystack;
    map<char, char> mymap{ {'(', ')'}, {'[', ']'}, {'{', '}'} };
    set<char> myset{ ')', ']', '}'};

    for(auto &i: expression)
    {
        if(mymap.count(i) == 1)
        {
            mystack.push(i);
        }
        
        else if(myset.count(i) == 1)
        {
            cout << "Received :: " << i << endl;
            
            if(mymap.at(mystack.top()) == i)
            {
                cout << " Popped: " << mystack.top() << endl;
                mystack.pop();
            }
            else
            {
                cout << "Non Balanced" << endl;
                break;
            }
        }
    }
    if(mystack.size() != 0)
    {
        cout << "Non Balanced" << endl;
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire