@Test public void testPushStack() { Deck deck = new Deck(); deck.shuffleDeck(); Stack stack = new Stack(); Cards aCard = deck.drawACard(); stack.pushCard(aCard, 0, 0); System.out.print( stack.top(0, 0).getValue() + " " + stack.top(0, 0).getRank() + " " + stack.top(0, 0).getSuit() + "\n"); }
@Test public void testTop() throws Exception { stack = new Stack<Object>(); stack.push(5); stack.push(4); stack.push(3); stack.push(2); stack.push(1); Assert.assertEquals(1, stack.top()); stack.pop(); Assert.assertEquals(2, stack.top()); }
@SuppressWarnings("unused") @Test(expected = NoSuchElementException.class) public void testTopEmptyStack() { // tack stack = new Stack(2); Integer top = stack.top(); fail("Top on empty stack"); }
@Test public void testTop1() { // Stack stack = new Stack(2); int value = 460; stack.push(value); int top = stack.top(); assertEquals("Stack value is ok", value, top); }
@SuppressWarnings("unused") @Test public void testTopWithIsEmpty() { // Stack stack = new Stack(2); stack.push(0); int top = stack.top(); assertFalse("Stack size = 1", stack.isEmpty()); }
@Test public void testTopN() { // Stack stack = new Stack(10); int values[] = {460, 6, 9, 8, 7, 41, 3, 67, 5}; for (int i = 0; i < values.length; i++) { stack.push(values[i]); } int top = stack.top(); assertEquals("Top value is ok", values[values.length - 1], top); }
@Test public void testNTop() { // Stack stack = new Stack(2); int value = 460; int top = 0; stack.push(value); for (int i = 0; i < 10; i++) { top = stack.top(); } assertEquals("Top value is the same", value, top); }
@Test public void testTopStack() { Deck deck = new Deck(); deck.shuffleDeck(); Stack stack = new Stack(); stack.dealFour(); for (int i = 0; i < 4; i++) { System.out.print( stack.top(0, i).getValue() + " " + stack.top(0, i).getRank() + " " + stack.top(0, i).getSuit() + "\n"); } stack.dealFour(); for (int i = 0; i < 4; i++) { System.out.print( stack.top(0, i).getValue() + " " + stack.top(1, i).getRank() + " " + stack.top(1, i).getSuit() + "\n"); } }
/** * 练习1.3.3扩展 判断出栈操作是否合理 * * @param array 出栈操作返回值数组[0-9] * @return 结果 */ static boolean checkPopStackOrderPossible(int[] array) { /* 思路: 遍历返回值数组 将比返回值相等或小的所有数都入栈,再将该返回值出栈 */ Stack<Integer> stack = new Stack<>(); stack.setNeedPrintLog(true); Stack<Integer> hasPushedStack = new Stack<>(); List<Integer> hasPushedList = new ArrayList<>(); for (int i = 0; i < array.length; i++) { int value = array[i]; if (hasPushedStack.isEmpty() || value > hasPushedStack.top()) { int minus = hasPushedStack.isEmpty() ? 0 : hasPushedStack.top() + 1; for (int j = minus; j <= value; j++) { stack.push(j); hasPushedStack.push(j); } } if (value == stack.top()) { stack.pop(); } else { System.out.println(value + "==出栈顺序错误!!"); Iterator<Integer> iterator = stack.iterator(); System.out.println("栈里剩余元素为:"); while (iterator.hasNext()) { Integer next = iterator.next(); System.out.print(" " + next); } return false; } } System.out.println("出栈顺序正确"); return true; }