Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use java and take ide screenshot Stack for checking Balanced Parentheses,

ID: 3757189 • Letter: P

Question

Please use java and take ide screenshot

Stack for checking Balanced Parentheses, Brackets, and Braces In this assignment, you are using Sequential List (Array) to implement the Stack, with Push and Pop operation. You need to write your own Push and Pop operations (and any other operations that you might need) Use your Stack to match balanced parentheses, brackets, and braces. For example, input string "([)[] should return TRUE, and "[D)" should return FALSE I suggest implementing this by creating a Stack, pushing a (or [or fonto the stack whenever one of those characters is seen, and popping it off when the corresponding closing character is seen. If the wrong character is at the top of the stack, the parentheses do not match, and the progranm should return false. Test your program with several inputs, to cover balanced and unbalance cases. Use simple as well as complex parenthesized string of (, {, and [ Use appropriate error messages; stack-full, stack-empty, and so on. Print the string and say if it is balanced or unbalanced.

Explanation / Answer

Program


import java.util.*;

public class BalancedParanthesis {

    static class stack
    {
        int top=-1;
        char items[] = new char[100];

        void push(char x)
        {
            if (top == 99)
            {
                System.out.println("Stack full");
            }
            else
            {
                items[++top] = x;
            }
        }

        char pop()
        {
            if (top == -1)
            {
                System.out.println("Underflow error");
                return '';
            }
            else
            {
                char element = items[top];
                top--;
                return element;
            }
        }

boolean isEmpty()
        {
            return (top == -1) ? true : false;
        }
    }
     

    public static void main(String[] args) {
        System.out.println(balancedParenthensies("{(a,b)}"));
        System.out.println(balancedParenthensies("{(a},b)"));
        System.out.println(balancedParenthensies("{)(a,b}"));

    }
    public static boolean balancedParenthensies(String s) {
      
        /* Declare an empty character stack */
       stack st=new stack();

        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c == '[' || c == '(' || c == '{' ) {    
                st.push(c);
            } else if(c == ']') {
                if(st.isEmpty() || st.pop() != '[') {
                    return false;
                }
            } else if(c == ')') {
                if(st.isEmpty() || st.pop() != '(') {
                    return false;
                }          
            } else if(c == '}') {
                if(st.isEmpty() || st.pop() != '{') {
                    return false;
                }
            }

}
        return st.isEmpty();

    }

    }