idastan97

CSCI152 L11 P3

Feb 22nd, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////// Class with static Method
  2. public class LinkedListStaticMethods {
  3.     public static boolean isPalindrome(Queue<Character> q){
  4.         Stack<Character> tempStack=new LinkedListStack();
  5.         Queue<Character> tempQueue=new LinkedListQueue();
  6.         int s=q.getSize();
  7.         for (int i=0; i<s; i++){
  8.             char x;
  9.             try {
  10.                 x=q.dequeue();
  11.             } catch (Exception ex){
  12.                 System.out.println(ex.getMessage());
  13.                 x=' ';
  14.             }
  15.             tempStack.push(x);
  16.             tempQueue.enqueue(x);
  17.             q.enqueue(x);
  18.         }
  19.         boolean res=true;
  20.         for (int i=0; i<s; i++){
  21.             try {
  22.                 if (tempStack.pop()!=tempQueue.dequeue()){
  23.                     res=false;
  24.                     break;
  25.                 }
  26.             } catch (Exception ex) {
  27.                 System.out.println(ex.getMessage());
  28.             }
  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.         System.out.println(myQ);
  40.         System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
  41.         System.out.println(myQ+"\n");
  42.        
  43.         String s="QazaQ";
  44.         for (int i=0; i<s.length(); i++){
  45.             myQ.enqueue(s.charAt(i));
  46.         }
  47.         System.out.println(myQ);
  48.         System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
  49.         System.out.println(myQ+"\n");
  50.        
  51.         s="Qazaq";
  52.         myQ.clear();
  53.         for (int i=0; i<s.length(); i++){
  54.             myQ.enqueue(s.charAt(i));
  55.         }
  56.         System.out.println(myQ);
  57.         System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
  58.         System.out.println(myQ+"\n");
  59.        
  60.         s="qwert";
  61.         myQ.clear();
  62.         for (int i=0; i<s.length(); i++){
  63.             myQ.enqueue(s.charAt(i));
  64.         }
  65.         System.out.println(myQ);
  66.         System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
  67.         System.out.println(myQ+"\n");
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment