简体   繁体   English

谁能告诉我我在做什么错? -堆栈

[英]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. 每个周期一次。

  1. Your while loop should be wrapping the conditionals right after it. 您的while循环应在紧随其后包装条件。
  2. Your 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.

相关问题 有人可以告诉我在Java中设置此Robot类时我做错了什么吗? - Can someone tell me what I'm doing wrong to setup this Robot class in Java? 有人能告诉我我做错了什么吗? 通过LinkedList计数和循环 - Can someone tell me what i'm doing wrong? Counting and looping through LinkedList HackerRank 任务“Mini Max Sum”解决方案未通过 13 个测试用例中的 3 个,有人能告诉我我做错了什么吗 - HackerRank Task “Mini Max Sum” solution not passing 3 of the 13 test cases, can someone tell me what i'm doing wrong 请告诉我我做错了什么,if 语句和转换不起作用 - Please tell me what I'm doing wrong, the if statements and the conversions are not working 谁能告诉我这段代码出了什么问题? - Can anyone tell me what's going wrong with this code? 谁能告诉我这个递归函数出了什么问题? - Can anyone tell me what's going wrong with this recursive function? 任何人都可以告诉我这个伪代码怎么了? - Anyone can tell me what's wrong with this pseudo code? 谁能告诉我在这种情况下我哪里错了? - Can anyone tell me where i am wrong in this condition? 谁能告诉我以下程序做错了什么? 我收到类似的错误<identifier expected>和“&#39;;预期” - Can anybody tell me what am i doing wrong with the following program? I am getting errors like <identifier expected> and " '; expected" 谁能告诉我我做错了什么,“将数据从一个片段发送到另一个片段”? - Can anyone tell where i am doing it wrong, “sending the data from fragment to fragment”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM