Java delimiter matching using stack

Examples

package com.w3spoint;
 
public class Test {
    private int stackSize;
    private char[] stackArray;
    private int top;
 
    /**
     * constructor to create stack with size
     * @param size
     */
    public Test(int size) {
        this.stackSize = size;
        this.stackArray = new char[stackSize];
        this.top = -1;
    }
 
    /**
     * Adds new entry to the top of the stack
     * @param entry
     * @throws Exception 
     */
    public void push(char entry) throws Exception {
    	this.stackArray[++top] = entry;
    }
 
    /**
     * Removes an entry from the top of the stack.
     * @return
     * @throws Exception 
     */
    public char pop() throws Exception {
        if(this.isStackEmpty()){
        	System.out.println("Stack underflow.");
        }
        return this.stackArray[top--];
    }
 
    /**
     * Returns top of the stack without removing it.
     * @return
     */
    public int peek() {
        return stackArray[top];
    }
 
    /**
     * Returns true if the stack is empty
     * @return
     */
    public boolean isStackEmpty() {
        return (top == -1);
    }
 
    /**
     * Returns true if the stack is full
     * @return
     */
    public boolean isStackFull() {
        return (top == stackSize - 1);
    }
 
    public boolean isDelimiterMatching(String inputExpr) throws Exception {        
        for (int j = 0; j < inputExpr.length(); j++) {
            char ch = inputExpr.charAt(j);
            switch (ch) {
            case '{':
            case '[':
            case '(':
                    push(ch);
                    break;
            case '}':
            case ']':
            case ')':
                    if (!isStackEmpty()) {
                        char stackContent = pop();
                        if ((ch == '}' && stackContent != '{') 
                                || (ch == ']' && stackContent != '[')
                                || (ch == ')' && stackContent != '(')){
                            System.out.println("Mismatch found: " + ch + " at " + j);
                            return false;
                        }
                    } else {
                        System.out.println("Mismatch found: " + ch + " at " + j);
                        return false;
                    }
                    break;
            default: break;
            }
        }
        if (!isStackEmpty()){
            System.out.println("Error: missing right delimiter");
            return false;
        }
        return true;
    }
 
    public String reverseWord(String word) throws Exception{        
        StringBuilder sb = new StringBuilder();
        int size = word.length();
        for(int i=0;i<size;i++){
            push(word.charAt(i));
        }
        while(!isStackEmpty()){
            sb.append(pop());
        }
        return sb.toString();
    }
 
	public static void main(String args[]){
		try {
			String expression = "{(a+b)*(c+d)}";
			Test test = new Test(expression.length());
			System.out.println("Right expression: " + test.isDelimiterMatching(expression));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output

Right expression: true
Please follow and like us:
Content Protection by DMCA.com