Skip to content

Commit

Permalink
[Stack] Fix error code
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 29, 2020
1 parent 30ded86 commit de97791
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 8 additions & 3 deletions Stack/Stack.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef __DEUN_STACK_CPP__
#define __DEUN_STACK_CPP__

#include "Stack.h"

namespace Deun {
Expand All @@ -8,7 +11,7 @@ namespace Deun {

if (!elements) {
this->size = 0;
throw StackError::STACK_ALLOCATION_FAILED;
throw StackError::MEMORY_ALLOCATION_FAILED;
}
}

Expand Down Expand Up @@ -43,17 +46,19 @@ namespace Deun {

int Stack::pop() {
if (isEmpty()) {
throw StackError::STACK_IS_EMPTY;
throw StackError::ELEMENT_NOT_FOUND;
}

return elements[--top];
}

int Stack::peek() {
if (isEmpty()) {
throw StackError::STACK_IS_EMPTY;
throw StackError::ELEMENT_NOT_FOUND;
}

return elements[top - 1];
}
}

#endif
10 changes: 5 additions & 5 deletions Stack/Stack.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef __DEUN_STACK__
#define __DEUN_STACK__
#ifndef __DEUN_STACK_H__
#define __DEUN_STACK_H__

#include <new>

namespace Deun {
enum class StackError {
STACK_ALLOCATION_FAILED,
STACK_IS_EMPTY,
MEMORY_ALLOCATION_FAILED = 1000,
ELEMENT_NOT_FOUND,
};

// 배열 기반 스택
Expand All @@ -17,7 +17,7 @@ namespace Deun {
unsigned int top; // 원소를 삽입/삭제할 자리

public:
Stack(unsigned int size = 100);
Stack(unsigned int size = 1000);
~Stack();

bool isEmpty();
Expand Down

0 comments on commit de97791

Please sign in to comment.