java - copying Queue and Stack into each other -
i want use empty stack s check whether queue q contaitns element x, solution copy elements of q s , check if contain x, i'm asked return s elements q again originaly, must done using q , s without other arrays of sl, have written algorithm:
boolean found ← false int element ← 0 while(q not empty) element ← q.dequeue() if(element equal x) found ← true s.push(element) while(s not empty) ( elements q) ?
stuck in last step, if used q.enqueue(s.pop) order of elements in q reversed
correct, order reversed. means if repeat algorithm yet again (all s , q) reverse yet again, meaning original order of elements, want.
Comments
Post a Comment