Question
public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap]; index =-1; } public boolean isEmpty(){ return index ==
public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap]; index =-1; } public boolean isEmpty(){ return index == -1; } public boolean isFull(){ return index==data.length -1; } public NumStack pop(){ if(!isEmpty()) data[index--]=null; return this; } public int size(){ return index+1; } public Integer top(){ if(isEmpty()) return null; return data[index]; } public NumStack push(int num){ if(index < data.length-1) data[++index]=num; return this; } public int compareTo(NumStack s){} }
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
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