-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from deunlee/Study
Apply template to Stack and add Graph interface
- Loading branch information
Showing
17 changed files
with
623 additions
and
162 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,5 +31,7 @@ | |
*.out | ||
*.app | ||
|
||
.vs | ||
# IDE | ||
x64 | ||
.vs | ||
.vscode |
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
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
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
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
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 |
---|---|---|
@@ -1,6 +1,40 @@ | ||
#ifndef __DEUN_GRAPH_H__ | ||
#define __DEUN_GRAPH_H__ | ||
|
||
#include <iostream> | ||
#include <new> | ||
|
||
namespace Deun { | ||
/** | ||
* 그래프 인터페이스 | ||
*/ | ||
class Graph { | ||
private: | ||
void __dfs(int vertex, bool* visited); | ||
|
||
protected: | ||
int vSize; // 정점 개수 (메모리 할당량) | ||
|
||
public: | ||
virtual int insertVertex() = 0; | ||
virtual bool insertEdge(int from, int to, bool undirected = false) = 0; | ||
|
||
virtual bool hasVertex(int v) = 0; | ||
virtual bool hasEdge(int from, int to) = 0; | ||
|
||
virtual void clear() = 0; | ||
virtual void print() = 0; | ||
|
||
bool dfs(int vertex); | ||
}; | ||
|
||
/** | ||
* 그래프 오류 | ||
*/ | ||
enum class GraphError { | ||
MEMORY_ALLOCATION_FAILED = 1000, | ||
TOO_MANY_VERTICES, | ||
}; | ||
} | ||
|
||
#endif |
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,80 @@ | ||
#ifndef __DEUN_ARRAY_STACK_H__ | ||
#define __DEUN_ARRAY_STACK_H__ | ||
|
||
#include "Stack.hpp" | ||
|
||
namespace Deun { | ||
/** | ||
* 스택 클래스 (배열 기반) | ||
*/ | ||
template <typename T> | ||
class ArrayStack : public Stack<T> { | ||
private: | ||
int size; // 스택의 최대 크기 (메모리 할당량) | ||
T* array; // 데이터 저장을 위한 배열 | ||
// top은 Stack 인터페이스의 count 변수를 사용함. | ||
// count라고 명명한 이유는 LinkedListStack에서도 사용하기 위해서임. | ||
|
||
public: | ||
/** | ||
* 스택 생성자 | ||
* @param size 스택의 최대 크기 | ||
* @throw StackError (크기 오류 또는 메모리 할당 실패) | ||
*/ | ||
ArrayStack(int size = 1000); | ||
|
||
/** | ||
* 스택 소멸자 | ||
*/ | ||
~ArrayStack(); | ||
|
||
/** | ||
* 스택의 최대 크기를 반환합니다. | ||
*/ | ||
int getSize(); | ||
|
||
/** | ||
* 현재 스택에 들어있는 원소의 개수를 반환합니다. | ||
*/ | ||
int getCount(); | ||
|
||
/** | ||
* 스택이 비어있는지의 여부를 반환합니다. | ||
*/ | ||
bool isEmpty(); | ||
|
||
/** | ||
* 스택이 가득 차있는지의 여부를 반환합니다. | ||
*/ | ||
bool isFull(); | ||
|
||
/** | ||
* 스택에 원소를 삽입하고 성공 여부를 반환합니다. | ||
* @param element 원소 | ||
*/ | ||
bool push(const T& element); | ||
|
||
/** | ||
* 스택의 마지막 원소를 삭제하고 성공 여부를 반환합니다. | ||
*/ | ||
bool pop(); | ||
|
||
/** | ||
* 스택의 마지막 원소를 반환합니다. | ||
* @throw StackError (스택이 빈 경우) | ||
*/ | ||
T& peek(); | ||
|
||
/** | ||
* 스택을 출력합니다. | ||
*/ | ||
void print(); | ||
|
||
/** | ||
* 스택을 초기화합니다. (모든 원소가 삭제됩니다.) | ||
*/ | ||
void clear(); | ||
}; | ||
} | ||
|
||
#endif |
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,103 @@ | ||
#ifndef __DEUN_ARRAY_STACK_HPP__ | ||
#define __DEUN_ARRAY_STACK_HPP__ | ||
|
||
#include "ArrayStack.h" | ||
|
||
namespace Deun { | ||
template <typename T> | ||
ArrayStack<T>::ArrayStack(int size) : Stack(0), size(size) { | ||
if (size <= 0) { | ||
throw StackError::SIZE_IS_TOO_SMALL; | ||
} | ||
array = static_cast<T*>(operator new(sizeof(T) * size, std::nothrow)); | ||
// new는 메모리 할당 후 생성자를 호출하지만, operator new는 메모리만 할당한다. | ||
// (C의 malloc()와 동일하게 생성자를 호출하지 않는다.) | ||
// new를 사용하는 경우 기본 생성자가 없는 클래스는 컴파일 오류가 발생한다. | ||
if (!array) { | ||
throw StackError::MEMORY_ALLOCATION_FAILED; | ||
} | ||
} | ||
|
||
template <typename T> | ||
ArrayStack<T>::~ArrayStack() { | ||
clear(); // 명시적 소멸자 호출 | ||
operator delete(array); // 모든 메모리를 반환한다. | ||
} | ||
|
||
template <typename T> | ||
int ArrayStack<T>::getSize() { | ||
return size; | ||
} | ||
|
||
template <typename T> | ||
int ArrayStack<T>::getCount() { | ||
return count; | ||
} | ||
|
||
template <typename T> | ||
bool ArrayStack<T>::isEmpty() { | ||
return (count == 0); // count는 top 용도로 쓰인다. | ||
} | ||
|
||
template <typename T> | ||
bool ArrayStack<T>::isFull() { | ||
return (count == size); | ||
} | ||
|
||
template <typename T> | ||
bool ArrayStack<T>::push(const T& element) { | ||
if (isFull()) { | ||
return false; | ||
} | ||
new(&array[count++]) T(element); // placement new | ||
// 스택 생성자에서 미리 할당해 놓은 메모리에 T의 복사 생성자로 생성한다. | ||
// placement new는 new와 다르게 메모리를 할당하지 않고 기존 메모리에 생성한다. | ||
// 깊은 복사가 필요한 클래스를 사용할 경우 직접 복사 생성자를 구현해야 한다. | ||
return true; | ||
} | ||
|
||
template <typename T> | ||
bool ArrayStack<T>::pop() { | ||
if (isEmpty()) { | ||
return false; | ||
} | ||
array[--count].~T(); // 명시적 소멸자 호출 | ||
return true; | ||
} | ||
|
||
template <typename T> | ||
T& ArrayStack<T>::peek() { | ||
if (isEmpty()) { | ||
throw StackError::ELEMENT_NOT_FOUND; | ||
} | ||
return array[count - 1]; | ||
} | ||
|
||
template <typename T> | ||
void ArrayStack<T>::print() { | ||
using namespace std; | ||
cout << "ArrayStack(size=" << size << ", count=" << count << ")" << endl; | ||
if (!count) { | ||
cout << "(empty)" << endl; | ||
return; | ||
} | ||
cout << "(top) "; | ||
for (int i = count - 1; i >= 0; i--) { | ||
cout << array[i]; | ||
if (i) { | ||
cout << " / "; | ||
} | ||
} | ||
cout << endl; | ||
} | ||
|
||
template <typename T> | ||
void ArrayStack<T>::clear() { | ||
for (int i = count - 1; i >= 0; i--) { // top 부터 삭제 | ||
array[i].~T(); // 명시적 소멸자 호출 | ||
} | ||
count = 0; | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.