From b6e654657da058e9a438ec4583fac8028758dfb4 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 29 Dec 2022 09:09:41 +0200 Subject: [SDP/P/exa] Removed hard coded example and added user input reading --- .../Project/exa/SkipList.hpp | 9 ++++ .../Project/exa/main.cpp | 51 +++++++++++++--------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Year 2/Structures and data algorithms/Project/exa/SkipList.hpp b/Year 2/Structures and data algorithms/Project/exa/SkipList.hpp index 125013b..4821aef 100644 --- a/Year 2/Structures and data algorithms/Project/exa/SkipList.hpp +++ b/Year 2/Structures and data algorithms/Project/exa/SkipList.hpp @@ -32,6 +32,7 @@ public: bool addSkip(const T&, const T&); T pop(); T& front(); + T& back(); unsigned size(); bool empty(); @@ -175,6 +176,14 @@ T& SkipList::front() { return head->value; } +template +T& SkipList::back() { + if (empty()) + throw "Back called on empty SkipList!"; + + return tail->value; +} + template unsigned SkipList::size() { return listSize; diff --git a/Year 2/Structures and data algorithms/Project/exa/main.cpp b/Year 2/Structures and data algorithms/Project/exa/main.cpp index 88bf9ce..0beae8b 100644 --- a/Year 2/Structures and data algorithms/Project/exa/main.cpp +++ b/Year 2/Structures and data algorithms/Project/exa/main.cpp @@ -3,30 +3,41 @@ #include int main() { - SkipList cities; - cities.push("Sofia"); - cities.push("Pazardzhik"); - cities.push("Plovdiv"); - cities.push("Dimitrovgrad"); - cities.push("StaraZagora"); - cities.push("NovaZagora"); - cities.push("Yambol"); - cities.push("Karnobat"); - cities.push("Burgas"); + std::cout << "Брой на градовете през които преминава пътническия влак: "; + unsigned n = 0; + std::cin >> n; - cities.addSkip("Sofia", "Plovdiv"); - cities.addSkip("Plovdiv", "NovaZagora"); - cities.addSkip("Dimitrovgrad", "NovaZagora"); - cities.addSkip("StaraZagora", "Yambol"); - cities.addSkip("NovaZagora", "Burgas"); + std::cout << "Имената на градовете: "; + SkipList cities; + std::string temp1; + while (n-- > 0) { + std::cin >> temp1; + cities.push(temp1); + } - cities.print(std::cout) << std::endl; + std::cout << "Брой на директните връзки: "; + std::cin >> n; + std::cout << "Директни връзки:" << std::endl; + std::string temp2; + while (n-- > 0) { + std::cin >> temp1 >> temp2; + cities.addSkip(temp1, temp2); + } - std::deque locs = { "Plovdiv", "StaraZagora", "Yambol", "Burgas" }; - std::deque path = cities.shortestSublistIncluding(locs); + std::cout << "Брой на градовете на Аня и Ванката: "; + std::cin >> n; + std::cout << "Градовете на Аня и Ванката: "; + std::deque visitCities; + while (n-- > 0) { + std::cin >> temp1; + visitCities.push_back(temp1); + } + visitCities.push_back(cities.back()); - for (auto loc : path) { - std::cout << loc << ", "; + std::cout << "Решение:" << std::endl; + std::deque visited = cities.shortestSublistIncluding(visitCities); + for (auto city : visited) { + std::cout << city << ", "; } std::cout << std::endl; } -- cgit v1.3.1-23-gd82d