[英]Can anyone tell me what I'm doing wrong? - Stacks
I am required to write a method, compress to do the following; 我需要编写一个方法,执行以下压缩操作;
The objective of the method compress is to remove all null elements from the stack s1. 方法compress的目标是从堆栈s1中删除所有空元素。 The remaining (non-null) elements should be kept on s1 in their initial order.
其余(非空)元素应按其初始顺序保留在s1上。 The auxiliary stack s2 should be used as a temporary storage for the elements from s1.
辅助堆栈s2应该用作s1中元素的临时存储。 At the end of the computation of this method, stack s2 should have the same content as at the beginning of the computation.
在此方法的计算结束时,堆栈s2应具有与计算开始时相同的内容。 See the method main for an example of the expected behaviour of the method compress.
有关方法compress预期行为的示例,请参见方法main。
So far I have; 到目前为止,我有;
import net.datastructures.ArrayStack;
import net.datastructures.Stack;
public class Stacks {
public static <E> void compress(Stack<E> S1, Stack<E> S2) {
int counter = 0;
while (!S1.isEmpty()) {
}
if (S1.top() == null) {
S1.pop();
} else if (S1.top() != null) {
S2.push(S1.pop());
counter++;
}
for (int i = counter; i < counter; i++) {
S2.push(S1.pop());
}
}
public static void main(String[] args) {
// test method compress
Stack<Integer> S1 = new ArrayStack<Integer>(10);
S1.push(2);
S1.push(null);
S1.push(null);
S1.push(4);
S1.push(6);
S1.push(null);
Stack<Integer> S2 = new ArrayStack<Integer>(10);
S2.push(7);
S2.push(9);
System.out.println("stack S1: " + S1);
// prints: "stack S1: [2, null, null, 4, 6, null]"
System.out.println("stack S2: " + S2);
// prints: "stack s2: [7, 9]"
compress(S1, S2);
System.out.println("stack S1: " + S1);
// should print: "stack S1: [2, 4, 6]"
System.out.println("stack S2: " + S2);
// should print: "stack S2: [7, 9]"
}
}
I can't figure out where I'm going wrong, the code prints the two lines before the compress method and then prints nothing. 我无法弄清楚哪里出了问题,代码在compress方法之前打印了两行,然后什么也不打印。
while (!S1.isEmpty()) {
}
Right there, you have an infinite loop. 在那里,您有一个无限循环。
i guess writing the if...else inside your while() as below 我想在if()里面写if ... else如下
while (!S1.isEmpty()) {
if (S1.top() == null) {
S1.pop();
} else if (S1.top() != null) {
S2.push(S1.pop());
counter++;
}
} }
and inside your for it must be something like 在你的里面,因为它一定是
for (int i = counter; i < counter; i++) {
S1.push(S2.pop());
}
guess it should work 猜它应该工作
I spotted two mistakes in your code. 我在您的代码中发现了两个错误。 One on each of your cycles.
每个周期一次。
while
loop should be wrapping the conditionals right after it. while
循环应在紧随其后包装条件。 for
loop has its increment variable badly assigned. for
循环的增量变量分配错误。 Correct version should be: 正确的版本应为:
public static <E> void compress(Stack<E> S1, Stack<E> S2) {
int counter = 0;
while (!S1.isEmpty()) {
if (S1.top() == null) {
S1.pop();
} else {
S2.push(S1.pop());
counter++;
}
}
for (int i = 0; i < counter; i++) {
S1.push(S2.pop());
}
}
Edit: An equivalent for
loop (maybe you were trying to write this one) may be as follows. 编辑:等效的
for
循环(也许您正在尝试编写此循环)可能如下。
for (int i = counter; i > 0; i--) {
S1.push(S2.pop());
}
2nd edit: The variables in the for
loops were switched (S2 was in S1's place and vice versa). 第二次编辑:切换了
for
循环中的变量(S2位于S1的位置,反之亦然)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.