#include#include using namespace std; int main() { stack mystack; mystack.push(1); mystack.push(2); mystack.push(3); while (!mystack.empty()) { cout << ' ' << mystack.top(); mystack.pop(); } return 0; }
#include#include #include using namespace std; int prec(char op) { if(op == '+'||op == '-') return 1; if(op == '*'||op == '/') return 2; return 0; } string infixToPostfix(string exp) { stack stack; string postfix = ""; for(int i = 0; i< exp.length(); i++) { if((exp[i] >= 'a' && exp[i] <= 'z')||(exp[i] >= 'A' && exp[i] <= 'Z')) postfix += exp[i]; else if(exp[i] == '(') stack.push('('); else if(exp[i] == ')') { while(stack.top() != '(') { postfix += stack.top(); stack.pop(); } stack.pop(); } else { while(!stack.empty() && prec(exp[i]) <= prec(stack.top())) { postfix += stack.top(); stack.pop(); } stack.push(exp[i]); } } while(!stack.empty()) { postfix += stack.top(); stack.pop(); } return postfix; } int main() { string exp = "A+B*C-D/E"; cout<<"Infix Expression: "< In the above example, we are converting an infix expression to postfix using the Cpp Stack Library. Here, we define a function infixToPostfix(), which takes input the infix expression and returns postfix expression using stack operation. We define a stack of characters and scan the infix expression, one character at a time, and push or pop operators from the stack according to the rules of postfix conversion. Finally, we return the postfix expression. Package Library: Standard Template Library (STL)