Wednesday, December 14, 2011

Balanced Parenthesis in an expression

Question 1:
Given an expression string exp, write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[","]” are correct in exp. For example, the program should print true for exp = “[()]{}{[()()]()}” and false for exp = “[(])”

Question 2:
Given an expression with only ‘}’ and ‘{‘. The expression may not be balanced. Find minimum number of bracket reversals to make the expression balanced.

Question 3:
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
http://www.programcreek.com/2012/12/leetcode-valid-parentheses-java/

Question 4:
You have been asked to Write an algo­rithm to find Whether Given the Sequence of paren­the­ses are well formed
http://algorithms.tutorialhorizon.com/algorithms-find-whether-given-the-sequence-of-parentheses-are-well-formed/

Question 5:
Print All Possible Valid Combinations Of Parenthesis of Given ‘N’
http://algorithms.tutorialhorizon.com/generate-all-valid-parenthesis-strings-of-length-2n-of-given-n/

Strategy:
1) Declare a character stack S.
2) Now traverse the expression string exp.
a) If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[') then push it to stack.
b) If the current character is a closing bracket (')' or '}' or ']‘) then pop from stack and if the popped character is the matching starting bracket then fine else parenthesis are not balanced.
3) After complete traversal, if there is some starting bracket left in stack then “not balanced”

Code:
 #include <stdio.h>
#include <stdlib.h>

#define bool int

struct sNode
{
char data;
struct sNode *next;
};
void push(struct sNode** top_ref, int new_data)
{

struct sNode* new_node =
(struct sNode*) malloc(sizeof(struct sNode));

if(new_node == NULL)
   {
    printf("Stack overflow \n");
    getchar();
    exit(0);
   }

new_node->data = new_data;

new_node->next = (*top_ref);

(*top_ref) = new_node;
}

int pop(struct sNode** top_ref)
{
char res;
struct sNode *top;

/*If stack is empty then error */
if(*top_ref == NULL)
  {
    printf("Stack overflow \n");
    getchar();
    exit(0);
  }
else
  {
    top = *top_ref;
    res = top->data;
    *top_ref = top->next;
    free(top);
    return res;
  }
}

//ALGORITHM  START

bool isMatchingPair(char character1, char character2)
{
if(character1 == '(' && character2 == ')')
return 1;
else if(character1 == '{' && character2 == '}')
return 1;
else if(character1 == '[' && character2 == ']')
return 1;
else
return 0;
}

bool areParenthesisBalanced(char exp[])
{
int i = 0;

struct sNode *stack = NULL;

while(exp[i])
{
if(exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
push(&stack, exp[i]);

if(exp[i] == '}' || exp[i] == ')' || exp[i] == ']')
{

if(stack == NULL)
return 0;

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;
else
return 0;
}

int main()
{
char exp[100] = "[{()}]";
if(areParenthesisBalanced(exp))
printf("\n Balanced ");
else
printf("\n Not Balanced "); \
getchar();
}

1 comment:

  1. enqueue and dequeue using stack
    http://www.careercup.com/question?id=7804678

    ReplyDelete