List some applications of Stack?

Following are some applications of the Stack :

  1. Evaluation of Arithmetic Expressions

  2. Backtracking

  3. Delimiter Checking

  4. Reverse a Data

  5. Processing Function Calls

The approach to check for valid parenthesis is to first

  • Declare an empty stack and then traverse the string and whenever an opening bracket is encountered, insert it into the stack.
  • On the other hand, when a closing bracket is encountered, check if the last bracket in the stack matches the current closing bracket.
  • If not, then return false otherwise remove the last bracket from the stack.
  • After traversing the string, if the stack is empty return true otherwise return false.

In four such steps, the problem can be solved. Similar pattern problems can be tackled with the same approach.

PSUEDO CODE: -

bool isBalancedParentheses(string str) {
   int n = str.size();
   stack<char> openingBrackets;
   for (int i = 0; i < n; i++) {
   if (str[i] == '(' || str[i] == '{' || str[i] == '[') {
openingBrackets.push(str[i]);
} else {
   if (openingBrackets.empty()) {
     return false;
     }
   if (str[i] == ')') {
      char last = openingBrackets.top();
      openingBrackets.pop();
  if (last != '(') {      
   return false;
 }
 }

if (str[i] == '}') {
      char last = openingBrackets.top();
      openingBrackets.pop();

       if (last != '{')
           return false;
        }
           }
         if (str[i] == ']') {
     char last = openingBrackets.top();
    openingBrackets.pop();
      if (last != '[') {
 return false;
    }
  }
  }
 }
      return openingBrackets.empty();
}