Question: Create a class named NumStack.java, copy the content from NumStack.txt then implement the public int compareTo(NumStacj s) method. public int compareTo(NumStack s) compares two stack
Create a class named NumStack.java, copy the content from NumStack.txt then implement the public int compareTo(NumStacj s) method.
public int compareTo(NumStack s) compares two stack from the bottom of each stack, for example, s1[2,0, 9] and s2[9,0,9], calling s1.compareTo(s2) should return -1, s1 s2 top 9 9 3rd: 9==9 we get 0 0 0 2nd: 0==0 we get 0 bottom 2 9 1st: 2<9 we get -1 ------------------------------------------ add all and return: if the two stacks have different sizes, then consider ith item is greater than "none-existing item" in smaller stack. for example, s1[8,0, -9] s2[1], calling s1.compareto(s2) should return 3, s1 s2 top -9 3rd: no ->1 0 2nd: no item in s2 ->1 bottom 8 1 1st: 8>1 ->1 ----------------------------------------- add all and return: 3
Note: This method should not change the structure or contents of the caller and argument stack
public class NumStack implements Comparable
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
