-
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.
Merge pull request #1 from deunlee/Queue
Add Queue and Fix Stack
- Loading branch information
Showing
16 changed files
with
688 additions
and
202 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,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; | ||
} |
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,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]; | ||
} | ||
} |
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,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 |
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>{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> |
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="Queue.cpp" /> | ||
<ClCompile Include="Main.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="Queue.h" /> | ||
</ItemGroup> | ||
</Project> |
File renamed without changes.
Oops, something went wrong.