Saeed Hassan Saeed Hassan - 1 month ago 8
Java Question

Stack Decompose into even and odd

This code decomposes the Stack into even and odd, how
do even.push and odd.push work?

public static ArrayStack decompose(ArrayStack x) {
ArrayStack y = new ArrayStack(x.size()/2);
ArrayStack odd = new ArrayStack(x.size()/2+1);
ArrayStack even = new ArrayStack(x.size()/2);
while(!x.isEmpty())
{
odd.push(x.pop());
if(!x.isEmpty())
even.push(x.pop());
}
while(!odd.isEmpty())
{
x.push(odd.pop());
}

while(!even.isEmpty())
{
y.push(even.pop());
}
return y;
}

Answer

push methods place the object passed onto the top of the stack.

even and odd are just the names of your Stacks.