Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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)t

Explanation / 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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote