Skip to content

Commit ea328b1

Browse files
committed
Add stack code
1 parent 1a29ccf commit ea328b1

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package datastructure.stack;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Stack;
6+
7+
import static org.hamcrest.CoreMatchers.is;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class IsPalindromeTest {
11+
12+
/*
13+
TASK
14+
Stack 자료구조를 사용하여 회문을 판별한다.
15+
*/
16+
17+
@Test
18+
public void test() {
19+
assertThat(isPalindrome("abba"), is(true));
20+
}
21+
22+
public boolean isPalindrome(String str) {
23+
Stack<Character> stack = new Stack<>();
24+
char[] charArr = str.toCharArray();
25+
for (int i = 0; i < str.length(); i++) {
26+
if (i <= str.length() / 2) {
27+
stack.add(charArr[i]);
28+
} else {
29+
if (stack.pop() != charArr[i]) {
30+
return false;
31+
}
32+
}
33+
}
34+
return true;
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package datastructure.stack;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class MyStackWithArrayListTest {
12+
@Test
13+
public void test() {
14+
MyStackWithArrayList<Integer> stack = new MyStackWithArrayList<>();
15+
16+
stack.push(0);
17+
stack.push(1);
18+
stack.push(2);
19+
stack.push(3);
20+
stack.push(4);
21+
stack.push(5);
22+
stack.push(6);
23+
24+
assertThat(6, is(stack.pop()));
25+
assertThat(5, is(stack.pop()));
26+
assertThat(4, is(stack.pop()));
27+
assertThat(3, is(stack.pop()));
28+
assertThat(2, is(stack.pop()));
29+
assertThat(1, is(stack.pop()));
30+
assertThat(0, is(stack.pop()));
31+
}
32+
33+
public class MyStackWithArrayList<T> {
34+
private List<T> data;
35+
36+
public MyStackWithArrayList() {
37+
data = new ArrayList<T>();
38+
}
39+
40+
public void push(T i) {
41+
data.add(i);
42+
}
43+
44+
public T pop() {
45+
return data.remove(data.size() - 1);
46+
}
47+
}
48+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package datastructure.stack;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class MyStackWithArrayTest {
9+
@Test
10+
public void test() {
11+
MyStackWithArray stack = new MyStackWithArray();
12+
13+
stack.push(0);
14+
stack.push(1);
15+
stack.push(2);
16+
stack.push(3);
17+
stack.push(4);
18+
stack.push(5);
19+
stack.push(6);
20+
21+
assertThat(6, is(stack.pop()));
22+
assertThat(5, is(stack.pop()));
23+
assertThat(4, is(stack.pop()));
24+
assertThat(3, is(stack.pop()));
25+
assertThat(2, is(stack.pop()));
26+
assertThat(1, is(stack.pop()));
27+
assertThat(0, is(stack.pop()));
28+
29+
// java.lang.RuntimeException: Empty Stack!
30+
// assertThat(0, is(stack.pop()));
31+
}
32+
33+
public class MyStackWithArray {
34+
private int[] data = new int[5];
35+
private int topIndex = -1;
36+
37+
public synchronized void push(int i) {
38+
topIndex++;
39+
if (topIndex >= data.length) {
40+
int[] oldData = data;
41+
data = new int[data.length * 2];
42+
// System.arraycopy(oldData, 0, data, 0, oldData.length);
43+
for (int j = 0; j < oldData.length; j++) {
44+
data[j] = oldData[j];
45+
}
46+
}
47+
48+
data[topIndex] = i;
49+
}
50+
51+
public synchronized int pop() {
52+
if (topIndex < 0) {
53+
throw new RuntimeException("Empty Stack!");
54+
}
55+
// int result = data[topIndex];
56+
// topIndex--;
57+
return data[topIndex--];
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)