Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

윤성우의 자료구조 스택 부분 구현 #24

Merged
merged 12 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions yoonexample/src/main/java/stack/ArrayStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package stack;

import java.util.EmptyStackException;

public class ArrayStack<E> implements Stack<E> {

private static final int INITIAL_CAPACITY = 100;

private int topIndex;
private Object[] arr;

public ArrayStack() {
this.topIndex = -1;
this.arr = new Object[INITIAL_CAPACITY];
}

@Override
public int size() {
return this.topIndex + 1;
}

@Override
public boolean isEmpty() {
return this.topIndex == -1;
}

@Override
public void push(E data) {
this.topIndex++;
arr[topIndex] = data;
}

@Override
public E pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
this.topIndex--;
return (E) arr[topIndex + 1];
}

@Override
public E peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return (E) arr[topIndex];
}
}
45 changes: 45 additions & 0 deletions yoonexample/src/main/java/stack/CircularListStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package stack;

import java.util.EmptyStackException;
import list.CircularLinkedList;
import list.List;

public class CircularListStack<E> implements Stack<E> {

private final List<E> list = new CircularLinkedList<>();
private int lastIndex = -1;

@Override
public int size() {
return this.list.size();
}

@Override
public boolean isEmpty() {
return this.list.isEmpty();
}

@Override
public void push(E data) {
this.lastIndex++;
this.list.insert(data);
}

@Override
public E pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
E tmpData = this.list.remove(lastIndex);
this.lastIndex--;
return tmpData;
}

@Override
public E peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return this.list.get(lastIndex);
}
}
56 changes: 56 additions & 0 deletions yoonexample/src/main/java/stack/ListStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package stack;

import java.util.EmptyStackException;

public class ListStack<E> implements Stack<E> {

private int size;
private Node<E> head;

@Override
public int size() {
return this.size;
}

@Override
public boolean isEmpty() {
return this.size == 0;
}

@Override
public void push(E data) {
Node<E> newNode = new Node<>(data);
newNode.next = this.head;
this.head = newNode;
this.size++;
}

@Override
public E pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
Node<E> tmpNode = this.head;
this.head = this.head.next;
size--;
return tmpNode.data;
}

@Override
public E peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return this.head.data;
}

private static class Node<T> {

private T data;
private Node<T> next;

public Node(T data) {
this.data = data;
}
}
}
45 changes: 45 additions & 0 deletions yoonexample/src/main/java/stack/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package stack;

/**
* 스택 자료구조의 ADT, 인터페이스
*
* @param <E> 데이터의 파라미터 타입
* @author dion
*/
public interface Stack<E> {

/**
* 스택에 저장된 데이터의 개수를 반환합니다.
*
* @return 데이터의 개수
*/
int size();

/**
* 스택이 비어있는지 여부를 반환합니다.
*
* @return 스택이 비어있으면 true, 그렇지 않으면 false
*/
boolean isEmpty();

/**
* 스택에 파라미터로 전달된 데이터를 마지막에 저장합니다.
*
* @param data 저장할 데이터
*/
void push(E data);

/**
* 스택의 마지막에 위치한 데이터를 꺼냅니다. 이 함수를 호출하기 위해서는 데이터가 하나 이상 있음이 보장되어야 합니다.
*
* @return 마지막에 위치했던 데이터
*/
E pop();

/**
* 스택의 마지막에 위치한 데이터를 확인합니다. 이 함수를 호출하기 위해서는 데이터가 하나 이상 있음이 보장되어야 합니다.
*
* @return 마지막에 위치한 데이터
*/
E peek();
}
Loading