Skip to content

Commit

Permalink
[Deque] Add array-based Deque
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 25, 2020
1 parent bca2cb2 commit 700dd49
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Data-Structure.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Queue", "Queue\Queue.vcxpro
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTest", "UnitTest\UnitTest.vcxproj", "{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Deque", "Deque\Deque.vcxproj", "{88D1A79E-BB93-46A3-A9A9-18BED3527596}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -41,6 +43,14 @@ Global
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Release|x64.Build.0 = Release|x64
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Release|x86.ActiveCfg = Release|Win32
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Release|x86.Build.0 = Release|Win32
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Debug|x64.ActiveCfg = Debug|x64
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Debug|x64.Build.0 = Debug|x64
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Debug|x86.ActiveCfg = Debug|Win32
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Debug|x86.Build.0 = Debug|Win32
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Release|x64.ActiveCfg = Release|x64
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Release|x64.Build.0 = Release|x64
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Release|x86.ActiveCfg = Release|Win32
{88D1A79E-BB93-46A3-A9A9-18BED3527596}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
127 changes: 127 additions & 0 deletions Deque/Deque.cpp
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;
}
}
45 changes: 45 additions & 0 deletions Deque/Deque.h
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
159 changes: 159 additions & 0 deletions Deque/Deque.vcxproj
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>
10 changes: 10 additions & 0 deletions Deque/Deque.vcxproj.filters
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>
4 changes: 4 additions & 0 deletions Deque/Deque.vcxproj.user
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>
Loading

0 comments on commit 700dd49

Please sign in to comment.