initial commit
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
.vs/
|
||||||
|
x64/
|
||||||
|
*.filters
|
||||||
|
*.user
|
||||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Data Structures and Algorithms Lab Work
|
||||||
|
|
||||||
|
This repository contains lab work from DSALG 2021 @ The University of Portsmouth
|
||||||
217
Stack and Queue/Stack and Queue.cpp
Normal file
217
Stack and Queue/Stack and Queue.cpp
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Stack
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Stack(int size)
|
||||||
|
{
|
||||||
|
mStorage = (int*)malloc(size * sizeof(int));
|
||||||
|
if (mStorage != nullptr)
|
||||||
|
memset(mStorage, NULL, size);
|
||||||
|
mSize = size;
|
||||||
|
mTop = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Stack()
|
||||||
|
{
|
||||||
|
delete mStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Push(int elem)
|
||||||
|
{
|
||||||
|
if (mTop > mSize)
|
||||||
|
return;
|
||||||
|
mStorage[mTop] = elem;
|
||||||
|
mTop++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Pop()
|
||||||
|
{
|
||||||
|
mTop--;
|
||||||
|
int elem = mStorage[mTop];
|
||||||
|
mStorage[mTop] = NULL;
|
||||||
|
return elem;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Peek()
|
||||||
|
{
|
||||||
|
return mStorage[mTop - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Length()
|
||||||
|
{
|
||||||
|
return mTop;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int* mStorage;
|
||||||
|
int mSize;
|
||||||
|
int mTop;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Queue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Queue(int size)
|
||||||
|
{
|
||||||
|
mStorage = (int*)malloc(size * sizeof(int));
|
||||||
|
if (mStorage != nullptr)
|
||||||
|
memset(mStorage, NULL, size);
|
||||||
|
mSize = size;
|
||||||
|
mHead = 0;
|
||||||
|
mLength = 0;
|
||||||
|
mTail = size - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Queue()
|
||||||
|
{
|
||||||
|
delete mStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// adds to tail
|
||||||
|
void Enqueue(int elem)
|
||||||
|
{
|
||||||
|
mTail = (mTail + 1) % mSize;
|
||||||
|
mStorage[mTail] = elem;
|
||||||
|
mLength++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// deletes head
|
||||||
|
int Dequeue()
|
||||||
|
{
|
||||||
|
int elem = mStorage[mHead];
|
||||||
|
mHead = (mHead + 1) % mSize;
|
||||||
|
mLength--;
|
||||||
|
return elem;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Peek()
|
||||||
|
{
|
||||||
|
return mStorage[mHead];
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Length()
|
||||||
|
{
|
||||||
|
return mLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int* mStorage;
|
||||||
|
int mSize;
|
||||||
|
int mLength;
|
||||||
|
int mHead, mTail;
|
||||||
|
};
|
||||||
|
|
||||||
|
class StackWithQueue : public Queue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
StackWithQueue(int size)
|
||||||
|
: Queue(size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Push(int elem)
|
||||||
|
{
|
||||||
|
Enqueue(elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Pop()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < Length() - 1; i++)
|
||||||
|
{
|
||||||
|
Enqueue(Dequeue());
|
||||||
|
}
|
||||||
|
return Dequeue();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class QueueWithStack : public Stack
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QueueWithStack(int size)
|
||||||
|
: Stack(size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enqueue(int elem)
|
||||||
|
{
|
||||||
|
Push(elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Dequeue()
|
||||||
|
{
|
||||||
|
std::vector<int> temp;
|
||||||
|
std::cout << "length " << Length();
|
||||||
|
for (int i = 0; i < Length() - 1; i++)
|
||||||
|
{
|
||||||
|
temp.push_back(Pop());
|
||||||
|
}
|
||||||
|
int elem = Pop();
|
||||||
|
for (int i = temp.size(); i > 0; i--)
|
||||||
|
{
|
||||||
|
Push(temp[i]);
|
||||||
|
}
|
||||||
|
return elem;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Stack stack{ 50 };
|
||||||
|
stack.Push(10);
|
||||||
|
stack.Push(9);
|
||||||
|
stack.Push(8);
|
||||||
|
stack.Push(7);
|
||||||
|
stack.Push(6);
|
||||||
|
|
||||||
|
std::cout << stack.Pop() << std::endl;
|
||||||
|
std::cout << stack.Pop() << std::endl;
|
||||||
|
std::cout << stack.Pop() << std::endl;
|
||||||
|
std::cout << stack.Pop() << std::endl;
|
||||||
|
|
||||||
|
// should be 1
|
||||||
|
std::cout << stack.Length() << std::endl << std::endl;
|
||||||
|
|
||||||
|
Queue queue{ 50 };
|
||||||
|
queue.Enqueue(6);
|
||||||
|
queue.Enqueue(5);
|
||||||
|
queue.Enqueue(4);
|
||||||
|
queue.Enqueue(3);
|
||||||
|
queue.Enqueue(2);
|
||||||
|
queue.Enqueue(1);
|
||||||
|
|
||||||
|
std::cout << queue.Dequeue() << std::endl;
|
||||||
|
std::cout << queue.Dequeue() << std::endl;
|
||||||
|
std::cout << queue.Dequeue() << std::endl;
|
||||||
|
std::cout << queue.Dequeue() << std::endl;
|
||||||
|
std::cout << queue.Length() << std::endl << std::endl;
|
||||||
|
|
||||||
|
StackWithQueue stackWithQueue{ 50 };
|
||||||
|
stackWithQueue.Push(9);
|
||||||
|
stackWithQueue.Push(8);
|
||||||
|
stackWithQueue.Push(7);
|
||||||
|
stackWithQueue.Push(6);
|
||||||
|
stackWithQueue.Push(5);
|
||||||
|
|
||||||
|
std::cout << stackWithQueue.Pop() << std::endl;
|
||||||
|
std::cout << stackWithQueue.Pop() << std::endl;
|
||||||
|
std::cout << stackWithQueue.Pop() << std::endl;
|
||||||
|
std::cout << stackWithQueue.Pop() << std::endl << std::endl;
|
||||||
|
|
||||||
|
QueueWithStack queueWithStack{ 50 };
|
||||||
|
// queueWithStack.Enqueue(9);
|
||||||
|
queueWithStack.Enqueue(8);
|
||||||
|
queueWithStack.Enqueue(7);
|
||||||
|
queueWithStack.Enqueue(6);
|
||||||
|
queueWithStack.Enqueue(5);
|
||||||
|
queueWithStack.Enqueue(4);
|
||||||
|
|
||||||
|
std::cout << queueWithStack.Dequeue() << std::endl;
|
||||||
|
std::cout << queueWithStack.Dequeue() << std::endl;
|
||||||
|
std::cout << queueWithStack.Dequeue() << std::endl;
|
||||||
|
std::cout << queueWithStack.Dequeue() << std::endl;
|
||||||
|
std::cout << queueWithStack.Dequeue() << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
31
Stack and Queue/Stack and Queue.sln
Normal file
31
Stack and Queue/Stack and Queue.sln
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31808.319
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Stack and Queue", "Stack and Queue.vcxproj", "{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Release|x64.Build.0 = Release|x64
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{04E0A3D3-803F-4A39-9106-7B0701A4F1DC}.Release|x86.Build.0 = Release|Win32
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {EA932C7A-E35D-4CE7-90DB-0C592DFB5B28}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
147
Stack and Queue/Stack and Queue.vcxproj
Normal file
147
Stack and Queue/Stack and Queue.vcxproj
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" xmlns="http://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>16.0</VCProjectVersion>
|
||||||
|
<Keyword>Win32Proj</Keyword>
|
||||||
|
<ProjectGuid>{04e0a3d3-803f-4a39-9106-7b0701a4f1dc}</ProjectGuid>
|
||||||
|
<RootNamespace>StackandQueue</RootNamespace>
|
||||||
|
<WindowsTargetPlatformVersion>10.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>v143</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</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)'=='Debug|Win32'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Stack and Queue.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user