Last active
June 6, 2021 09:52
-
-
Save jin-x/2d1fb8241ba16d2a98b3bc0f0aa0fec4 to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #191
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <list> | |
using std::cout; | |
using std::cin; | |
using std::string; | |
using std::vector; | |
using std::list; | |
list<int> hospital_list(const vector<string>& lst) | |
{ | |
list<int> result; | |
auto middle = result.end(); | |
bool end_ins, inc_middle = true; | |
std::size_t pos, count = 0; | |
for (auto& s : lst) { | |
end_ins = true; | |
switch (s[0]) { | |
case '*': | |
end_ins = false; | |
[[fallthrough]]; // to avoid doubling lines of next 'case' | |
case '+': | |
{ | |
int n = stoi(s.substr(1), &pos); // pitient number | |
if (pos != s.length()-1) { throw std::invalid_argument("extra chars"); } | |
if (end_ins) { result.push_back(n); } // '+' | |
else { result.insert(std::next(middle), n); } // '*'; 'next' is used because of 'insert_after' method absence | |
} | |
--count; // compensation of ++count on 'fallthrough' to next 'case' | |
[[fallthrough]]; // to avoid doubling ++middle !inc_middle lines :) | |
case '-': | |
if (!result.empty()) { | |
++count; | |
if (inc_middle) { ++middle; } | |
inc_middle = !inc_middle; | |
break; | |
} else { throw std::invalid_argument("empty queue"); } | |
default: | |
throw std::invalid_argument("wrong command"); | |
} // switch | |
} // for | |
if (result.size() > count) { result.resize(count); } // to remove superfluous | |
return result; | |
} // hospital_list | |
void print_list(const list<int>& lst) | |
{ | |
cout << "{ "; | |
for (int n : lst) { | |
cout << n << ' '; | |
} // for | |
cout << "}\n"; | |
} // print_list | |
int main() | |
{ | |
print_list(hospital_list({ "+1", "+2", "-", "+3", "+4", "-", "-" })); | |
print_list(hospital_list({ "+1", "+2", "*3", "-", "+4", "*5", "-", "-", "-", "-" })); | |
return 0; | |
} // main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <forward_list> | |
using std::cout; | |
using std::cin; | |
using std::string; | |
using std::vector; | |
using std::forward_list; | |
forward_list<int> hospital_list(const vector<string>& lst) | |
{ | |
forward_list<int> result; | |
forward_list<int>::iterator middle, last, after; | |
bool end_ins, inc_middle = true; | |
std::size_t pos, count = 0; | |
for (auto& s : lst) { | |
end_ins = true; | |
switch (s[0]) { | |
case '*': | |
end_ins = false; | |
[[fallthrough]]; // to avoid doubling lines of next 'case' | |
case '+': | |
{ | |
int n = stoi(s.substr(1), &pos); // pitient number | |
if (pos != s.length()-1) { throw std::invalid_argument("extra chars"); } | |
if (result.empty()) { | |
result.push_front(n); | |
middle = last = result.begin(); | |
inc_middle = !inc_middle; | |
break; | |
} | |
else if (end_ins) { last = result.insert_after(last, n); } | |
else { result.insert_after(middle, n); } | |
} | |
--count; // compensation of ++count on 'fallthrough' to next 'case' | |
[[fallthrough]]; // to avoid doubling ++middle !inc_middle lines :) | |
case '-': | |
if (!result.empty()) { | |
++count; | |
if (inc_middle) { middle = std::next(middle); } | |
inc_middle = !inc_middle; | |
break; | |
} else { throw std::invalid_argument("empty queue"); } | |
default: | |
throw std::invalid_argument("wrong command"); | |
} // switch | |
} // for | |
result.resize(count); // to remove superfluous | |
return result; | |
} // hospital_list | |
void print_list(const forward_list<int>& lst) | |
{ | |
cout << "{ "; | |
for (int n : lst) { | |
cout << n << ' '; | |
} // for | |
cout << "}\n"; | |
} // print_list | |
int main() | |
{ | |
print_list(hospital_list({ "+1", "+2", "-", "+3", "+4", "-", "-" })); | |
print_list(hospital_list({ "+1", "+2", "*3", "-", "+4", "*5", "-", "-", "-", "-" })); | |
return 0; | |
} // main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <deque> | |
using std::cout; | |
using std::cin; | |
using std::string; | |
using std::vector; | |
using std::deque; | |
deque<int> hospital_deque(const vector<string>& list) | |
{ | |
deque<int> result; | |
std::size_t pos, count = 0; | |
for (auto& s : list) { | |
switch (s[0]) { | |
case '+': | |
result.push_back(stoi(s.substr(1), &pos)); | |
if (pos != s.length()-1) { throw std::invalid_argument("extra chars"); } | |
break; | |
case '*': | |
result.insert(result.begin() + count + (result.size()+1)/2, stoi(s.substr(1), &pos)); | |
if (pos != s.length()-1) { throw std::invalid_argument("extra chars"); } | |
break; | |
case '-': | |
if (!result.empty()) { | |
count++; | |
break; | |
} else { throw std::invalid_argument("empty queue"); } | |
default: | |
throw std::invalid_argument("wrong command"); | |
} // switch | |
} // for | |
if (result.size() > count) { result.resize(count); } | |
return result; | |
} // hospital_deque | |
void print_deque(const deque<int>& list) | |
{ | |
cout << "{ "; | |
for (int n : list) { | |
cout << n << ' '; | |
} // for | |
cout << "}\n"; | |
} // print_deque | |
int main() | |
{ | |
print_deque(hospital_deque({ "+1", "+2", "-", "+3", "+4", "-", "-" })); | |
print_deque(hospital_deque({ "+1", "+2", "*3", "-", "+4", "*5", "-", "-", "-", "-" })); | |
return 0; | |
} // main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment