Using a queue and a stack, implement a method that tests whether a sequence of numbers is a palindrome, that is, equal to the sequence in reverse. For example, [1,2,3,2,1] is a palindrome, but [1,2,2,3,1] is not.
Complete the following file:
QueueStackUtil.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class QueueStackUtil
{
public static boolean isPalindrome(int[] values)
{
Queue<Integer> queue = new LinkedList<Integer>();
Stack<Integer> stack = new Stack<Integer>();
for (int s : values)
{
// insert s into the queue and stack
}
while (queue.size() > 0)
{
// remove an element from the queue
// remove an element from the stack
// compare the removed elements
}
return ...;
}
}
QueueStackUtil.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class QueueStackUtil {
public static boolean isPalindrome(int[] values) {
Queue < Integer > queue = new LinkedList < Integer > ();
Stack < Integer > stack = new Stack < Integer > ();
for (int s: values) {
stack.push(s);
queue.add(s);
}
int x, y;
while (queue.size() > 0) {
// remove an element from the queue
x = queue.remove();
// remove an element from the stack
y = stack.pop();
// compare the removed elements
if(x != y)
break;
}
boolean isP;
if(x != y)
isP = false;
else
isP = true
return isP;
}
}
if you have any doubt, feel free to ask in the comments.
Get Answers For Free
Most questions answered within 1 hours.