Stack Application Program in Java

 /*  
 StackApplication.java: Brackets checking program.   
 */  
 import java.util.*;  
 import java.io.*;  
 class stack_operations  
 {  
      private int size;  
      private char stack[];  
      private int top;  
      stack_operations(int s)  
      {  
           size=s;  
           stack=new char [size];  
           top=-1;  
      }  
      public void push (char item)  
      {  
           stack[++top]=item;  
      }  
      public char pop()  
      {  
           return stack[top--];  
      }  
      public char peek()  
      {  
           return stack[top];  
      }  
      public boolean isFull()  
      {  
           return (top==size-1);  
      }  
      public boolean isEmpty()  
      {  
           return (top==-1);  
      }  
      public void display()  
      {  
           for (int i=0;i<=top ;i++ )  
           {  
                System.out.print(""+stack[i]+"     ");  
           }  
           System.out.println();  
      }  
 }  
 class BracketChecker  
 {  
      private String input;  
      private String output;  
      BracketChecker(String in)  
      {  
           input=in;  
      }  
      public void check()  
      {  
           int size=input.length();  
           stack_operations obj=new stack_operations(size);  
           int flag=0;  
           for (int i=0;i<input.length();i++ )  
           {  
                char ch=input.charAt(i);  
                switch (ch)  
                {  
                     case '{':  
                     case '(':  
                     case '[':  
                          obj.push(ch);  
                          break;  
                     case '}':  
                     case ')':  
                     case ']':  
                          if (!obj.isEmpty())  
                          {  
                               char chx=obj.pop();  
                               if (ch=='}' && chx != '{'||  
                                    ch==')' && chx != '('||  
                                    ch==']' && chx != '[')  
                               {  
                                    System.out.println("Error!!"+ch+"at"+i);  
                               }  
                          }  
                          break;  
                     default:  
                          break;  
                }  
           }  
           if (!obj.isEmpty())  
           {  
                System.out.println("Error in mismatch..");  
           }  
           //else  
           //     System.out.println("Given Arithmetic expression is correct..");  
      }  
 }  
 class stackApp   
 {  
      public static void main(String[] args) throws IOException  
      {  
           String input;  
           while (true)  
           {  
                input=getString();  
                if (input.equals(""))  
                {  
                     break;  
                }  
                BracketChecker bobj=new BracketChecker(input);  
                bobj.check();  
           }  
      }  
      public static String getString()throws IOException  
      {  
           InputStreamReader is=new InputStreamReader(System.in);  
           BufferedReader br=new BufferedReader(is);  
           System.out.println("Enter string:");  
           String str=br.readLine();  
           return str;  
      }  
 }  

Post a Comment

0 Comments