【问题标题】:Stacks using push and pop使用 push 和 pop 的堆栈
【发布时间】:2016-08-16 04:28:48
【问题描述】:

目前我正在编写一个接收堆栈的方法,并且必须通过该堆栈并将所有偶数添加到新堆栈中,然后返回新堆栈。例如,如果堆栈包含 1,3,4,5,7,8,10...新堆栈在添加后将仅包含 4,8,10。到目前为止,这是我的代码,但似乎无法让它们通过。

public class StackExample {
public static Stack<Integer> getEvenNumbers(Stack<Integer> stack) {
Stack<Integer> a = stack;
Stack<Integer> c = new Stack<Integer>();
System.out.println(length(stack));
if(length(stack)==1)
{
    return stack;
}
while (!(a.isEmpty())) {
    int num = a.pop();
    if (num % 2 == 0) {
        c.push(num);
    } else {

    }
}
System.out.println(c.isEmpty());
return c;
}

这里是测试:

import static org.junit.Assert.*;

import java.lang.reflect.Field;

 import org.junit.Test;

public class StackExampleTest {
private class StackTest extends Stack<Integer> {
    public StackTest(int[] values) {
        for (int i = values.length-1; i > -1; i--) {
            push( values[ i ] );
        }
    }
}
@Test
public void testReflection() {
    Class<?> iClass  = StackExample.class;
    Field[]  iFields = iClass.getDeclaredFields();

    for (Field f : iFields) {
        if (!f.isSynthetic()) {
            fail( "Class shouldn't have any fields [found: \""+f.getName()+"\"]" );
        }
    }
}
@Test
public void testEmpty() {
    int[]          input  = new int[]{ };
    Stack<Integer> stack  = new StackTest( input );
    Stack<Integer> result = StackExample.getEvenNumbers( stack );

    assertTrue( "result should be empty", result.isEmpty() );
    assertTrue( "stack should be empty",  stack .isEmpty() );
    assertTrue( "stack and result cannot be the same object", stack !=    result );
}
@Test
public void test1Odd() {
    int[]          input   = new int[]{ 5 };
    Stack<Integer> stack   = new StackTest( input );
    Stack<Integer> result  = StackExample.getEvenNumbers( stack );

    assertTrue( "result should be empty", result.isEmpty() );

    for (int expected : input) {
        if (stack.isEmpty())
            fail( "\"stack\" empty: '"+ expected +"' expected" );
        else {
            int actual = stack.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void test1Even() {
    int[]          input   = new int[]{ 4 };
    Stack<Integer> stack   = new StackTest( input );
    Stack<Integer> result  = StackExample.getEvenNumbers( stack );

    for (int expected : new int[]{ 4 }) {
        if (result.isEmpty())
            fail( "\"result\" empty: '"+ expected +"' expected" );
        else {
            int actual = result.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    for (int expected : input) {
        if (stack.isEmpty())
            fail( "\"stack\" empty: '"+ expected +"' expected" );
        else {
            int actual = stack.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testNoneEven() {
    int[]          input   = new int[]{ 9, 77, 3, 5, 11 };
    Stack<Integer> stack   = new StackTest( input );
    Stack<Integer> result  = StackExample.getEvenNumbers( stack );

    assertTrue( "result should be empty", result.isEmpty() );

    for (int expected : input) {
        if (stack.isEmpty())
            fail( "\"stack\" empty: '"+ expected +"' expected" );
        else {
            int actual = stack.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testSomeEven() {
    int[]          input   = new int[]{ 44, 77, 8, 3, 5, 12 };
    Stack<Integer> stack   = new StackTest( input );
    Stack<Integer> result  = StackExample.getEvenNumbers( stack );

    for (int expected : new int[]{ 44, 8, 12 }) {
        if (result.isEmpty())
            fail( "\"result\" empty: '"+ expected +"' expected" );
        else {
            int actual = result.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    for (int expected : input) {
        if (stack.isEmpty())
            fail( "\"stack\" empty: '"+ expected +"' expected" );
        else {
            int actual = stack.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testAllEven() {
    int[]          input   = new int[]{ 12, 22, 6, 14, 12 };
    Stack<Integer> stack   = new StackTest( input );
    Stack<Integer> result  = StackExample.getEvenNumbers( stack );

    for (int expected : input) {
        if (result.isEmpty())
            fail( "\"result\" empty: '"+ expected +"' expected" );
        else {
            int actual = result.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    for (int expected : input) {
        if (stack.isEmpty())
            fail( "\"stack\" empty: '"+ expected +"' expected" );
        else {
            int actual = stack.pop();
            assertEquals( "incorrect result", expected, actual );
        }
    }
    assertTrue( "stack and result cannot be the same object", stack !=    result );
}
}

【问题讨论】:

    标签: java stack


    【解决方案1】:

    如果堆栈包含奇数,这将失败test1Odd

    if(length(stack)==1)
    {
        return stack;
    }
    

    要么放弃这个优化,要么检查它是否包含偶数元素。

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 2010-09-30
      • 1970-01-01
      • 2023-04-01
      • 2013-12-21
      • 2011-05-04
      • 2021-10-14
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多