idastan97

CSCI152 L11 P2

Feb 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////////// Class with static method
  2. public class LinkedListStaticMethods {
  3.     public static boolean isBalanced(Queue<Character> q) throws Exception{
  4.         boolean res=true, ex=false;
  5.         Stack<Character> tmpSt=new LinkedListStack();
  6.         int quSize=q.getSize();
  7.         for (int i=0; i<quSize; i++){
  8.             char tmpC=q.dequeue();
  9.             q.enqueue(tmpC);
  10.             if (tmpC=='(' || tmpC=='[' || tmpC=='{'){
  11.                 tmpSt.push(tmpC);
  12.             } else if (tmpC==')' || tmpC==']' || tmpC=='}'){
  13.                 if (tmpC==')'){
  14.                     tmpC='(';
  15.                 } else if (tmpC==']'){
  16.                     tmpC='[';
  17.                 } else if (tmpC=='}'){
  18.                     tmpC='{';
  19.                 }
  20.                 if (tmpSt.getSize()==0 || tmpSt.pop()!=tmpC){
  21.                     res=false;
  22.                 }
  23.             } else {
  24.                 ex=true;
  25.             }
  26.         }
  27.         if (ex){
  28.             throw new Exception("Illegel character was found.");
  29.         }
  30.         return res;
  31.     }
  32. }
  33.  
  34. /////////////////////////////////////////////////////////////////////////////////////////// Test Class
  35. public class testClass {
  36.    
  37.     public static void main(String[] args){
  38.         Queue<Character> myQ=new LinkedListQueue();
  39.        
  40.         String s="({[][]})()";
  41.         for (int i=0; i<s.length(); i++){
  42.             myQ.enqueue(s.charAt(i));
  43.         }
  44.         System.out.println(myQ);
  45.         try {
  46.             System.out.println("is balances: "+LinkedListStaticMethods.isBalanced(myQ));
  47.         } catch (Exception ex){
  48.             System.out.println(ex.getMessage());
  49.         }
  50.         System.out.println(myQ+"\n");
  51.        
  52.         s="({[])})";
  53.         myQ.clear();
  54.         for (int i=0; i<s.length(); i++){
  55.             myQ.enqueue(s.charAt(i));
  56.         }
  57.         System.out.println(myQ);
  58.         try {
  59.             System.out.println("is balances: "+LinkedListStaticMethods.isBalanced(myQ));
  60.         } catch (Exception ex){
  61.             System.out.println(ex.getMessage());
  62.         }
  63.         System.out.println(myQ+"\n");
  64.        
  65.         s="()))";
  66.         myQ.clear();
  67.         for (int i=0; i<s.length(); i++){
  68.             myQ.enqueue(s.charAt(i));
  69.         }
  70.         System.out.println(myQ);
  71.         try {
  72.             System.out.println("is balances: "+LinkedListStaticMethods.isBalanced(myQ));
  73.         } catch (Exception ex){
  74.             System.out.println(ex.getMessage());
  75.         }
  76.         System.out.println(myQ+"\n");
  77.        
  78.         s="({[]ff)})";
  79.         myQ.clear();
  80.         for (int i=0; i<s.length(); i++){
  81.             myQ.enqueue(s.charAt(i));
  82.         }
  83.         System.out.println(myQ);
  84.         try {
  85.             System.out.println("is balances: "+LinkedListStaticMethods.isBalanced(myQ));
  86.         } catch (Exception ex){
  87.             System.out.println(ex.getMessage());
  88.         }
  89.         System.out.println(myQ+"\n");
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment