Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want a detailed explanation of how each line of this code works #include #include #include #include using namespace std; struct Node{ char data; Node*

I want a detailed explanation of how each line of this code works

#include #include #include #include using namespace std; struct Node{ char data; Node* next; }; class Stack{ Node* top; public: Stack(){ top = NULL; } void push(char val){ Node* newNode = new Node; newNode->data = val; newNode->next = top; top = newNode; } char pop(){ if (isEmpty()){ cout << "Stack is empty" << endl; return 0; } char val = top->data; Node* temp = top; top = top->next; delete temp; return val; } char getTop(){ if (isEmpty()){ cout << "Stack is empty" << endl; return 0; } return top->data; } void display(){ if (isEmpty()){ cout << "Stack is empty" << endl; return; } Node* temp = top; while (temp != NULL){ cout << temp->data << " "; temp = temp->next; } cout << endl; } bool isEmpty(){ return top == NULL; } bool checkBraceBalance(string expression){ for (int i = 0; i < expression.length(); i++){ if (expression[i] == '(' || expression[i] == '{' || expression[i] == '[') push(expression[i]); if (expression[i] == ')' || expression[i] == '}' || expression[i] == ']'){ if (isEmpty()){ return false; } else if (!isMatchingPair(pop(), expression[i])){ return false; } } } if (isEmpty()) return true; else return false; } bool isMatchingPair(char character1, char character2){ if (character1 == '(' && character2 == ')') return true; else if (character1 == '{' && character2 == '}') return true; else if (character1 == '[' && character2 == ']') return true; else return false; } bool isPalindrome(string word){ for (int i = 0; i < word.length(); i++) push(word[i]); string reversedWord = ""; while (!isEmpty()) reversedWord += pop(); return word == reversedWord; } string infixToPostfix(string infix){ string postfix = ""; for (int i = 0; i < infix.length(); i++){ if (isOperand(infix[i])) postfix += infix[i]; else if (infix[i] == '(') push(infix[i]); else if (infix[i] == ')') { while (getTop() != '(') postfix += pop(); pop(); } else{ while (!isEmpty() && precedence(infix[i]) <= precedence(getTop())) postfix += pop(); push(infix[i]); } } while (!isEmpty()) postfix += pop(); return postfix; } int precedence(char operator_){ if (operator_ == '+' || operator_ == '-') return 1; if (operator_ == '*' || operator_ == '/') return 2; if (operator_ == '^') return 3; return 0; } bool isOperand(char ch){ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); } void listOperatorsByPrecedence(string infix){ map precedence; precedence['+'] = 1; precedence['-'] = 1; precedence['*'] = 2; precedence['/'] = 2; precedence['^'] = 3; cout << "The operators in the infix expression " << infix << " based on their precedence are: "; for (int i = 0; i < infix.length(); i++) { if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') cout << infix[i] << " "; } cout << endl; } int evaluatePostfix(string postfix){ for (int i = 0; i < postfix.length(); i++) { if (isOperand(postfix[i])) push(postfix[i] - '0'); else { int val1 = pop(); int val2 = pop(); switch (postfix[i]){ case '+': push(val2 + val1); break; case '-': push(val2 - val1); break; case '*': push(val2 * val1); break; case '/': push(val2 / val1); break; } } } return pop(); } }; int main(){ Stack stack; int choice; cout << "************** Some Stack Applications ************" << endl; cout << "1- Check the balancing of the braces." << endl; cout << "2- Check whether the word is a palindrome or not." << endl; cout << "3- Convert an infix expression to its postfix expression." << endl; cout << "4- List the expression operators based on their precedence." << endl; cout << "5- Evaluate a postfix expression." << endl; cout << "6- Exit" << endl; while (true){ cout << "Please press any number (1-6) to continue: "; cin >>choice; switch (choice){ case 1:{ string expression; cout << "--------------------You are working on Checking the braces balancing---------------" << endl; cout << "Please enter the expression: "; cin >> expression; if (stack.checkBraceBalance(expression)) cout << "The expression " << expression << " has balanced braces." << endl; else cout << "The expression " << expression << " has no balanced braces." << endl; break; } case 2:{ string word; cout << "----------------You are working on Checking the palindrome words------------------" << endl; cout << "Please enter the world: "; cin >> word; if (stack.isPalindrome(word)) cout << "The word " << word << " is a palindrome." << endl; else cout << "The word " << word << " is not a palindrome." << endl; break; } case 3:{ string infix; cout << "-----------------You are working on Converting infix to postfix expressions ----------" << endl; cout << "Please enter the infix expression: "; cin >> infix; string postfix = stack.infixToPostfix(infix); cout << "The postfix expression of " << infix << " expression is: " << postfix << endl; break; } case 4:{ string infix; cout << "-----------------You are working on Listing the operators based on their precedence ----------" << endl; cout << "Please enter the infix expression: "; cin >> infix; stack.listOperatorsByPrecedence(infix); break; } case 5:{ string postfix; cout << "-----------------You are working on Evaluating postfix expressions ----------" << endl; cout << "Please enter the postfix expression: "; cin >> postfix; int result = stack.evaluatePostfix(postfix); cout << "The result of the postfix expression " << postfix << " is: " << result << endl; break; } case 6: return 0; default: cout << "Invalid choice. Please enter a valid choice (1-6)." << endl; break; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions