From b7719f2de9200cc0d4f0b11208e910fd4eef6e68 Mon Sep 17 00:00:00 2001 From: Christopher Sanden Date: Wed, 5 Nov 2025 23:32:00 +0100 Subject: [PATCH] halfway part 2 --- .gitignore | 2 +- .../Portfolio/Assignment-02/option1.cpp | 66 ++++++- .../Portfolio/SharedLib/CMakeLists.txt | 6 +- .../Portfolio/SharedLib/FileReaderUtils.cpp | 2 - .../Portfolio/SharedLib/TLinkedList.cpp | 187 ++++++++++++++++++ .../Portfolio/SharedLib/TLinkedList.h | 56 ++++++ .../Portfolio/SharedLib/TPerson.cpp | 14 ++ Exam/IKT203Exam/Portfolio/SharedLib/TPerson.h | 32 +++ Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp | 15 ++ Exam/IKT203Exam/Portfolio/SharedLib/Utils.h | 1 + 10 files changed, 371 insertions(+), 10 deletions(-) create mode 100644 Exam/IKT203Exam/Portfolio/SharedLib/TLinkedList.cpp create mode 100644 Exam/IKT203Exam/Portfolio/SharedLib/TLinkedList.h create mode 100644 Exam/IKT203Exam/Portfolio/SharedLib/TPerson.cpp create mode 100644 Exam/IKT203Exam/Portfolio/SharedLib/TPerson.h diff --git a/.gitignore b/.gitignore index 1b2f460..5aa699f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ # Ignore -*.zip +*.zip \ No newline at end of file diff --git a/Exam/IKT203Exam/Portfolio/Assignment-02/option1.cpp b/Exam/IKT203Exam/Portfolio/Assignment-02/option1.cpp index aa8a3fa..a558b61 100644 --- a/Exam/IKT203Exam/Portfolio/Assignment-02/option1.cpp +++ b/Exam/IKT203Exam/Portfolio/Assignment-02/option1.cpp @@ -5,10 +5,13 @@ #include #include "option1.h" #include "SharedLib.h" +#include "TLinkedList.h" +#include "TPerson.h" + +TLinkedList g, e; /** * @brief Callback function to process one name. - */ static bool NameReadCallback(const int aIndex, const int aTotalCount, const std::string& aFirstName, const std::string& aLastName) { std::cout << "Reading Name " << (aIndex + 1) << " of " << aTotalCount << ": " @@ -18,19 +21,70 @@ static bool NameReadCallback(const int aIndex, const int aTotalCount, const std: // Return false when aIndex is 9 to stop the loop after this one. return (aIndex < 9); } +*/ + +// *Inspired* by the provided NameReadCallback given above +static bool onNameRead(const int aIndex, int aTotalCount, const std::string& aFirstName, const std::string& aLastName) +{ + const ENumStatus status = (aIndex < 1500) ? EMPLOYEE : GUEST; + + const TPerson p(aFirstName, aLastName, status); + + + if (status == EMPLOYEE) + e.Append(p); + else + g.Append(p); + + std::cout << "[" < + +TLinkedList::~TLinkedList() +{ + const Node* cur = head; + while (cur) { + const Node* next = cur->next; + delete cur; + cur = next; + } +} + +void TLinkedList::Append(const TPerson& person) +{ + Node* newNode = new Node(person); + if (size == 0) { + head = tail = newNode; + } else { + tail->next = newNode; + tail = newNode; + } + size++; +} + +void TLinkedList::Prepend(const TPerson& person) +{ + Node* newNode = new Node(person); + if (size == 0) { + head = tail = newNode; + } else { + newNode->next = head; + head = newNode; + } + size++; +} + +void TLinkedList::InsertAtIndex(const int index, const TPerson& person) +{ + if (index < 0 || index > size) { + std::cout << "Index out of range\n"; + return; + } + + if (index == 0) { + Prepend(person); + return; + } + if (index == size) { + Append(person); + return; + } + + Node* prev = head; + for (int i = 0; i < index - 1; ++i) + prev = prev->next; + + Node* newNode = new Node(person); + newNode->next = prev->next; + prev->next = newNode; + size++; +} + +void TLinkedList::Remove(const int index) +{ + if (index < 0 || index >= size) + return; + + if (index == 0) { + const Node* oldHead = head; + head = head->next; + if (size == 1) + tail = nullptr; + delete oldHead; + size--; + return; + } + + Node* prev = head; + for (int i = 0; i < index - 1; ++i) + prev = prev->next; + + const Node* toDelete = prev->next; + prev->next = toDelete->next; + if (toDelete == tail) + tail = prev; + delete toDelete; + size--; +} + +TPerson TLinkedList::GetAtIndex(const int index) const +{ + if (index < 0 || index >= size) { + std::cout << "Index out of range\n"; + // return dummy + return {"N/A", "N/A", GUEST}; + } + + Node* cur = head; + for (int i = 0; i < index; ++i) + cur = cur->next; + + return cur->person; +} + +void TLinkedList::MergeSortSplit(Node *source, Node **front, Node **back) +{ + if (source == nullptr || source->next == nullptr) { + *front = source; + *back = nullptr; + return; + } + + Node* slow = source; + const Node* fast = source->next; + + while (fast != nullptr) { + fast = fast->next; + if (fast != nullptr) { + slow = slow->next; + fast = fast->next; + } + } + + *front = source; + *back = slow->next; + slow->next = nullptr; +} + +TLinkedList::Node *TLinkedList::MergeList(Node *a, Node *b) +{ + if (a == nullptr) + return b; + if (b == nullptr) + return a; + + Node* result = nullptr; + + if (a->person < b->person) { + result = a; + result->next = MergeList(a->next, b); + } + else { + result = b; + result->next = MergeList(a, b->next); + } + return result; +} + +TLinkedList::Node *TLinkedList::MergeSort(Node *head) +{ + if (head == nullptr || head->next == nullptr) + return head; + + Node* front; + Node* back; + + MergeSortSplit(head, &front, &back); + + front = MergeSort(front); + back = MergeSort(back); + return MergeList(front, back); + + +} + +void TLinkedList::Sort() +{ + this->head = MergeSort(head); + + Node* cur = this->head; + tail = nullptr; + while (cur) { + if (cur->next == nullptr) + tail = cur; + cur = cur->next; + } +} + + + + + + + + + diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/TLinkedList.h b/Exam/IKT203Exam/Portfolio/SharedLib/TLinkedList.h new file mode 100644 index 0000000..a8956b4 --- /dev/null +++ b/Exam/IKT203Exam/Portfolio/SharedLib/TLinkedList.h @@ -0,0 +1,56 @@ +#ifndef IKT203_COURSE_ASSIGNMENTS_TLINKEDLIST_H +#define IKT203_COURSE_ASSIGNMENTS_TLINKEDLIST_H + +#include "TPerson.h" + +class TLinkedList { + + + private: + struct Node { + TPerson person; + Node* next; + explicit Node(const TPerson& p) : person(p), next(nullptr) {} + + void setNext(Node* n) + { + this->next = n; + } + + [[nodiscard]] static Node* GetNext(const Node* n) + { + return n->next; + } + + [[nodiscard]] static TPerson GetPerson(Node* n) + { + return n->person; + } + }; + Node* head; + Node* tail; + int size; +public: + TLinkedList() : head(nullptr), tail(nullptr), size(0) {} + ~TLinkedList(); + + void Append(const TPerson& person); + void Prepend(const TPerson& person); + void InsertAtIndex(int index, const TPerson& person); + void Remove(int index); + [[nodiscard]] TPerson GetAtIndex(int index) const; + [[nodiscard]] int GetSize() const { return size; } + static void MergeSortSplit(Node* source, Node** front, Node** back); + static Node* MergeList(Node*, Node*); + static Node* MergeSort(Node*); + void Sort(); + + + + + + +}; + + +#endif //IKT203_COURSE_ASSIGNMENTS_TLINKEDLIST_H \ No newline at end of file diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/TPerson.cpp b/Exam/IKT203Exam/Portfolio/SharedLib/TPerson.cpp new file mode 100644 index 0000000..41effea --- /dev/null +++ b/Exam/IKT203Exam/Portfolio/SharedLib/TPerson.cpp @@ -0,0 +1,14 @@ +#include "TPerson.h" + + + + + + + + + + + + + diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/TPerson.h b/Exam/IKT203Exam/Portfolio/SharedLib/TPerson.h new file mode 100644 index 0000000..d10d94f --- /dev/null +++ b/Exam/IKT203Exam/Portfolio/SharedLib/TPerson.h @@ -0,0 +1,32 @@ +#ifndef IKT203_COURSE_ASSIGNMENTS_TPERSON_H +#define IKT203_COURSE_ASSIGNMENTS_TPERSON_H +#include + +#include "Utils.h" + +enum ENumStatus { + GUEST, + EMPLOYEE +}; + + +struct TPerson { + std::string firstName; + std::string lastName; + ENumStatus status; + int cabinSize = Utils::RandomInt(1, 4); + + TPerson(std::string f, std::string l, ENumStatus s) : firstName(std::move(f)), lastName(std::move(l)), status(s){} + ~TPerson() = default; + + bool operator<(const TPerson& other) const + { + if (lastName < other.lastName) return true; + if (lastName > other.lastName) return false; + // same last name → compare first name + return firstName < other.firstName; + } +}; + + +#endif //IKT203_COURSE_ASSIGNMENTS_TPERSON_H \ No newline at end of file diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp b/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp index 74ef15a..9c8d6f8 100644 --- a/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp +++ b/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp @@ -1,5 +1,6 @@ #include "Utils.h" +#include #include #include @@ -76,3 +77,17 @@ int Utils::RemoveLine(TDoublyLinkedList &document, TStack &undoStack, TStack &re } return index; } + +int Utils::RandomInt(const int min, const int max) +{ + static bool isSeeded = false; + if (!isSeeded) { + std::srand(static_cast(std::time(nullptr))); //<---- not the "best" random seeding available + isSeeded = true; // but sufficient for this use case + } + + if (max <= min) + return 0; + + return min + rand() % (max - min + 1); // <---- Limited randomness, but again +} // sufficient for this use case diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/Utils.h b/Exam/IKT203Exam/Portfolio/SharedLib/Utils.h index 29bbe3f..fbf115e 100644 --- a/Exam/IKT203Exam/Portfolio/SharedLib/Utils.h +++ b/Exam/IKT203Exam/Portfolio/SharedLib/Utils.h @@ -10,6 +10,7 @@ class Utils { static int Insert(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index); static void PrintList(const TDoublyLinkedList &document); static int RemoveLine(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index); + static int RandomInt(int, int);