From d635e52a58be824e1b6a1fe4698d793194665d04 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 26 Oct 2021 14:51:33 +0100 Subject: [PATCH] initial commit --- .gitignore | 4 + README.md | 3 + Stack and Queue/Stack and Queue.cpp | 217 ++++++++++++++++++++++++ Stack and Queue/Stack and Queue.sln | 31 ++++ Stack and Queue/Stack and Queue.vcxproj | 147 ++++++++++++++++ 5 files changed, 402 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 Stack and Queue/Stack and Queue.cpp create mode 100644 Stack and Queue/Stack and Queue.sln create mode 100644 Stack and Queue/Stack and Queue.vcxproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2bfc07a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vs/ +x64/ +*.filters +*.user diff --git a/README.md b/README.md new file mode 100644 index 0000000..419aeb2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Data Structures and Algorithms Lab Work + +This repository contains lab work from DSALG 2021 @ The University of Portsmouth diff --git a/Stack and Queue/Stack and Queue.cpp b/Stack and Queue/Stack and Queue.cpp new file mode 100644 index 0000000..499e4a3 --- /dev/null +++ b/Stack and Queue/Stack and Queue.cpp @@ -0,0 +1,217 @@ +#include +#include + +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 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; + +} + diff --git a/Stack and Queue/Stack and Queue.sln b/Stack and Queue/Stack and Queue.sln new file mode 100644 index 0000000..0a123db --- /dev/null +++ b/Stack and Queue/Stack and Queue.sln @@ -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 diff --git a/Stack and Queue/Stack and Queue.vcxproj b/Stack and Queue/Stack and Queue.vcxproj new file mode 100644 index 0000000..18e3fb7 --- /dev/null +++ b/Stack and Queue/Stack and Queue.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {04e0a3d3-803f-4a39-9106-7b0701a4f1dc} + StackandQueue + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file