4.Queue and Stack: Its points Is it possable to ?mplement a queue by stacks? The
ID: 3915037 • Letter: 4
Question
4.Queue and Stack: Its points Is it possable to ?mplement a queue by stacks? The answer is YES. In the describe a way Dequeue If outStack is empty, refill it by popping each element from inStack and pushing it onto outStack Pop and return the pop clement from outStack (1) Given the partial implementasion QueuelU singStacks class,please complete three methods sized Note: AmayStack class contains methods pop0, pushiObject ek we'd ?nEnp00 you can assume the two stacks are never full (with infinite space) private Arraystack inStack new Arraystack) private ArrayStack outstack new ArrayStack) public vold enqueue(object e)( Instack.push(e) public int si)tExplanation / Answer
public int size () {
int count=0;//To keep track of count
while(instack.isEmpty()){ // untill all elements are in outstack
outStack.push(inStack.pop());
count++;//increament count for each element
}
while(outStack.isEmpty()) //again put element is instack as it was because size should not modify //origanl state of queue
inStack.push(outStack.pop());
return count;
}
public boolean isEmpty(){
if(inStack.isEmpty() && outStack.isEmpty() )
return true;
else
return false;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.