Thursday, 7 March 2013

Stack Class OverFlow


  • public class ParenthesisValidation {
    public Boolean paranthesisCheck(String exp) {
    MyStack Mystk = new MyStack(exp.length());
    char ch;
    Boolean Valid = true;
    for (int i = 0; i < exp.length(); i++) {
    ch = exp.charAt(i);
    if (ch == '[' || ch == '{' || ch == '(') {
    Mystk.PUSH(ch);
    } else {
    if (ch == ']' || ch == '}' || ch == ')') {
    if (Mystk.isEmpty()) {
    Valid = false;
    }
    char c = Mystk.POP();
    if(c == '[' && ch == ']' || c == '{' && ch == '}' || c == '(' && ch == ')'){

    }
    }
    }
    }
    return true;
    }
    }
    • Shiraz Sohail

      public class MyStackDemo {
      public static void main(String[] args) {
      MyStack MYstk = new MyStack(10);
      ParenthesisValidation Par = new ParenthesisValidation();
      Par.paranthesisCheck("{(2+3)*3}");

      /* MYstk.PUSH(23);
      MYstk.PUSH(34);
      MYstk.PUSH(66);
      System.out.println(MYstk.POP());
      System.out.println(MYstk.POP());
      System.out.println(MYstk.POP());
      System.out.println(MYstk.POP()); */
      }
      }
      • Shiraz Sohail

        public class MyStack {
        char[] Stack;
        int top;

        MyStack(int s){
        Stack = new char[s];
        top = -1;
        }

        public Boolean isFull(){
        if(top == Stack.length-1)
        return true;
        else
        return false;
        }

        public Boolean isEmpty(){
        if(top == -1)
        return true;
        else
        return false;
        }
        public void PUSH(char d){
        if(isFull()){
        System.out.println("Stack Overflow");
        }
        else{
        Stack[++top] = d;
        }
        }

        public char POP(){
        if(isEmpty()){
        System.out.println("Stack Underflow");
        return ' ';
        }
        else{
        char T=Stack[top];
        top--;
        return T;
        }
        }

        }

      No comments:

      Post a Comment