Stack and Queue C NOTE Must aggregate the array class poste
Stack and Queue - C++
**NOTE: Must aggregate the array class posted below.**
Implement a Stack class using the Array Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { } and/or < >. The following is one set of data to test your code:
( )
[ ] ( ) { }
[ { ( ) } ]
< > <<< >>>
[ [ ) )
{ ( ) [ ( ) ] }
class Array
 {
 private:
    int begin;
    int end;
    int *ary;
 public:
    Array(int n);
    Array(int n, int m);
    int &operator[](int index);
    Array &operator = (Array a);
    bool operator == (Array &a);
    bool operator != (Array &a);
    friend ostream &operator << (ostream &out, Array &a);
    friend Array operator + (Array &a, Array&b);
    friend Array operator - (Array &a, Array&b);
    friend Array operator * (Array &a, Array&b);
    friend Array operator / (const Array &a, const Array&b);
 };
 //Constructor
 Array::Array(int n)
 {
    begin = 0;
    end = n - 1;
    ary = new int[n];
    for (int i = 0; i < n; i++)
        ary[i] = i + 1;
 }
//Copy constructor
 Array::Array(int n, int m)
 {
    begin = n;
    end = m - 1;
    ary = new int[m - n];
    for (int i = 0; i < m - n; i++)
        ary[i] = begin + i;
 }
//Overload [] operator
 int &Array::operator[](int index)
 {
    static int element; //Static added to silence reference to stack memory warning
    if (index >= begin && index <= end)
    {
        element = ary[index - begin];
    }
    else
    {
        cout << \"Error: Index out of bounds.\" << endl;
        element = 0;
    }
    return element;
 }
//Overload the = operator
 Array &Array::operator = (Array a)
 {
    begin = a.begin;
    end = a.end;
    for(int i = 0; i < end - begin; i++)
    {
        ary[i] = a.ary[i];
    }
    return *this;
 }
//Overload the == operator
 bool Array::operator == (Array &a)
 {
    for(int i = 0; i < end - begin; i++)
    {
        if(ary[i] != a.ary[i])
            return false;
    }
    return true;
 }
//Overoads the != operator
 bool Array::operator != (Array &a)
 {
    return *this == a;
 }
ostream &operator << (ostream &out, Array &b)
 {
    for(int i = 0; i < b.end - b.begin; i++)
    {
        out << \"(\" << i << \")\" << b.ary[i] << \" \";
    }
    return out;
 }
//Overload the + operator
 Array operator + (Array &a, Array&b)
 {
    Array arr(a.end - a.begin);
    for(int i = 0; i < a.end - a.begin; i++)
    {
        arr.ary[i] = a.ary[i] + b.ary[i];
    }
    return arr;
   
 }
//Overloads the subtraction operator
 Array operator - (Array &a, Array&b)
 {
    Array arr(a.end - a.begin);
    for(int i = 0; i < a.end - a.begin; i++)
    {
        arr.ary[i] = a.ary[i] - b.ary[i];
       
    }
    return arr;
 }
//Overload the multiplication operator
 Array operator * (Array &a, Array&b)
 {
    Array arr(a.end - a.begin);
    for(int i = 0; i < a.end - a.begin; i++)
    {
        arr.ary[i] = a.ary[i] * b.ary[i];
    }
    return arr;
 }
//Overload the division operator
 Array operator / (const Array &a, const Array&b)
 {
    Array arr(a.end - a.begin);
    for(int i = 0; i < a.end - a.begin; i++)
    {
        if (b.ary[i] == 0)
        {
            arr.ary[i] = 0;
        }
        else
        {
            arr.ary[i] = a.ary[i] / b.ary[i];
        }
       
    }
    return arr;
 }
Solution
 #include <iostream>
 #include <string>
 using namespace std;
 /* structure of a stack node */
 struct sNode
 {
 char data;
 struct sNode *next;
 };
 
 /* Function to push an item to stack*/
 void push(struct sNode** top_ref, int new_data);
 
 /* Function to pop an item from stack*/
 int pop(struct sNode** top_ref);
 
 /* Returns 1 if character1 and character2 are matching left
 and right Parenthesis */
 bool isMatchingPair(char character1, char character2)
 {
 if (character1 == \'(\' && character2 == \')\')
 return 1;
 else if (character1 == \'{\' && character2 == \'}\')
 return 1;
 else if (character1 == \'[\' && character2 == \']\')
 return 1;
 else if (character1 == \'<\' && character2 == \'>\')
 return 1;
 else
 return 0;
 }
 
 /*Return 1 if expression has balanced Parenthesis */
 bool areParenthesisBalanced(char exp[])
 {
 int i = 0;
 
 /* Declare an empty character stack */
 struct sNode *stack = NULL;
 
 /* Traverse the given expression to check matching parenthesis */
 while (exp[i])
 {
 /*If the exp[i] is a starting parenthesis then push it*/
 if (exp[i] == \'{\' || exp[i] == \'(\' || exp[i] == \'[\' || exp[i] ==\'<\')
 push(&stack, exp[i]);
 
 /* If exp[i] is a ending parenthesis then pop from stack and
 check if the popped parenthesis is a matching pair*/
 if (exp[i] == \'}\' || exp[i] == \')\' || exp[i] == \']\' || exp[i]==\'>\')
 {
 
 /*If we see an ending parenthesis without a pair then return false*/
 if (stack == NULL)
 return 0;
 
 /* Pop the top element from stack, if it is not a pair
 parenthesis of character then there is a mismatch.
 This happens for expressions like {(}) */
 else if ( !isMatchingPair(pop(&stack), exp[i]) )
 return 0;
 }
 i++;
 }
   
 /* If there is something left in expression then there is a starting
 parenthesis without a closing parenthesis */
 if (stack == NULL)
 return 1; /*balanced*/
 else
 return 0; /*not balanced*/
 }
int main()
 {
 char exp[100] = \"( ) [ ] ( ) { } [ { ( ) } ] < > <<< >>>[ [ (( ) )]]{ ( ) [ ( ) ] }\";
 if (areParenthesisBalanced(exp))
 printf(\"\  Balanced \");
 else
 printf(\"\  Not Balanced \");
 return 0;
 }
 
 /* Function to push an item to stack*/
 void push(struct sNode** top_ref, int new_data)
 {
 /* allocate node */
 struct sNode* new_node =
 (struct sNode*) malloc(sizeof(struct sNode));
 
 if (new_node == NULL)
 {
 printf(\"Stack overflow \ \");
 getchar();
 exit(0);
 }   
 
 /* put in the data */
 new_node->data = new_data;
 
 /* link the old list off the new node */
 new_node->next = (*top_ref);
 
 /* move the head to point to the new node */
 (*top_ref) = new_node;
 }
 
 /* Function to pop an item from stack*/
 int pop(struct sNode** top_ref)
 {
 char res;
 struct sNode *top;
 
 /*If stack is empty then error */
 if (*top_ref == NULL)
 {
 printf(\"Stack overflow \ \");
 getchar();
 exit(0);
 }
 else
 {
 top = *top_ref;
 res = top->data;
 *top_ref = top->next;
 free(top);
 return res;
 }
 }





