-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bobo
committed
Jun 28, 2020
1 parent
8421189
commit 3880776
Showing
6 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/spring2go/algorithms/datastructures/stack/ListStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.spring2go.algorithms.datastructures.stack; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* 基于链表实现的栈. | ||
* | ||
* 用你的实现代码替换YOUR CODE HERE. | ||
* | ||
* Created on Jun, 2020 by @author bobo | ||
*/ | ||
public class ListStack<T> implements Iterable<T> { | ||
|
||
private java.util.LinkedList<T> list = new java.util.LinkedList<>(); | ||
|
||
// 创建一个空栈 | ||
public ListStack() {} | ||
|
||
// 创建一个带有一个初始元素的栈 | ||
public ListStack(T firstElem) { | ||
// YOUR CODE HERE | ||
} | ||
|
||
// 返回栈中的元素个数 | ||
public int size() { | ||
// YOUR CODE HERE | ||
return 0; | ||
} | ||
|
||
// 检查栈是否为空 | ||
public boolean isEmpty() { | ||
// YOUR CODE HERE | ||
return false; | ||
} | ||
|
||
// 从栈中弹出一个元素 | ||
// 如果栈空就抛出一个异常 | ||
public T pop() { | ||
// YOUR CODE HERE | ||
return null; | ||
} | ||
|
||
// 查看栈顶元素(并不移除) | ||
// 如果栈空就抛出一个异常 | ||
public T peek() { | ||
// YOUR CODE HERE | ||
return null; | ||
} | ||
|
||
// 将一个元素入栈 | ||
public void push(T elem) { | ||
// YOUR CODE HERE | ||
} | ||
|
||
// 支持以迭代器方式对栈进行遍历 | ||
@Override | ||
public Iterator<T> iterator() { | ||
// YOUR CODE HERE | ||
return null; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/test/java/com/spring2go/algorithms/datastructures/stack/StackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.spring2go.algorithms.datastructures.stack; | ||
|
||
/** | ||
* Created on Jun, 2020 by @author bobo | ||
*/ | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class StackTest { | ||
|
||
private ListStack<Integer> stack = new ListStack<>(); | ||
|
||
@Test | ||
public void testEmptyStack() { | ||
assertTrue(stack.isEmpty()); | ||
assertEquals(stack.size(), 0); | ||
} | ||
|
||
@Test | ||
public void testPopOnEmpty() { | ||
assertThrows(Exception.class, stack::pop); | ||
} | ||
|
||
@Test | ||
public void testPeekOnEmpty() { | ||
assertThrows(Exception.class, stack::peek); | ||
} | ||
|
||
@Test | ||
public void testPush() { | ||
stack.push(2); | ||
assertEquals(stack.size(), 1); | ||
} | ||
|
||
@Test | ||
public void testPeek() { | ||
stack.push(2); | ||
assertEquals(2, (int) (Integer) stack.peek()); | ||
assertEquals(stack.size(), 1); | ||
} | ||
|
||
@Test | ||
public void testPop() { | ||
stack.push(2); | ||
assertEquals(2, (int) stack.pop()); | ||
assertEquals(stack.size(), 0); | ||
} | ||
|
||
@Test | ||
public void testExhaustively() { | ||
assertTrue(stack.isEmpty()); | ||
stack.push(1); | ||
assertFalse(stack.isEmpty()); | ||
stack.push(2); | ||
assertEquals(stack.size(), 2); | ||
assertEquals(2, (int) stack.peek()); | ||
assertEquals(stack.size(), 2); | ||
assertEquals(2, (int) stack.pop()); | ||
assertEquals(stack.size(), 1); | ||
assertEquals(1, (int) stack.peek()); | ||
assertEquals(stack.size(), 1); | ||
assertEquals(1, (int) stack.pop()); | ||
assertEquals(stack.size(), 0); | ||
assertTrue(stack.isEmpty()); | ||
} | ||
} |