From 9670e444568d77f3790aaa2d1e0cca7afb839b9b Mon Sep 17 00:00:00 2001 From: tristanduun <119244064+tristanduun@users.noreply.github.com> Date: Sun, 10 Sep 2023 14:22:19 +0200 Subject: [PATCH] Ex4a --- MaxHeapVector.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++ MaxHeapVector.h | 18 ++++++++++++ main.cpp | 23 +++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 MaxHeapVector.cpp create mode 100644 MaxHeapVector.h create mode 100644 main.cpp diff --git a/MaxHeapVector.cpp b/MaxHeapVector.cpp new file mode 100644 index 0000000..d2b8cb8 --- /dev/null +++ b/MaxHeapVector.cpp @@ -0,0 +1,71 @@ +#include "MaxHeapVector.h" + +MaxHeapVector::MaxHeapVector() +{ + +} + +MaxHeapVector::~MaxHeapVector() +{ + +} + +bool MaxHeapVector::isEmpty() const +{ + return maxHeapVector.empty(); +} + +int MaxHeapVector::size() +{ + int size = 0; + for (std::vector::iterator it = maxHeapVector.begin(); it != maxHeapVector.end(); it++) + { + size++; + } + return size; +} + +void MaxHeapVector::insert(const int x) +{ + maxHeapVector.emplace_back(x); +} + +const int MaxHeapVector::findMax() const +{ + int maxVal = 0; + if (isEmpty()) + return -1; + else + { + for (auto it = maxHeapVector.begin(); it != maxHeapVector.end(); it++) + { + if (*it > maxVal) + maxVal = *it; + } + return maxVal; + } +} + +int MaxHeapVector::deleteMax() +{ + int maxVal = 0; + + std::vector::iterator maxValPos = maxHeapVector.begin(); + + if (isEmpty()) + return -1; + else + { + for (std::vector::iterator it = maxHeapVector.begin(); it != maxHeapVector.end(); it++) + { + if (*it > maxVal) + { + maxVal = *it; + maxValPos = it; + } + } + maxHeapVector.erase(maxValPos); + + return maxVal; + } +} diff --git a/MaxHeapVector.h b/MaxHeapVector.h new file mode 100644 index 0000000..f6d5e82 --- /dev/null +++ b/MaxHeapVector.h @@ -0,0 +1,18 @@ +#pragma once + +#include "MaxHeap.H" +#include + +class MaxHeapVector : public MaxHeap +{ +public: + MaxHeapVector(); + virtual ~MaxHeapVector(); + bool isEmpty() const override; + int size() override; + void insert(const int x) override; + const int findMax() const override; + int deleteMax() override; +private: + std::vector maxHeapVector; +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cfeb2ab --- /dev/null +++ b/main.cpp @@ -0,0 +1,23 @@ +#include "MaxHeapVector.h" +#include + +int main() +{ + MaxHeapVector bigV; + + bigV.insert(1); + bigV.insert(2); + bigV.insert(3); + bigV.insert(4); + bigV.insert(5); + bigV.insert(6); + bigV.insert(1); + + std::cout << "Max: " << bigV.findMax() << std::endl; + std::cout << "Is empty" << bigV.isEmpty() << std::endl; + std::cout << "Size: " << bigV.size() << std::endl; + bigV.deleteMax(); + std::cout << "Max: " << bigV.findMax() << std::endl; + std::cout << "Is empty" << bigV.isEmpty() << std::endl; + std::cout << "Size: " << bigV.size() << std::endl; +} \ No newline at end of file