avr39-ripe

linkedNodeListAdvanced

May 22nd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9.     int value = 0;
  10.     Node * prev = nullptr;
  11.     Node * next = nullptr;
  12. };
  13.  
  14. struct NodeList
  15. {
  16.     Node* head = nullptr;
  17.     Node* tail = nullptr;
  18.     Node* curr = nullptr;
  19.  
  20.     void add(int val)
  21.     {
  22.         if (!head)
  23.         {
  24.             head = new Node{ val };
  25.             tail = head;
  26.             return;
  27.         }
  28.         if (!tail->next)
  29.         {
  30.             tail->next = new Node{ val, tail };
  31.             tail = tail->next;
  32.             return;
  33.         }
  34.     };
  35.     void show()
  36.     {
  37.         Node* ptr = head;
  38.         if (!ptr) std::cout << "Empty NodeList!" << std::endl;
  39.         while (ptr)
  40.         {
  41.             std::cout << ptr->value << std::endl;
  42.             ptr = ptr->next;
  43.         }
  44.     }
  45.  
  46.     void showReverse()
  47.     {
  48.         Node* ptr = tail;
  49.         if (!ptr) std::cout << "Empty NodeList!" << std::endl;
  50.         while (ptr)
  51.         {
  52.             std::cout << ptr->value << std::endl;
  53.             ptr = ptr->prev;
  54.         }
  55.     }
  56.     void deleteLast()
  57.     {
  58.         Node* ptr = nullptr;
  59.         if (!tail) std::cout << "Empty NodeList!" << std::endl;
  60.         ptr = tail;
  61.         tail = tail->prev;
  62.         tail->next = nullptr;
  63.         std::cout << "Deleting.. " << ptr->value;
  64.         delete ptr;
  65.         std::cout << " Done!" << std::endl;
  66.     }
  67.  
  68.     void deleteAll()
  69.     {
  70.         Node* ptr = nullptr;
  71.         if (!tail) std::cout << "Empty NodeList!" << std::endl;
  72.         while (tail)
  73.         {
  74.             ptr = tail;
  75.             tail = tail->prev;
  76.             std::cout << "Deleting.. " << ptr->value;
  77.             delete ptr;
  78.             std::cout << " Done!" << std::endl;
  79.         }
  80.         head = nullptr;
  81.         tail = nullptr;
  82.  
  83.     }
  84.  
  85.     void forEach(std::function <void(int&)> funct)
  86.     {
  87.         Node* ptr = head;
  88.         if (!ptr) std::cout << "Empty NodeList!" << std::endl;
  89.         while (ptr)
  90.         {
  91.             funct(ptr->value);
  92.             ptr = ptr->next;
  93.         }
  94.     }
  95.  
  96.     void forEachReverse(std::function <void(int&)> funct)
  97.     {
  98.         Node* ptr = tail;
  99.         if (!ptr) std::cout << "Empty NodeList!" << std::endl;
  100.         while (ptr)
  101.         {
  102.             funct(ptr->value);
  103.             ptr = ptr->prev;
  104.         }
  105.     }
  106.  
  107.     int count()
  108.     {
  109.         Node* ptr = head;
  110.         if (!ptr) return 0;
  111.         int cnt = 0;
  112.         while (ptr)
  113.         {
  114.             cnt++;
  115.             ptr = ptr->next;
  116.         }
  117.         return cnt;
  118.     }
  119.  
  120.     void begin()
  121.     {
  122.         curr = head;
  123.     }
  124.  
  125.     void end()
  126.     {
  127.         curr = tail;
  128.     }
  129.  
  130.     Node* get()
  131.     {
  132.         return curr;
  133.     }
  134.  
  135.     Node* getNext()
  136.     {
  137.         curr = curr->next;
  138.         return curr;
  139.     }
  140.  
  141.     Node* getPrev()
  142.     {
  143.         curr = curr->prev;
  144.         return curr;
  145.     }
  146. };
  147.  
  148. int main()
  149. {
  150.     NodeList* list = new NodeList;
  151.     list->show();
  152.     list->add(2902);
  153.     list->add(2407);
  154.     list->add(2802);
  155.     list->add(2607);
  156.     list->show();
  157.     std::cout << "####" << std::endl;
  158.     list->showReverse();
  159.     list->deleteAll();
  160.     list->showReverse();
  161.     list->add(29022);
  162.     list->add(24073);
  163.     list->add(28024);
  164.     list->add(26075);
  165.     list->show();
  166.     std::cout << "####" << std::endl;
  167.     list->deleteLast();
  168.     list->forEach([](int& arg) {arg = arg + 1; });
  169.     list->forEachReverse([](int& arg) {std::cout << arg << std::endl; });
  170.     std::cout << "There are: " << list->count() << " nodes" << std::endl;
  171.     list->begin();
  172.     for (int i = 0; i < list->count(); i++)
  173.     {
  174.         std::cout << "Iteration " << i << " " << list->get()->value << std::endl;
  175.         list->getNext();
  176.     }
  177.     std::cout << "####" << std::endl;
  178.     list->end();
  179.     for (int i = 0; i < list->count(); i++)
  180.     {
  181.         std::cout << "Iteration " << i << " " << list->get()->value << std::endl;
  182.         list->getPrev();
  183.     }
  184.     list->deleteAll();
  185.     list->show();
  186. }
Advertisement
Add Comment
Please, Sign In to add comment