Can someone please help me with this solution?

here my solution for infix to postfix problem, for which I am getting runtime error (SIGSEGV )
please help me point out the mistake in the code.

else if(S[i]==')'){
            while(!st.empty() && st.top()!='('){
                res += st.top();
                st.pop();
            }
            if(st.top() == '('){   // error here
                st.pop();
            }
        }

with st.top() you’re trying to access the top of the stack, but what happens if the stack is empty? Your condition should be something like this,

if(!st.empty() && st.top() == '('){
     st.pop();
3 Likes

thank you so much :pray:

2 Likes

Hi @kabirpathak I am still stuck at this problem (can you tell me what am I doing wrong )
for input: 1
15
((A+B)D)^(E-F)
my output:
A+B
D^E-F
although it should be AB+D*EF-^

ps: where can I learn stack (getting stuck at every stack problem)

Check the following stack-based solution.

https://www.codechef.com/viewsolution/44319498

1 Like