dj5
dj5

Reputation: 81

Determining a Palindrome using Stack and Queue in java

Here I am trying to determine if a word or phrase is a palindrome by using stacks and queues depending on the phrase I write in.

What it is doing is that it says that everything is a palindrome and writes "Palindrome" by how many letters it has.

I'm guessing that I need to add something between the last for loop and the while loop, but I'm not sure what.

public class CheckPalindrome {

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    while (true) {

        String line = reader.readLine();


        if (line.toLowerCase().equals("quit")) {
            break;
        }

        Stack<Character> stack = new Stack<Character>();

        Queue<Character> queue = new LinkedList<Character>();

         for (int i = 0; i < line.length(); i++) {
                stack.push(line.charAt(i));
            }

         for (int i = line.length() - 1; i >= 0; i--) {
             queue.add(line.charAt(i));
         }

          while (!queue.isEmpty()) {
             if (queue.remove().equals(stack.pop())) {
                 System.out.println("Palindrome");
             } else {
                 System.out.println("Not a Palindrome");
             }
         }

      }     
   }
}

Upvotes: 0

Views: 15191

Answers (5)

Salauddin007
Salauddin007

Reputation: 15

Using both stack and queue together for checking palindrome in JAVA

    public class Palindrome {


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the String:: ");
        String real = input.nextLine();

        Queue q = new LinkedList();
        Stack st = new Stack();


        for(int i=0; i<=real.length()-1; i++) {
            q.add(real.charAt(i));
        }

        for(int i=0; i<real.length(); i++) {
            st.push(real.charAt(i));
        }

        if(q.remove().equals(st.pop())) {
            System.out.println("Palindrom");
        }else {
            System.out.println("Not Palindrom");
        }

    }

}

Upvotes: -1

RAJNISH YADAV
RAJNISH YADAV

Reputation: 181

Here is new Solution. Try this one. If any modification ,let me know. package Stack;

public class pallindrome {

Stack<Integer> stack = new Stack<Integer>();
Queue<Integer> queue = new LinkedList<Integer>();

void pushh(int new_Data) {
    stack.push(new_Data);
}

void enquee(int new_Data) {

    queue.add(new_Data);
}

int popStack(){
    return stack.pop();
}

int dequeueQueue() {
    return queue.remove();
}   
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        scan.close();

        // Convert input String to an array of characters:
        char[] s = input.toCharArray();

        // Create a Solution object:
        pallindrome p = new pallindrome();

        // Enqueue/Push all Integer to their respective data structures:
        for(int i=0;i<input.length();i++)
        {
            p.pushh(i);
            p.enquee(i);
        }
        // Pop/Dequeue the chars at the head of both data structures and compare them:
        boolean isPalindrome = true;
        for ( i = 0; i < s.length/2; i++) {
            if (p.popStack() != p.dequeueQueue()) {
                isPalindrome = false;                
                break;
            }
        }

        //Finally, print whether string s is palindrome or not.
        System.out.println( "The Integer, " + input + ", is " 
                           + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) );
    }
}

Upvotes: 0

d.j.brown
d.j.brown

Reputation: 1842

Incase it's of interest, here's a variation on your approach using a Deque<E> as opposed to a Stack and Queue separately. A Deque is just a double ended queue (i.e. operates as both).

public static boolean isPalindrome(String word) {
    boolean isPalindrome = word.length() == 1;
    if (!isPalindrome) {
        Deque<Character> wordDeque = new LinkedList<>();
        for (Character c : word.toCharArray()) {
            wordDeque.add(Character.toLowerCase(c));
        }
        isPalindrome = true;
        while (isPalindrome && wordDeque.size() > 1) {
            isPalindrome = wordDeque.pollFirst().compareTo(wordDeque.pollLast()) == 0;
        }
    }
    return isPalindrome;
}

Upvotes: 0

David Choweller
David Choweller

Reputation: 1050

I made some very minor modifications (first to fix one of your for loops, and second to prevent your "Palindrome/Not a Palindrome" message from printing once for every character in the input) to your code to get it to work:

import java.util.Stack;
import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
class Palindrome {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true) {

            String line = reader.readLine();


            if (line.toLowerCase().equals("quit")) {
                break;
            }

            Stack<Character> stack = new Stack<Character>();

            Queue<Character> queue = new LinkedList<Character>();

            for (int i = 0; i < line.length(); i++) {
                stack.push(line.charAt(i));
            }

            for (int i = 0; i < line.length(); i++) {
                queue.add(line.charAt(i));
            }

            boolean isPalindrome=true;
            while (!queue.isEmpty()) {
                if (queue.remove().equals(stack.pop())) {
                    continue;
                } else {
                    isPalindrome=false;
                    break;
                }
            }
            if (!isPalindrome) {
                System.out.println("Not a Palindrome");
            } else {
                System.out.println("Palindrome");
            }

        }     
    }
}

Upvotes: 2

Douglas
Douglas

Reputation: 5087

You need to put the characters into each of the stack and the queue in the same order. The point of using both is that one reverses the order and the other doesn't. Reversing the order yourself on one of them, as you are doing now, negates that.

Upvotes: 1

Related Questions