-
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.
- Loading branch information
Showing
7 changed files
with
410 additions
and
0 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
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,127 @@ | ||
#include "Deque.h" | ||
|
||
namespace Deun { | ||
Deque::Deque(unsigned int size) { | ||
this->size = size; | ||
count = front = rear = 0; | ||
elements = new (std::nothrow) int[size]; | ||
|
||
if (!elements) { | ||
this->size = 0; | ||
throw DequeError::DEQUE_ALLOCATION_FAILED; | ||
} | ||
} | ||
|
||
Deque::~Deque() { | ||
delete[] elements; | ||
} | ||
|
||
bool Deque::isEmpty() { | ||
return (count == 0); | ||
} | ||
|
||
bool Deque::isFull() { | ||
return (count == size); | ||
} | ||
|
||
unsigned int Deque::getSize() { | ||
return size; | ||
} | ||
|
||
unsigned int Deque::getCount() { | ||
return count; | ||
} | ||
|
||
inline unsigned int safeMove(unsigned int basis, int diff) { | ||
return basis; | ||
} | ||
|
||
// front : 채워진 상태 (단, count가 0이면 비워져 있음) | ||
// rear : 비어있는 상태 (단, count가 size면 채워져 있음) | ||
|
||
bool Deque::pushFront(int element) { | ||
if (isFull()) { | ||
return false; | ||
} | ||
|
||
count++; | ||
front = (size - 1 + front) % size; // 사실 안전한 방법은 아님 | ||
//front = (front - 1 + size) % size; // 이건 더 위험함 (front는 unsigned) | ||
elements[front] = element; | ||
return true; | ||
} | ||
|
||
bool Deque::pushRear(int element) { | ||
if (isFull()) { | ||
return false; | ||
} | ||
|
||
// 큐의 enqueue()와 동일 | ||
count++; | ||
elements[rear] = element; | ||
rear = (rear + 1) % size; | ||
return true; | ||
} | ||
|
||
int Deque::popFront() { | ||
if (isEmpty()) { | ||
throw DequeError::DEQUE_IS_EMPTY; | ||
} | ||
|
||
// 큐의 dequeue()와 동일 | ||
count--; | ||
int result = elements[front]; | ||
front = (front + 1) % size; | ||
return result; | ||
} | ||
|
||
int Deque::popRear() { | ||
if (isEmpty()) { | ||
throw DequeError::DEQUE_IS_EMPTY; | ||
} | ||
|
||
count--; | ||
rear = (size - 1 + rear) % size; | ||
return elements[rear]; | ||
} | ||
|
||
int Deque::peekFront() { | ||
if (isEmpty()) { | ||
throw DequeError::DEQUE_IS_EMPTY; | ||
} | ||
|
||
return elements[front]; | ||
} | ||
|
||
int Deque::peekRear() { | ||
if (isEmpty()) { | ||
throw DequeError::DEQUE_IS_EMPTY; | ||
} | ||
|
||
return elements[(size - 1 + rear) % size]; | ||
} | ||
|
||
// for debug | ||
void Deque::clear() { | ||
for (unsigned int i = 0; i < size; i++) { | ||
elements[i] = 0; | ||
} | ||
} | ||
|
||
// for debug | ||
void Deque::print() { | ||
using namespace std; | ||
|
||
cout << "Deque(size=" << size << ", count=" << count; | ||
cout << ", front=" << front << ", rear=" << rear << ")" << endl; | ||
|
||
for (unsigned int i = 0; i < size; i++) { | ||
cout << elements[i]; | ||
if (i == front) cout << "F"; | ||
if (i == rear) cout << "R"; | ||
cout << "\t"; | ||
} | ||
|
||
cout << endl; | ||
} | ||
} |
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,45 @@ | ||
#ifndef __DEUN_DEQUE__ | ||
#define __DEUN_DEQUE__ | ||
|
||
#include <iostream> | ||
#include <new> | ||
|
||
namespace Deun { | ||
enum class DequeError { | ||
DEQUE_ALLOCATION_FAILED, | ||
DEQUE_IS_EMPTY, | ||
}; | ||
|
||
// 배열 기반 원형 덱 | ||
class Deque { | ||
private: | ||
int* elements; | ||
unsigned int size; // 덱의 크기 (배열 원소의 개수) | ||
unsigned int count; // 채워진 원소의 개수 | ||
unsigned int front; // 원소를 삭제할 자리 | ||
unsigned int rear; // 원소를 삽입할 자리 | ||
|
||
public: | ||
Deque(unsigned int size = 100); | ||
~Deque(); | ||
|
||
bool isEmpty(); | ||
bool isFull(); | ||
|
||
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) | ||
|
||
// for debug | ||
void clear(); | ||
void print(); | ||
}; | ||
} | ||
|
||
#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,159 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<VCProjectVersion>15.0</VCProjectVersion> | ||
<ProjectGuid>{88D1A79E-BB93-46A3-A9A9-18BED3527596}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>Deque</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0.10240.0</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="Deque.cpp" /> | ||
<ClCompile Include="Main.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="Deque.h" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
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,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<ClCompile Include="Deque.cpp" /> | ||
<ClCompile Include="Main.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="Deque.h" /> | ||
</ItemGroup> | ||
</Project> |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup /> | ||
</Project> |
Oops, something went wrong.