you have a stack aStack and an empty auxiliary stack auxStack. Show how you can
ID: 3530904 • Letter: Y
Question
you have a stack aStack and an empty auxiliary stack auxStack. Show how you can do each of the following tasks by using only the operations of the ADT stack: a) Display the contents of aStack in reverse order; that is display the top last. public static void ex04a() { StackInterface aStack = new StackReferenceBased(); StackInterface auxStack = new StackReferenceBased(); aStack.push(3);//on the bottom of stack aStack.push(2); aStack.push(1);//on top of stack ////////////////////////////////////////missing code needed/////////////////////////////////// printStack(auxStack); } b) Count the number of items in aStack, leaving aStack unchanged. public static void ex04b() { StackInterface aStack = new StackReferenceBased(); StackInterface auxStack = new StackReferenceBased(); aStack.push(3);//on the bottom of stack aStack.push(2); aStack.push(1);//on top of stack int size = 0; ////////////////////////////////////////missing code needed/////////////////////////////////// System.out.println("The size of the stack is : " + size);; } c) Delete every occurrence ofExplanation / Answer
A part is working fine.. i have checked it in eclipse.
I am modifying the rest of the part.check out
----------------------------------------------------------
import java.util.Stack;
public class StackDemo
{
private static void printStack(Stack auxStack) {
while(!auxStack.isEmpty())
{
System.out.println(auxStack.peek());
auxStack.pop();
}
}
public static void ex04a()
{
Stack aStack = new Stack();
Stack auxStack = new Stack();
aStack.push(3);
//on the bottom of stack
aStack.push(2);
aStack.push(1);
auxStack.push(aStack.pop());
auxStack.push(aStack.pop());
auxStack.push(aStack.pop());
System.out.println("Stack in reverse :");
printStack(auxStack);
}
//b) Count the number of items in aStack, leaving aStack unchanged.
public static void ex04b()
{
Stack aStack = new Stack();
Stack auxStack = new Stack();
aStack.push(3);
//on the bottom of stack
aStack.push(2);
aStack.push(1);
//on top of stack
int size = 0;
System.out.println("The size of the stack is : " + size);;
}
//c) Delete every occurrence of
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.