Write a method
equals
that takes as parameters two stacks of integers and returns true if the two stacks are equal and that returns false otherwise. To be considered equal, the two stacks would have to store the same sequence of integer values in the same order. Your method is to examine the two stacks but must return them to their original state before terminating. You may use one stack as auxiliary storage.
I'm pretty new to stacks and queues, and data structures in general. I'd like to know if there are other ways I can do this that are easier or better for when I'm dealing with stacks in the future. Also, is my code readable? Does everything "make sense"? And can my code be easily optimized?
public boolean equals(Stack<Integer> s1, Stack<Integer> s2) {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();
boolean equal = true;
while (!s1.isEmpty() && !s2.isEmpty()) {
int firstNum = s1.pop();
int secondNum = s2.pop();
q1.offer(firstNum);
q2.offer(secondNum);
if (firstNum != secondNum) {
equal = false;
}
}
// used to preserve stack structure
if (s1.isEmpty() && !s2.isEmpty()) {
while(!s2.isEmpty()) {
q2.offer(s2.pop());
}
equal = false;
}
if (s2.isEmpty() && !s1.isEmpty()) {
while(!s1.isEmpty()) {
q1.offer(s1.pop());
}
equal = false;
}
// add back to stack using s -> q -> s
// for both stacks
while (!q1.isEmpty()) {
s1.push(q1.remove());
}
while (!s1.isEmpty()) {
q1.offer(s1.pop());
}
while (!q1.isEmpty()) {
s1.push(q1.remove());
}
while (!q2.isEmpty()) {
s2.push(q2.remove());
}
while (!s2.isEmpty()) {
q2.offer(s2.pop());
}
while (!q2.isEmpty()) {
s2.push(q2.remove());
}
return equal;
}