How can we write a function List multiPop(int k) that pops k elements from the stack or until the stack is empty?
so far I have tried: public void multipop(int k) {
while (top != null) {
for(int i =0; i < k; i++) {
this.pop();
}
}
}
Printable View
How can we write a function List multiPop(int k) that pops k elements from the stack or until the stack is empty?
so far I have tried: public void multipop(int k) {
while (top != null) {
for(int i =0; i < k; i++) {
this.pop();
}
}
}
What is top? And since you are apparently not keeping any of the values it is not necessary to pop anything. Just use the value of k to reset the index of the stack. This does make an assumption on how the stack is implemented.
Regards,
Jim
Moved from Advanced Java. Please confine homework questions to the New to Java section.
db