Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////// Class with static Method
- public class LinkedListStaticMethods {
- public static boolean isPalindrome(Queue<Character> q){
- Stack<Character> tempStack=new LinkedListStack();
- Queue<Character> tempQueue=new LinkedListQueue();
- int s=q.getSize();
- for (int i=0; i<s; i++){
- char x;
- try {
- x=q.dequeue();
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- x=' ';
- }
- tempStack.push(x);
- tempQueue.enqueue(x);
- q.enqueue(x);
- }
- boolean res=true;
- for (int i=0; i<s; i++){
- try {
- if (tempStack.pop()!=tempQueue.dequeue()){
- res=false;
- break;
- }
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- return res;
- }
- }
- //////////////////////////////////////////////////////////////////////////// Test Class
- public class testClass {
- public static void main(String[] args){
- Queue<Character> myQ=new LinkedListQueue();
- System.out.println(myQ);
- System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
- System.out.println(myQ+"\n");
- String s="QazaQ";
- for (int i=0; i<s.length(); i++){
- myQ.enqueue(s.charAt(i));
- }
- System.out.println(myQ);
- System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
- System.out.println(myQ+"\n");
- s="Qazaq";
- myQ.clear();
- for (int i=0; i<s.length(); i++){
- myQ.enqueue(s.charAt(i));
- }
- System.out.println(myQ);
- System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
- System.out.println(myQ+"\n");
- s="qwert";
- myQ.clear();
- for (int i=0; i<s.length(); i++){
- myQ.enqueue(s.charAt(i));
- }
- System.out.println(myQ);
- System.out.println("is palindrome: "+LinkedListStaticMethods.isPalindrome(myQ));
- System.out.println(myQ+"\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment