Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(10 marks) Give code for the method compress in the class Stacks shown below. This method has two arguments, the main stack s1 and an
(10 marks) Give code for the method compress in the class Stacks shown below. This method has two arguments, the "main" stack s1 and an auxiliary stack s2, both of the generic type Stack. This generic type Stack is the interface discussed in Lecture 4 (and is included in the net.datastructures package for the Goodrich-Tamassia-Goldwasser textbook). The objective of the method compress is to remove all null elements from the stack s1. The remaining (non-null) elements should be kept on s1 in their initial order. The stack s2 should be used as a temporary storage for the elements from s1. At the end of the computation of this method, stack s2 should have the same content as at the beginning of the computation. The method should not use any arrays. See the method main below for an example of the expected behaviour of the method compress. Remark. The method compress only knows that s1 and s2 are implementations of the interface Stack, but does not know anything about the details of those implementations. This means that method compress can access s1 and s2 only by using the interface methods. package labsSGT sCoursework.cw1; import net.datastructures. Stack; import net.datastructures. ArrayStack; public class Stacks { public static void compress (Stack s1, Stack s2) { ... // YOUR CODE REPLACES DOTS HERE public static void main(String[] args) { // test method compress Stack S = new ArrayStack(10); S.push(2); S.push(null); S.push(null); S.push(4); S.push(6); S.push(null); Stack X = new ArrayStack(10); X.push(7); X.push(9); System.out.println("stack S: " + S); // prints: "stack S: [2, null, null, 4, 6, null)" System.out.println("stack X: " + X): // prints: "stack X: [7, 9] " compress (S, X); System.out.println("stack S: " + S); // should print: "stack S: [2, 4, 6)". System.out.println("stack X: " + x); // should print: "stack X: [7, 9]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started