Question Implement Stack using Queues
class MyStack {
private:
queue<int> queue1;
queue<int> queue2;
public:
MyStack() {
// Constructor
void push(int x) {
// Push element onto queue2
queue2.push(x);
// Move all elements from queue1 to queue2
while (!queue1.empty()) {
queue2.push(queue1.front());
queue1.pop();
// Swap the names of the two queues
swap(queue1, queue2);
int pop() {
if (empty()) {
cout << "Stack Underflow\n";
return -1; // Return -1 if stack is empty
}
int poppedValue = queue1.front();
queue1.pop();
return poppedValue;
int top() {
if (empty()) {
cout << "Stack is Empty\n";
return -1; // Return -1 if stack is empty
return queue1.front();
bool empty() {
return queue1.empty();
};
int main() {
MyStack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " popped from the stack\n"; // Outputs 30
cout << "Top element is: " << s.top() << endl; // Outputs 20
cout << (s.empty() ? "Stack is empty" : "Stack is not empty") << endl; // Outputs "Stack is not
empty"
return 0;
Question: implementation to convert an infix expression to postfix
#include <iostream>
#include <stack>
#include <cctype>
using namespace std;
// Function to return precedence of operators
int precedence(char c) {
if (c == '^') return 3; // Exponentiation has the highest precedence
if (c == '*' || c == '/') return 2; // Multiplication and division
if (c == '+' || c == '-') return 1; // Addition and subtraction
return -1; // Parentheses or invalid characters
// Function to check if a character is an operator
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
// Function to convert infix expression to postfix expression
string infixToPostfix(string infix) {
stack<char> st; // Stack to hold operators
string postfix; // Resultant postfix expression
for (int i = 0; i < infix.length(); i++) {
char c = infix[i];
// If the scanned character is an operand, add it to the postfix output
if (isalnum(c)) {
postfix += c;
// If the scanned character is '(', push it to the stack
else if (c == '(') {
st.push(c);
// If the scanned character is ')', pop and output from the stack
// until an '(' is encountered
else if (c == ')') {
while (!st.empty() && st.top() != '(') {
postfix += st.top();
st.pop();
st.pop(); // Pop the '('
// If an operator is encountered
else if (isOperator(c)) {
while (!st.empty() && precedence(st.top()) >= precedence(c)) {
postfix += st.top();
st.pop();
st.push(c);
// Pop all the operators from the stack
while (!st.empty()) {
postfix += st.top();
st.pop();
}
return postfix;
int main() {
string infix;
cout << "Enter infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
Question infix to prefix
#include <bits/stdc++.h>
using namespace std;
// Function to check if the character is an operator
bool isOperator(char c)
return (!isalpha(c) && !isdigit(c));
// Function to get the priority of operators
int getPriority(char C)
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
// Function to convert the infix expression to postfix
string infixToPostfix(string infix)
infix = '(' + infix + ')';
int l = infix.size();
stack<char> char_stack;
string output;
for (int i = 0; i < l; i++) {
// If the scanned character is an
// operand, add it to output.
if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];
// If the scanned character is an
// ‘(‘, push it to the stack.
else if (infix[i] == '(')
char_stack.push('(');
// If the scanned character is an
// ‘)’, pop and output from the stack
// until an ‘(‘ is encountered.
else if (infix[i] == ')') {
while (char_stack.top() != '(') {
output += char_stack.top();
char_stack.pop();
// Remove '(' from the stack
char_stack.pop();
// Operator found
else {
if (isOperator(char_stack.top())) {
//you can’t store tow power together
if (infix[i] == '^') {
while (
getPriority(infix[i])
<= getPriority(char_stack.top())) {
output += char_stack.top();
char_stack.pop();
else {
while (
getPriority(infix[i])
< getPriority(char_stack.top())) {
output += char_stack.top();
char_stack.pop();
}
// Push current Operator on stack
char_stack.push(infix[i]);
while (!char_stack.empty()) {
output += char_stack.top();
char_stack.pop();
return output;
// Function to convert infix to prefix notation
string infixToPrefix(string infix)
// Reverse String and replace ( with ) and vice versa
// Get Postfix
// Reverse Postfix
int l = infix.size();
// Reverse infix
reverse(infix.begin(), infix.end());
// Replace ( with ) and vice versa
for (int i = 0; i < l; i++) {
if (infix[i] == '(') {
infix[i] = ')';
else if (infix[i] == ')') {
infix[i] = '(';
}
string prefix = infixToPostfix(infix);
// Reverse postfix
reverse(prefix.begin(), prefix.end());
return prefix;
// Driver code
int main()
string s = ("x+y*z/w+u");
// Function call
cout << infixToPrefix(s) << std::endl;
return 0;