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 #include

#define DefaultListSize 100

using namespace std;

template class Stack { public: virtual Stack() {} virtual void clear() = 0; virtual void push(const Elem&) = 0; virtual Elem pop() = 0; virtual const Elem& topValue() const = 0; virtual int length() const = 0; };

template class AStack : public Stack { private: int maxSize; int top; Elem* listArray;

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 stack;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!