Skip to content

Commit

Permalink
add source code for stack
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo committed Jun 28, 2020
1 parent 8421189 commit 3880776
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Binary file added slides/datastructures/stack/Stack.pdf
Binary file not shown.
Binary file added slides/datastructures/stack/Stack.pptx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.util.Iterator;

/**
* 双向链表实现.
*
* 用你的实现代码替换YOUR CODE HERE.
*
* Created on Jun, 2020 by @author bobo
*/
public class DoublyLinkedList<T> implements Iterable<T> {
Expand Down Expand Up @@ -88,6 +92,7 @@ public T removeLast() {
}

// 移除链表中的一个指定的节点,O(1)
// 内部使用
private T remove(Node<T> node) {
// YOUR CODE HERE
return null;
Expand Down
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;
}
}
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());
}
}

0 comments on commit 3880776

Please sign in to comment.