Question
Please fix the code! #include #include #define DefaultListSize 100 using namespace std; template class Stack { public: virtual Stack() {} virtual void clear() = 0;
Please fix the code!
#include
#define DefaultListSize 100
using namespace std;
template
template
public: AStack(int size = DefaultListSize) { maxSize = size; top = 0; listArray = new Elem[size]; }
AStack() { delete[] listArray; }
void clear() { top = 0; }
void push(const Elem& it) { assert(top != maxSize); cout << "Stack is full"; listArray[top++] = it; }
Elem pop() { assert(top != 0); cout << "Stack is empty"; return listArray[--top]; }
const Elem& topValue() const { assert(top != 0); cout << "Stack is empty"; return listArray[top - 1]; }
int length() const { return top; } };
bool checkPair(char opening, char closing) { if (opening == '(' && closing == ')') return true; else if (opening == '{' && closing == '}') return true; else if (opening == '[' && closing == ']') return true; return false; }
bool areParenthesisBalanced(string exp) { AStack
for (int i = 0; i < exp.length(); i++) { if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') stack.push(exp[i]); else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') { if (stack.length() == 0 || !checkPair(stack.pop(), exp[i])) return false; } }
return (stack.length() == 0); }
int main() { string exp1 = "[()]{}{[()()]()}"; if (areParenthesisBalanced(exp1)) cout << "The expression is balanced" << endl; else cout << "The expression is not balanced" << endl;
string exp2 = "[(])"; if (areParenthesisBalanced(exp2)) cout << "The expression is balanced" << endl; else cout << "The expression is not balanced" << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started