Balanced Parenthesis

I try to check for Balanced Parenthesis but i couldnt get to work. I am at stuck at
int main
how can i pass the information to function it can check

here is what I have to check:


(1)	{ [ [ ( ) ( ) ] [ ( ) ] ( ) }

(2)	{ [ ( ) ) ( ) ] }

(3)	{ [ [ [ ( ) ( ) ] }

(4)	{ [ ( ) ( ) } ]



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

class DynIntStack
{
private:
    // Structure for stack nodes
    struct StackNode
    {
        char data[MAX_SIZE];
        int value;        // Value in the node
        StackNode *next;  // Pointer to the next node
    };
    
    StackNode *top;      // Pointer to the stack top
    
public:
    // Constructor
    DynIntStack()
    {  top = NULL; }
    
    // Destructor
    ~DynIntStack();
    
    // Stack operations
    void push(int);
    void pop(int &);
    bool checkParenthesisMatch(char, char);
    bool checkBalancedParenthesis(char[]);
    bool isEmpty();
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
bool checkParenthesisMatch(char ch1, char ch2)
{
    bool result = false;
    if(    (ch1 == ']' && ch2 == '[') ||
       (ch1 == '}' && ch2 == '{') ||
       (ch1 == ')' && ch2 == '(') )
        result = true;
    return result;
}
/*
 checkBalancedParenthesis(char[]) would return false if parenthesis are not balanced.
 */
bool checkBalancedParenthesis(char exp[])
{
    int len = strlen(exp);
    bool result = true;
    stack st;
    for(int i=0;i<len;i++)
    {
        if(    (exp[i] == '[') ||
           (exp[i] == '{') ||
           (exp[i] == '(') )
        {
            st.push(exp[i]);
        }
        else if((exp[i] == ']') ||
                (exp[i] == '}') ||
                (exp[i] == ')') )
        {
            if(checkParenthesisMatch(exp[i],st.pop()) == false)
            {
                result = false;
                break;
            }
        }
    }
    /*There exists a case where even after successful result from above statements
     still the possibility of imbalance exists; e.g., [{}()[()]  -->Missed one more
     closing parenthesis. Check this case by checking stack space
     */
    if(st.isEmpty()== false)
        result = false;
    
    return result;
}


Although I know of another way to accomplish the checking, I don't understand what your question is to help you with what it is you're stuck with.
Could you please elaborate/rephrase?
Topic archived. No new replies allowed.