Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone please do this assigment im really struggling Stack Permutation Stack permutations are performed using two queues and one stack. Elements from the input

can someone please do this assigment im really struggling image text in transcribed
image text in transcribed
Stack Permutation Stack permutations are performed using two queues and one stack. Elements from the input queue inputQueue can only be dequeued. Elements can only be enqueued to the output queue outputQueue. You can do push and pop operations on the stack middleStack. The operations allowed are: If inputQueue is not empty, dequeue (remove) the element in the front of inputQueue and place it at the end of outputQueue. If inputQueue is not empty, dequeue (remove) the element in the front of inputQueue and push it onto middleStack. If middleStack is not empty, pop (remove) the element at the top and enqueue (add) it at the end of outputQueue. Suppose we have the numbers 1, 2, 3, 4, 5 in the inputQueue. We perform the following operations: enqueue (outputQueue, dequeue(inputQueue)) // 1 is to outputQueue enqueue (outputQueue, dequeue (inputQueue)) // 2 is to outputQueue push(middleStack, dequeue (inputQueue)) // 3 is to middleStack push(middleStack, dequeue (inputQueue)) // 4 is to middleStack enqueue (outputQueue, dequeue(inputQueue)) // 5 is to outputQueue enqueue (outputQueue, pop (middleStack)) // 4 is to outputQueue enqueue (outputQueue, pop (middleStack)) // 3 is to outputQueue The outputQueue will now have the sequence 1, 2,5, 4, 3. This is a stack permutation of the input sequence. Note that 1, 2,5, 3, 4 is not an example for stack permutation. Stack and Queue Interface In this assignment your task is to implement a Java program to check whether a given output (sequence of numbers) is a stack permutation. You will use the stack and queue interfaces as described in Queue.java and Stack.java in the class. You must implement these Interfaces using the ArrayQueue.java and Arraystack.java. You must use the 4 files (discussed in class) to do the assignment. You are NOT allowed to use the default stack and queue implementations available in Java. You are NOT allowed to use your own implementation of queue and stack. Inputs and Outputs of the program The input to your program will be the output sequence. For example, your sequence can be: 1,2,5,4,3 COMP250: Programming Assignment 2 You should check if the remaining numbers are a stack permutation of input queue 1,2,3,4,5. In this case, the output sequence is a stack permutation you will return true. However 1.2.5.3.4 is NOT a stack permutation. So, return false. As another example, if you get: 2,1,3,4,6,5, check if the remaining numbers are a stack permutation. In this case, the output sequence is a stack permutation again and you return true. However 2.1.4.6.3.5 is NOT a stack permutation. So, return false. Hint: Stack Permutation Check Algorithm Step 1. Get all elements from input (from Assignment Test.java-JUnit Test) enqueue all the elements to outputQueue enqueue the inputQueue with the numbers of 1 ton Step 2. dequeue one element from the inputQueue and check if it is equal to the top of outputQueue. If so, dequeue one element from both inputQueue and outputQueue If not, push the element to middleStack. You will also need to compare the top of middleStack with the outputQueue. If equal, pop and dequeue element from middleStack and outputQueue If not equal, go to Step 2 Repeat Step 2 until the inputQueue and middleStack becomes empty. At the end, if the both queues and stack are empty then the input queue is a stack permutation. JUnit Test In this assignment, you will run unit test to check if your code is correctly written I will test your code with the following code. import static org.junit.jupiter.api.Assertions.*; import org.Junit.jupiter.api. Test; class Assignment2Test { @Test void test() { assert True (Assignment2.stackPermutation(new int[] {1,2,5,4,3 }); assertFalse(Assignment2.stackPermutation(new int[] (1,2,5,3,4 }); assert True (Assignment2.stackPermutation(new int[] {2,1,3,4,6,5 }); assert True (Assignment2.stackPermutation(new int[] (2,1,4,6,5,3 }); assertFalse(Assignment2.stackPermutation(new int[] {2,1,6,3,4,5 }); assert True (Assignment2.stackPermutation(new int[] 0))); 1/ to pass the unit test, you must have the same following class name and method name. public class Assignment2 { public static boolean stackPermutation(int[] numbers) w Students will need to implement this method to pass the above unit test. // FYI, I have 35 lines of code total 2

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

Beyond Greed And Fear Understanding Behavioral Finance And The Psychology Of Investing

Authors: Hersh Shefrin

1st Edition

0195161211, 978-0195161212

Students also viewed these Databases questions