Site icon TestingDocs.com

Design a Stack to separate positive and negative values

Problem Statement

Design a Stack with all standard functions and an additional method that moves all positive values from a stack into a second stack and all the negative values into a third stack.

Condition for the additional method to be met:
The values moved into the second and third stacks must maintain their original order from the first stack as shown in the below example picture. Create a Demo class to demonstrate the example output shown in the picture.

Picture and Sample Output

 

 

Solution is left as an exercise for the reader.

Hints:

Consider a method signature for the additional method as:

 

public void filterValues(Stack stack2,Stack stack3) {
    .....
}

Use a print method to print the elements of different stacks.

 

public void print(String stackName) { 
    .... 
}

 

Exit mobile version