Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are only allowed to use integer arrays and ArrayStack data structures. You can step through the array from index 0 to n only. Given

You are only allowed to use integer arrays and ArrayStack data structures. You can step through the array from index 0 to n only. Given an array of numbers from 1 to 9, in order, reverse the array so that the numbers are now in decreasing order.

STARTER CODE:

Public class Stack_Queue_Driver {

// You would like a nice representation of a stack

public static void main(String[] args) {

// FOR OPTION 1

ArrayStack myStack = new ArrayStack();

myStack.push(1);

myStack.push(2);

myStack.push(3);

myStack.push(4);

myStack.push(5);

myStack.push(6);

myStack.push(7);

myStack.push(8);

myStack.push(9);

System.out.println("My stack:");

displayS(myStack);

System.out.println();

ArrayStack reversed = reverseStack(myStack);

System.out.println("My reversed stack:");

displayS(reversed);

System.out.println();

// OPTION 1: Your code here to reverse the Stack:

// Your code should work on a stack of any size.

private static ArrayStack reverseStack(ArrayStack as) {

// *** your code here ***

return as; // you may change the return value

}

// You would like a nice representation of a stack

// to be displayed to the console. This method is provided

// and should work once reverseStack is implemented.

private static void displayS(ArrayStack as) {

// TODO Auto-generated method stub

int numItems = as.size();

String toDisplay = "bottom~";

ArrayStack tempStack = reverseStack(as);

for (int i = 0; i

toDisplay += tempStack.pop() + "~";

}

for (int i = 0; i

System.out.print(toDisplay.charAt(i));

}

System.out.println("top");

}

}

The output of the program with both problems solved should be:

image text in transcribed

Stack_Queue_Solutions [Java Application] OPTION 1 My stack: bottom~1~2~3~4~5~6~7~8~9~top My reversed stack: bottom~9~8~7~6~5~4~3~2~1~top OPTION 2 My queue: front~3~5~2~1~4~back My sorted queue: front~1~2~3~4~5~back

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions