Skip to content

Commit

Permalink
[Deque] Fix error code
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 29, 2020
1 parent 2853655 commit e3a4435
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
23 changes: 14 additions & 9 deletions Deque/Deque.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef __DEUN_DEQUE_CPP__
#define __DEUN_DEQUE_CPP__

#include "Deque.h"

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

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

Expand Down Expand Up @@ -45,8 +48,8 @@ namespace Deun {
}

count++;
front = (size - 1 + front) % size; // 사실 안전한 방법은 아님
//front = (front - 1 + size) % size; // 이건 더 위험함 (front는 unsigned)
front = (size - 1 + front) % size; // 안전한 방법은 아님
//front = (front - 1 + size) % size; // 이건 더 위험함 (front는 unsigned임)
elements[front] = element;
return true;
}
Expand All @@ -65,7 +68,7 @@ namespace Deun {

int Deque::popFront() {
if (isEmpty()) {
throw DequeError::DEQUE_IS_EMPTY;
throw DequeError::ELEMENT_NOT_FOUND;
}

// 큐의 dequeue()와 동일
Expand All @@ -77,7 +80,7 @@ namespace Deun {

int Deque::popRear() {
if (isEmpty()) {
throw DequeError::DEQUE_IS_EMPTY;
throw DequeError::ELEMENT_NOT_FOUND;
}

count--;
Expand All @@ -87,28 +90,28 @@ namespace Deun {

int Deque::peekFront() {
if (isEmpty()) {
throw DequeError::DEQUE_IS_EMPTY;
throw DequeError::ELEMENT_NOT_FOUND;
}

return elements[front];
}

int Deque::peekRear() {
if (isEmpty()) {
throw DequeError::DEQUE_IS_EMPTY;
throw DequeError::ELEMENT_NOT_FOUND;
}

return elements[(size - 1 + rear) % size];
}

// for debug
void Deque::clear() {
count = front = rear = 0;

for (unsigned int i = 0; i < size; i++) {
elements[i] = 0;
}
}

// for debug
void Deque::print() {
using namespace std;

Expand All @@ -125,3 +128,5 @@ namespace Deun {
cout << endl;
}
}

#endif
27 changes: 13 additions & 14 deletions Deque/Deque.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef __DEUN_DEQUE__
#define __DEUN_DEQUE__
#ifndef __DEUN_DEQUE_H__
#define __DEUN_DEQUE_H__

#include <iostream>
#include <new>

namespace Deun {
enum class DequeError {
DEQUE_ALLOCATION_FAILED,
DEQUE_IS_EMPTY,
MEMORY_ALLOCATION_FAILED = 1000,
ELEMENT_NOT_FOUND,
};

// 배열 기반 원형 덱
Expand All @@ -20,7 +20,7 @@ namespace Deun {
unsigned int rear; // 원소를 삽입할 자리

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

bool isEmpty();
Expand All @@ -29,16 +29,15 @@ namespace Deun {
unsigned int getSize(); // 전체 크기 반환
unsigned int getCount(); // 채워진 원소의 개수 반환

bool pushFront(int element); // 맨 앞에 원소 삽입 (성공 = true, 실패 = false)
bool pushRear(int element); // 맨 뒤에 원소 삽입 (성공 = true, 실패 = false)
int popFront(); // 맨 앞 원소 반환 및 삭제 (실패 = throw)
int popRear(); // 맨 뒤 원소 반환 및 삭제 (실패 = throw)
int peekFront(); // 맨 앞 원소 반환 (실패 = throw)
int peekRear(); // 맨 뒤 원소 반환 (실패 = throw)
bool pushFront(int element); // 맨 앞에 원소 삽입 (성공 = true, 실패 = false)
bool pushRear(int element); // 맨 뒤에 원소 삽입 (성공 = true, 실패 = false)
int popFront(); // 맨 앞 원소 반환 및 삭제 (실패 = throw)
int popRear(); // 맨 뒤 원소 반환 및 삭제 (실패 = throw)
int peekFront(); // 맨 앞 원소 반환 (실패 = throw)
int peekRear(); // 맨 뒤 원소 반환 (실패 = throw)

// for debug
void clear();
void print();
void clear(); // O(n)
void print(); // O(n)
};
}

Expand Down

0 comments on commit e3a4435

Please sign in to comment.