Guest User

Untitled

a guest
Dec 9th, 2019
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <list>
  5.  
  6. template <typename T>
  7. struct Node {
  8.     T   data;
  9.     Node* next;
  10. };
  11.  
  12. template <typename T>
  13. void showList(const Node<T>* head) {
  14.  
  15. }
  16.  
  17. template <typename T>
  18. Node<T>* arrayToList(const T tab[], size_t size) {
  19.     std::list <Node> l;
  20.  
  21.     Node z = new Node(arr[size], null);
  22.     l.push_front(z);
  23.  
  24.     for (int i = size-2; i >=0; i--) {
  25.         Node x = new Node(arr[i],z)
  26.         l.push_front(x);
  27.         z = x;
  28.     }
  29.  
  30.     return &z;
  31. }
  32.  
  33. template<typename T, typename Pred>
  34. Node<T>* extract(Node<T>*& head, Pred predicate) {
  35.     std::list <Node> tmp;
  36.     while (*head!= NULL) {
  37.         if (predicate(*head->data)) {
  38.             tmp.push_back(*head);
  39.         }
  40.     }
  41.     return tmp;
  42.  
  43. }
  44.  
  45. template <typename T>
  46. void deleteList(Node<T>*& head) {
  47.     head = null;
  48. }
  49.  
  50. bool isLong(const string& s) { return s.size() >= 5; }
  51.  
  52. int main() {
  53.     int tabi[] = { 2,1,4,3,6,5,7,8 };
  54.     size_t sizei = sizeof(tabi) / sizeof(tabi[0]);
  55.     Node<int> *listAi = arrayToList(tabi, sizei);
  56.     showList(listAi);
  57.     Node<int> *listBi = extract(
  58.         listAi, [](int n)->bool {return n % 2 == 0});
  59.     showList(listAi);
  60.     showList(listBi);
  61.     deleteList(listAi);
  62.     deleteList(listBi);
  63.  
  64.     string tabs[]{ "Kasia", "Ola", "Ala",
  65.                   "Zosia", "Ela", "Basia" };
  66.     size_t sizes = sizeof(tabs) / sizeof(tabs[0]);
  67.     Node<string> *listAs = arrayToList(tabs, sizes);
  68.     showList(listAs);
  69.     Node<string> *listBs = extract(listAs, isLong);
  70.     showList(listAs);
  71.     showList(listBs);
  72.     deleteList(listAs);
  73.     deleteList(listBs);
  74. }
RAW Paste Data