Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- //
- // Created by noname on 25.05.2017.
- //
- template <typename T>
- class List {
- public:
- List();
- List(const List&);
- List(const List& , const List&);//collide them
- ~List();
- bool is_empty() const;//obv
- void push_back(const T&);//adds an element to the start of the sequence
- void push_front(const T&);//adds an element to the end of sequence
- bool is_zeroed() const;
- void sort() const;
- void reset();
- T sum_of_greater_than(const T&) const;
- T sum() const;
- bool find_and_delete(const T&);
- T find_minimum();//check is_empty before calling this!!!!!!!!!!!
- template <typename callable>
- void for_each(callable&&);
- template <typename callable>
- void for_each(callable&&) const;
- private:
- struct Node {
- Node(const T& in) : value(in){}
- T value;
- Node* next = nullptr;
- };
- Node* head = nullptr;
- Node* last = nullptr;
- };
- template<typename T>
- List<T>::List() { }
- template<typename T>
- List<T>::List(const List& in) {
- auto p = this;
- //in lambdas "this" will not be defined the way its needed
- in.for_each([&p](T i){
- p->push_back(i);
- });
- }
- template<typename T>
- List<T>::List(const List& first, const List& second) {
- auto p = this;
- first.for_each([&p](auto i){
- p->push_back(i);
- });
- second.for_each([&p](auto i){
- p->push_back(i);
- });
- }
- template<typename T>
- List<T>::~List() {
- reset();
- }
- template <typename T>
- void List<T>::reset(){
- while (head != nullptr){
- auto temp = head;
- head = head->next;
- delete temp;
- }
- last = head;
- }
- template <typename T>
- bool List<T>::is_empty() const {
- return head == nullptr;
- };
- template <typename T>
- void List<T>::push_back(const T& in){
- auto temp = new Node(in);
- if (this->is_empty()){
- last = head = temp;
- } else {
- last = last->next = temp;
- }
- };
- template <typename T>
- void List<T>::sort() const {
- auto item = head;
- bool done = false;
- while (!done){
- done = true;
- auto i = head;
- while (i != nullptr){
- if ((i->next != nullptr ) && (i->value < i->next->value)){
- done = false;
- //swap them
- auto temp = i->value;
- i->value = i->next->value;
- i->next->value = temp;
- }
- i = i->next;
- }
- }
- }; //the worst sort method i have ever written
- template <typename T>
- T List<T>::sum_of_greater_than(const T & in) const {
- T sum = 0;
- for_each([&sum , in](auto item){
- if (item > in)
- sum += item;
- });
- return sum;
- };
- template <typename T>
- T List<T>::sum() const {
- T sum = 0;
- for_each([&sum](auto item){
- sum += item;
- });
- return sum;
- };
- template <typename T>
- bool List<T>::find_and_delete(const T & in) {
- auto tempPointer = head;
- auto tempValue = head->value;
- //dont wanna break the whole list
- if (head->value == in){
- if (head->next == nullptr){
- //only one and it matches. Delete it!
- delete head;
- head = last = nullptr;
- } else {
- //matches and there's something left, so the next one should be the head
- head = head->next;
- delete tempPointer;
- }
- return true;
- }
- //head is not the key, os we can simply skip it
- tempPointer = tempPointer->next;
- while (tempPointer != nullptr){
- if (tempPointer->value == in) {
- Node* previous = head;
- while (previous->next != tempPointer)
- previous = previous->next;
- if (tempPointer == last) {
- last = previous;
- last->next = nullptr;
- }
- else {
- previous->next = tempPointer->next;
- }
- delete tempPointer;
- return true;
- } else
- tempPointer = tempPointer->next;
- }
- return false;
- };
- template <typename T>
- void List<T>::push_front(const T& in) {
- auto temp = new Node(in);
- if (this->is_empty()){
- last = head = temp;
- } else {
- temp->next = head;
- head = temp;
- }
- };
- template <typename T>
- T List<T>::find_minimum() {
- T res = head->value;// NOT EMPTY-SAFE!
- for_each([&res](auto item){
- if (item < res)
- res = item;
- });
- return res;
- }
- template <typename T>
- template <typename callable>
- void List<T>::for_each(callable&& f) {
- auto item = head;
- while (item != nullptr){
- auto next = item->next;
- f(item->value);
- item = next;
- }
- return;
- };
- template <typename T>
- template <typename callable>
- void List<T>::for_each(callable&& f) const {
- auto item = head;
- while (item != nullptr){
- auto next = item->next;
- f(item->value);
- item = next;
- }
- return;
- };
- template <typename T>
- bool List<T>::is_zeroed() const {
- try{
- for_each([](T& i) {
- if (i != 0)
- throw 0;
- });
- } catch (...){
- return false;
- }
- return true;
- };
- template <>
- bool List<List<int>>::is_zeroed() const{
- try {
- for_each([](List<int> &i) {
- if (!i.is_zeroed())
- throw 0;
- });
- } catch (...){
- return false;
- }
- return true;
- };
- uint32_t inputPositiveNumber(){
- int64_t temp;
- std::cin >> temp;
- if (temp <= 0){
- std::cout <<"\nPlease input positive numbers only:";
- return inputPositiveNumber();
- }
- return static_cast<uint32_t>(temp);
- }
- void test(){
- List<int> a;
- int sum = 0;
- a.push_front(-10);
- a.push_front(-30);
- for (int i = -1 ; i < 2 ; i++)
- a.push_back(7*i);
- a.sort();
- a.find_and_delete(4);
- return;
- }
- int main(){
- test();
- List<List<int>> islands;
- uint32_t islandCount = 0;
- std::cout << "Input the number of islands:";
- islandCount = inputPositiveNumber();
- for (uint32_t i = 0; i < islandCount; ++i){
- std::cout << "\nFilling island number " << i + 1;
- List<int> tempIsland;
- std::cout << "\nInput the number of rabbits here:";
- auto rabbitsCount = inputPositiveNumber();
- for (uint32_t j = 0; j < rabbitsCount; ++j){
- std::cout << "Input the weight of rabbit number " << j + 1 << ":";
- tempIsland.push_back(inputPositiveNumber());
- }
- //little trick here.
- //push front coz we need to go backwards c;
- islands.push_front(tempIsland);
- }
- List<int> boat;
- std::cout << "Input the boat capacity:";
- auto boatCapacity = inputPositiveNumber();
- //end of input
- while (!islands.is_zeroed()){//while the islands are not tottaly empty
- //lets try so save them
- islands.for_each([&boat, &boatCapacity](List<int>& currIsland){
- List<int> buffer(boat , currIsland);
- boat.reset(); currIsland.reset();
- buffer.sort();
- List<int> currIslandCopy(currIsland);
- buffer.for_each([&boat , &boatCapacity , &currIslandCopy](int i){
- if (i + boat.sum() <= boatCapacity){
- //able to grab this guy
- boat.push_back(i);
- } else {
- //unable to grab him
- currIslandCopy.push_back(i);
- }
- });
- currIsland = currIslandCopy;
- });
- std::cout << "\nThe result of another swimming adventure:\n";
- islands.for_each([](auto island){
- island.for_each([](auto weight){
- std::cout << weight << ' ';
- });
- std::cout << "< - - - >\n";
- });
- std::cout << "Press any key to continue the cycle";
- std::cin.ignore();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement