From 004fbc291e0df65265286699547657bbcb889f03 Mon Sep 17 00:00:00 2001 From: Christopher Sanden Date: Sat, 18 Oct 2025 17:17:11 +0200 Subject: [PATCH] Completed part 2 minus the bonus --- assignment1/.idea/editor.xml | 245 +++++++++++++++++- assignment1/.idea/workspace.xml | 54 ++-- assignment1/TMovie.h | 15 +- assignment1/TMovieList.cpp | 84 +++++- assignment1/TMovieList.h | 20 +- assignment1/TMovieNode.h | 25 +- .../Testing/Temporary/LastTest.log | 4 +- 7 files changed, 390 insertions(+), 57 deletions(-) diff --git a/assignment1/.idea/editor.xml b/assignment1/.idea/editor.xml index 641f275..9cf688e 100644 --- a/assignment1/.idea/editor.xml +++ b/assignment1/.idea/editor.xml @@ -1,7 +1,250 @@ - - - - - - - - - - - + + + + + + + + + - + { + "associatedIndex": 0 +} - - + + - + + @@ -97,6 +110,7 @@ diff --git a/assignment1/TMovie.h b/assignment1/TMovie.h index cf65bf0..0d611d6 100644 --- a/assignment1/TMovie.h +++ b/assignment1/TMovie.h @@ -4,14 +4,9 @@ #define IKT203_TMOVIE_H -#include #include #include -// I know there's debate on the use of namespace, but honestly -// I cba to write std:: before everything... -using namespace std; - enum EMovieGenreType{ ACTION = 1 << 0, COMEDY = 1<< 1, @@ -24,23 +19,23 @@ enum EMovieGenreType{ class TMovie { private: - string title; - string director; + std::string title; + std::string director; int year; EMovieGenreType genre; float score; public: - TMovie(string T, string D, int Y, EMovieGenreType G, float S) : + TMovie(std::string T, std::string D, int Y, EMovieGenreType G, float S) : title(std::move(T)), director(std::move(D)), year(Y), genre(G), score(S) {} // Simple getters - using [[nodiscard]] to give warning if value is not used - [[nodiscard]] string GetTitle() const + [[nodiscard]] std::string GetTitle() const { return title; } - [[nodiscard]] string GetDirector() const + [[nodiscard]] std::string GetDirector() const { return director; } diff --git a/assignment1/TMovieList.cpp b/assignment1/TMovieList.cpp index e750258..5beb21b 100644 --- a/assignment1/TMovieList.cpp +++ b/assignment1/TMovieList.cpp @@ -1,5 +1,81 @@ -// -// Created by csand on 13/10/2025. -// - #include "TMovieList.h" + +#include + +using namespace std; + +void TMovieList::Append(TMovie* m) +{ + auto* newNode = new TMovieNode(m); + newNode->SetPrevNode(tail); + tail->SetNextNode(newNode); + tail = newNode; + cout << "Appended '" << m->GetTitle() << "' into existing list of movies" <GetNextNode(); + newNode->SetNextNode(head->GetNextNode()); + head->SetNextNode(newNode); + + newNode->SetPrevNode(head); + if (old) + old->SetPrevNode(newNode); + + + cout << "Prepended '" << m->GetTitle() << "' into list of movies" < ugyldig + if (index < 0) + return nullptr; + + auto* node = head->GetNextNode(); + int i = 0; + while (node != nullptr && i < index) + { + node = node->GetNextNode(); + i++; + } + + // Index større enn lista -> ugyldig + if (node == nullptr) + return nullptr; + return node; + +} + +TMovie* TMovieList::GetAtIndex(int index) +{ + auto* node = NavigateToNode(index); + return node ? node->GetMovie() : nullptr; +} + +void TMovieList::Remove(int index) +{ + auto* node = NavigateToNode(index); + if (node == nullptr) + return; + + auto* prev = node->GetPrevNode(); + auto* next = node->GetNextNode(); + + if (prev) + prev->SetNextNode(next); + if (next) + next->SetPrevNode(prev); + else + tail = (prev ? prev : head); + + + if (head->GetNextNode() == nullptr) + tail = head; + + delete node; +} \ No newline at end of file diff --git a/assignment1/TMovieList.h b/assignment1/TMovieList.h index 8c11c05..3a1df02 100644 --- a/assignment1/TMovieList.h +++ b/assignment1/TMovieList.h @@ -4,16 +4,13 @@ #include "TMovie.h" #include "TMovieNode.h" - - -using namespace std; - class TMovieList { private: TMovieNode* head; + TMovieNode* tail; public: - TMovieList() : head(new TMovieNode(nullptr)) {} + TMovieList() : head(new TMovieNode(nullptr)), tail(head) {} ~TMovieList() { @@ -25,14 +22,15 @@ public: current = next; } head = nullptr; + tail = nullptr; } + void Append(TMovie* m); + void Prepend(TMovie* m); -void PushFront(TMovie* m) const - { - auto* n = new TMovieNode(m); - n->SetNextNode(head->GetNextNode()); - head->SetNextNode(n); - } + TMovieNode *NavigateToNode(int index); + + TMovie* GetAtIndex(int index); + void Remove(int index); }; diff --git a/assignment1/TMovieNode.h b/assignment1/TMovieNode.h index 9a37205..13cc3c7 100644 --- a/assignment1/TMovieNode.h +++ b/assignment1/TMovieNode.h @@ -3,22 +3,17 @@ #include "TMovie.h" - - -using namespace std; - - - class TMovieNode { private: TMovie* movie; TMovieNode* nextNode; + TMovieNode* prevNode; public: // constructor - explicit TMovieNode(TMovie* moviePointer) : movie(moviePointer), nextNode(nullptr) {} + explicit TMovieNode(TMovie* moviePointer) : movie(moviePointer), nextNode(nullptr), prevNode(nullptr) {} // destructor ~TMovieNode() @@ -27,8 +22,8 @@ public: movie = nullptr; } - // getter and setter for nextNode pointer - TMovieNode* GetNextNode() + // getters and setters for next and previous nodes + [[nodiscard]] TMovieNode* GetNextNode() const { return nextNode; } @@ -38,6 +33,18 @@ public: nextNode = next; } + [[nodiscard]] TMovieNode* GetPrevNode() const + { + return prevNode; + } + void SetPrevNode(TMovieNode* prev) + { + prevNode = prev; + } + [[nodiscard]] TMovie* GetMovie() const + { + return movie; + } }; diff --git a/assignment1/cmake-build-debug/Testing/Temporary/LastTest.log b/assignment1/cmake-build-debug/Testing/Temporary/LastTest.log index 78cae8c..6a101d7 100644 --- a/assignment1/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/assignment1/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Oct 13 17:51 W. Europe Summer Time +Start testing: Oct 18 13:54 W. Europe Summer Time ---------------------------------------------------------- -End testing: Oct 13 17:51 W. Europe Summer Time +End testing: Oct 18 13:54 W. Europe Summer Time