Skip to content

Commit 5a1dfaf

Browse files
authored
Merge pull request #24 from Data-Structure-Study/yoonexample
윤성우의 자료구조 스택 부분 구현
2 parents ac2586f + fda41b6 commit 5a1dfaf

File tree

5 files changed

+404
-0
lines changed

5 files changed

+404
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package stack;
2+
3+
import java.util.EmptyStackException;
4+
5+
public class ArrayStack<E> implements Stack<E> {
6+
7+
private static final int INITIAL_CAPACITY = 100;
8+
9+
private int topIndex;
10+
private Object[] arr;
11+
12+
public ArrayStack() {
13+
this.topIndex = -1;
14+
this.arr = new Object[INITIAL_CAPACITY];
15+
}
16+
17+
@Override
18+
public int size() {
19+
return this.topIndex + 1;
20+
}
21+
22+
@Override
23+
public boolean isEmpty() {
24+
return this.topIndex == -1;
25+
}
26+
27+
@Override
28+
public void push(E data) {
29+
this.topIndex++;
30+
arr[topIndex] = data;
31+
}
32+
33+
@Override
34+
public E pop() {
35+
if (isEmpty()) {
36+
throw new EmptyStackException();
37+
}
38+
this.topIndex--;
39+
return (E) arr[topIndex + 1];
40+
}
41+
42+
@Override
43+
public E peek() {
44+
if (isEmpty()) {
45+
throw new EmptyStackException();
46+
}
47+
return (E) arr[topIndex];
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package stack;
2+
3+
import java.util.EmptyStackException;
4+
import list.CircularLinkedList;
5+
import list.List;
6+
7+
public class CircularListStack<E> implements Stack<E> {
8+
9+
private final List<E> list = new CircularLinkedList<>();
10+
private int lastIndex = -1;
11+
12+
@Override
13+
public int size() {
14+
return this.list.size();
15+
}
16+
17+
@Override
18+
public boolean isEmpty() {
19+
return this.list.isEmpty();
20+
}
21+
22+
@Override
23+
public void push(E data) {
24+
this.lastIndex++;
25+
this.list.insert(data);
26+
}
27+
28+
@Override
29+
public E pop() {
30+
if (isEmpty()) {
31+
throw new EmptyStackException();
32+
}
33+
E tmpData = this.list.remove(lastIndex);
34+
this.lastIndex--;
35+
return tmpData;
36+
}
37+
38+
@Override
39+
public E peek() {
40+
if (isEmpty()) {
41+
throw new EmptyStackException();
42+
}
43+
return this.list.get(lastIndex);
44+
}
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package stack;
2+
3+
import java.util.EmptyStackException;
4+
5+
public class ListStack<E> implements Stack<E> {
6+
7+
private int size;
8+
private Node<E> head;
9+
10+
@Override
11+
public int size() {
12+
return this.size;
13+
}
14+
15+
@Override
16+
public boolean isEmpty() {
17+
return this.size == 0;
18+
}
19+
20+
@Override
21+
public void push(E data) {
22+
Node<E> newNode = new Node<>(data);
23+
newNode.next = this.head;
24+
this.head = newNode;
25+
this.size++;
26+
}
27+
28+
@Override
29+
public E pop() {
30+
if (isEmpty()) {
31+
throw new EmptyStackException();
32+
}
33+
Node<E> tmpNode = this.head;
34+
this.head = this.head.next;
35+
size--;
36+
return tmpNode.data;
37+
}
38+
39+
@Override
40+
public E peek() {
41+
if (isEmpty()) {
42+
throw new EmptyStackException();
43+
}
44+
return this.head.data;
45+
}
46+
47+
private static class Node<T> {
48+
49+
private T data;
50+
private Node<T> next;
51+
52+
public Node(T data) {
53+
this.data = data;
54+
}
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package stack;
2+
3+
/**
4+
* 스택 자료구조의 ADT, 인터페이스
5+
*
6+
* @param <E> 데이터의 파라미터 타입
7+
* @author dion
8+
*/
9+
public interface Stack<E> {
10+
11+
/**
12+
* 스택에 저장된 데이터의 개수를 반환합니다.
13+
*
14+
* @return 데이터의 개수
15+
*/
16+
int size();
17+
18+
/**
19+
* 스택이 비어있는지 여부를 반환합니다.
20+
*
21+
* @return 스택이 비어있으면 true, 그렇지 않으면 false
22+
*/
23+
boolean isEmpty();
24+
25+
/**
26+
* 스택에 파라미터로 전달된 데이터를 마지막에 저장합니다.
27+
*
28+
* @param data 저장할 데이터
29+
*/
30+
void push(E data);
31+
32+
/**
33+
* 스택의 마지막에 위치한 데이터를 꺼냅니다. 이 함수를 호출하기 위해서는 데이터가 하나 이상 있음이 보장되어야 합니다.
34+
*
35+
* @return 마지막에 위치했던 데이터
36+
*/
37+
E pop();
38+
39+
/**
40+
* 스택의 마지막에 위치한 데이터를 확인합니다. 이 함수를 호출하기 위해서는 데이터가 하나 이상 있음이 보장되어야 합니다.
41+
*
42+
* @return 마지막에 위치한 데이터
43+
*/
44+
E peek();
45+
}

0 commit comments

Comments
 (0)