Skip to content

Commit

Permalink
Merge pull request #1 from deunlee/Queue
Browse files Browse the repository at this point in the history
Add Queue and Fix Stack
  • Loading branch information
deunlee committed May 24, 2020
2 parents fefe460 + 4c12ac1 commit 3e22ecc
Show file tree
Hide file tree
Showing 16 changed files with 688 additions and 202 deletions.
28 changes: 19 additions & 9 deletions Data-Structure.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ VisualStudioVersion = 15.0.28010.2046
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Stack", "Stack\Stack.vcxproj", "{7630A810-5B5B-4B05-985E-E411F8E05429}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StackTest", "StackTest\StackTest.vcxproj", "{28CD51F6-9762-4773-9314-F136E0755B1C}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Queue", "Queue\Queue.vcxproj", "{C7617599-24A5-4791-A634-5E6E16F4AD9E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTest", "UnitTest\UnitTest.vcxproj", "{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,14 +25,22 @@ Global
{7630A810-5B5B-4B05-985E-E411F8E05429}.Release|x64.Build.0 = Release|x64
{7630A810-5B5B-4B05-985E-E411F8E05429}.Release|x86.ActiveCfg = Release|Win32
{7630A810-5B5B-4B05-985E-E411F8E05429}.Release|x86.Build.0 = Release|Win32
{28CD51F6-9762-4773-9314-F136E0755B1C}.Debug|x64.ActiveCfg = Debug|x64
{28CD51F6-9762-4773-9314-F136E0755B1C}.Debug|x64.Build.0 = Debug|x64
{28CD51F6-9762-4773-9314-F136E0755B1C}.Debug|x86.ActiveCfg = Debug|Win32
{28CD51F6-9762-4773-9314-F136E0755B1C}.Debug|x86.Build.0 = Debug|Win32
{28CD51F6-9762-4773-9314-F136E0755B1C}.Release|x64.ActiveCfg = Release|x64
{28CD51F6-9762-4773-9314-F136E0755B1C}.Release|x64.Build.0 = Release|x64
{28CD51F6-9762-4773-9314-F136E0755B1C}.Release|x86.ActiveCfg = Release|Win32
{28CD51F6-9762-4773-9314-F136E0755B1C}.Release|x86.Build.0 = Release|Win32
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Debug|x64.ActiveCfg = Debug|x64
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Debug|x64.Build.0 = Debug|x64
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Debug|x86.ActiveCfg = Debug|Win32
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Debug|x86.Build.0 = Debug|Win32
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Release|x64.ActiveCfg = Release|x64
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Release|x64.Build.0 = Release|x64
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Release|x86.ActiveCfg = Release|Win32
{C7617599-24A5-4791-A634-5E6E16F4AD9E}.Release|x86.Build.0 = Release|Win32
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Debug|x64.ActiveCfg = Debug|x64
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Debug|x64.Build.0 = Debug|x64
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Debug|x86.ActiveCfg = Debug|Win32
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Debug|x86.Build.0 = Debug|Win32
{A5830C9D-CF4D-471F-9F1E-F33F2EEF7378}.Release|x64.ActiveCfg = Release|x64
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
49 changes: 49 additions & 0 deletions Queue/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
using namespace std;

#include "Queue.h"
using namespace Deun;

int main() {
Queue q(3);

cout << boolalpha;

cout << "isEmpty(): " << q.isEmpty() << endl;
cout << endl;
cout << "enqueue(1): " << q.enqueue(1) << endl;
cout << "enqueue(2): " << q.enqueue(2) << endl;
cout << "enqueue(3): " << q.enqueue(3) << endl;
cout << "isFull(): " << q.isFull() << endl;
cout << "enqueue(4): " << q.enqueue(4) << endl;
cout << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "dequeue(): " << q.dequeue() << endl;

try {
cout << "dequeue(): " << q.dequeue() << endl;
}
catch (QueueError err) {
cout << "dequeue(): ErrorCode " << (int)err << endl;
}

cout << "isEmpty(): " << q.isEmpty() << endl;
cout << endl;

cout << "enqueue(1): " << q.enqueue(1) << endl;
cout << "enqueue(2): " << q.enqueue(2) << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "enqueue(3): " << q.enqueue(3) << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "enqueue(4): " << q.enqueue(4) << endl;
cout << "enqueue(5): " << q.enqueue(5) << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "enqueue(6): " << q.enqueue(6) << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "dequeue(): " << q.dequeue() << endl;
cout << "isEmpty(): " << q.isEmpty() << endl;

return 0;
}
67 changes: 67 additions & 0 deletions Queue/Queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "Queue.h"

namespace Deun {
Queue::Queue(unsigned int size) {
this->size = size;
count = front = rear = 0;
elements = new (std::nothrow) int[size];

if (!elements) {
this->size = 0;
throw QueueError::QUEUE_ALLOCATION_FAILED;
}
}

Queue::~Queue() {
delete[] elements;
}

bool Queue::isEmpty() {
return (count == 0);
}

bool Queue::isFull() {
return (count == size);
}

unsigned int Queue::getSize() {
return size;
}

unsigned int Queue::getCount() {
return count;
}

// front : 원소를 삭제할 자리 (채워져 있는 상태, 단, count가 0이면 비워져 있음)
// rear : 원소를 삽입할 자리 (비어있는 상태)

bool Queue::enqueue(int element) {
if (isFull()) {
return false;
}

count++;
elements[rear] = element;
rear = (rear + 1) % size;
return true;
}

int Queue::dequeue() {
if (isEmpty()) {
throw QueueError::QUEUE_IS_EMPTY;
}

count--;
int result = elements[front];
front = (front + 1) % size;
return result;
}

int Queue::peek() {
if (isEmpty()) {
throw QueueError::QUEUE_IS_EMPTY;
}

return elements[front];
}
}
40 changes: 40 additions & 0 deletions Queue/Queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef __DEUN_QUEUE__
#define __DEUN_QUEUE__

#include <new>

namespace Deun {
enum class QueueError {
QUEUE_ALLOCATION_FAILED,
QUEUE_IS_EMPTY,
};

// 배열 기반 원형 큐
class Queue {
private:
int* elements;
unsigned int size; // 큐의 크기 (배열 원소의 개수)
unsigned int count; // 채워진 데이터의 개수
// (count 변수를 사용하지 않아도 큐의 공백/포화 상태를
// 검출할 수 있지만, 배열의 원소 중 하나를 비워두어야 한다.
// 이것이 별로 마음에 들지 않아 count 변수를 사용하기로 하였다.)
unsigned int front; // 원소를 삭제할 자리
unsigned int rear; // 원소를 삽입할 자리

public:
Queue(unsigned int size = 100);
~Queue();

bool isEmpty();
bool isFull();

unsigned int getSize();
unsigned int getCount();

bool enqueue(int element); // 맨 뒤에 삽입 (성공시 true, 실패시 false)
int dequeue(); // 맨 앞 원소 반환 및 삭제 (실패시 throw)
int peek(); // 맨 앞 원소 반환 (실패시 throw)
};
}

#endif
159 changes: 159 additions & 0 deletions Queue/Queue.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>{C7617599-24A5-4791-A634-5E6E16F4AD9E}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Queue</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="Main.cpp" />
<ClCompile Include="Queue.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Queue.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
10 changes: 10 additions & 0 deletions Queue/Queue.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="Queue.cpp" />
<ClCompile Include="Main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Queue.h" />
</ItemGroup>
</Project>
File renamed without changes.
Loading

0 comments on commit 3e22ecc

Please sign in to comment.