The Java code checks whether the queue of characters is a palindrome or not. A palindrome is a sequence of characters that reads the same backward as forward.
The code uses two queues: the original queue and a reversed queue (created by copying the elements of the original queue). It then compares corresponding elements from both queues to determine if the original queue is a palindrome.
import java.util.LinkedList; import java.util.Queue; public class QueuePalindrome { public static void main(String[] args) { Queue<Character> queue = new LinkedList<>(); // Adding elements to the queue queue.add('r'); queue.add('a'); queue.add('d'); queue.add('a'); queue.add('r'); // Checking if the queue is palindrome boolean isPalindrome = true; Queue<Character> reverseQueue = new LinkedList<>(queue); while (!queue.isEmpty()) { char c1 = queue.remove(); char c2 = reverseQueue.remove(); if (c1 != c2) { isPalindrome = false; break; } } if (isPalindrome) { System.out.println("The Queue is a Palindrome"); } else { System.out.println("The Queue is not a Palindrome"); } } }
The Queue is a Palindrome
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions