Advertisement
Guest User

List_all_2

a guest
May 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. class Exception
  7. {
  8. private:
  9.     string content;
  10. public:
  11.     Exception(string message) : content(message) {}
  12.  
  13.     string Message()
  14.     {
  15.         return content;
  16.     }
  17. };
  18.  
  19. template<class Item> class List
  20. {
  21. private:
  22.     struct Element
  23.     {
  24.         Item inf;
  25.         Element* next;
  26.  
  27.         Element(Item inf, Element* next) : inf(inf), next(next) {}
  28.     };
  29.  
  30.     Element* head;
  31.     int length;
  32.  
  33.     Element* find_index(int index)
  34.     {
  35.         if (index < 0 || index >= length) throw Exception("List - Index error");
  36.  
  37.         Element* t = head;
  38.         while (index > 0)
  39.         {
  40.             t = t->next;
  41.             index--;
  42.         }
  43.  
  44.         return t;
  45.     }
  46. public:
  47.     List()
  48.     {
  49.         head = 0;
  50.         length = 0;
  51.     }
  52.  
  53.     void Add(Item data)
  54.     {
  55.         if (length == 0)
  56.         {
  57.             head = new Element(data, 0);
  58.             length++;
  59.             return;
  60.         }
  61.  
  62.         find_index(length - 1)->next = new Element(data, 0);
  63.         length++;
  64.     }
  65.  
  66.     void Insert(Item data, int index)
  67.     {
  68.         if (index == 0)
  69.         {
  70.             head = new Element(data, head);
  71.             length++;
  72.             return;
  73.         }
  74.  
  75.         if (index == length)
  76.         {
  77.             Add(data);
  78.             return;
  79.         }
  80.  
  81.         Element* t = find_index(index - 1);
  82.         t->next = new Element(data, t->next);
  83.         length++;
  84.     }
  85.  
  86.     Item Get(int index)
  87.     {
  88.         return find_index(index)->inf;
  89.     }
  90.  
  91.     int Length()
  92.     {
  93.         return length;
  94.     }
  95. };
  96.  
  97. void with_list(int* a, int n)
  98. {
  99.     cout << "--- List ---\n";
  100.  
  101.     List<int>* l = new List<int>();
  102.  
  103.     for (int i = 0; i < n; i++)
  104.     {
  105.         l->Add(a[i]);
  106.     }
  107.  
  108.     int sum = 0;
  109.     List<int>* l2 = new List<int>();
  110.     int t;
  111.  
  112.     for (int i = 0; i < l->Length(); i++)
  113.     {
  114.         t = l->Get(i);
  115.         cout << t << " ";
  116.         if (t > 0)
  117.         {
  118.             sum += t;
  119.             l2->Add(t);
  120.         }
  121.     }
  122.  
  123.     cout << "\n" << sum << "\n";
  124.     for (int i = 0; i < l2->Length(); i++)
  125.     {
  126.         cout << l2->Get(i) << " ";
  127.     }
  128.     cout << "\n--- --- ---\n";
  129. }
  130.  
  131. template<class Item> class DLList
  132. {
  133. private:
  134.     struct Element
  135.     {
  136.         Item inf;
  137.         Element* next;
  138.         Element* prev;
  139.  
  140.         Element(Item data, Element* next, Element* prev) : inf(data), next(next), prev(prev) {}
  141.     };
  142.  
  143.     Element* head;
  144.     Element* tail;
  145.     int length;
  146. public:
  147.     DLList()
  148.     {
  149.         head = tail = 0;
  150.         length = 0;
  151.     }
  152.  
  153.     int Length()
  154.     {
  155.         return length;
  156.     }
  157.  
  158.     void Add(Item data)
  159.     {
  160.         if (length == 0)
  161.         {
  162.             head = tail = new Element(data, 0, 0);
  163.             length++;
  164.             return;
  165.         }
  166.  
  167.         tail->next = new Element(data, tail, 0);
  168.         tail = tail->next;
  169.         length++;
  170.     }
  171.  
  172.     Item Get(int index)
  173.     {
  174.         if (index < 0 || index >= length) throw Exception("DLList - Index error");
  175.  
  176.         Element* t = head;
  177.         while (index > 0)
  178.         {
  179.             t = t->next;
  180.             index--;
  181.         }
  182.  
  183.         return t->inf;
  184.     }
  185. };
  186.  
  187. void with_dllist(int* a, int n)
  188. {
  189.     cout << "--- Double Linked List ---\n";
  190.  
  191.     DLList<int>* l = new DLList<int>();
  192.  
  193.     for (int i = 0; i < n; i++)
  194.     {
  195.         l->Add(a[i]);
  196.     }
  197.  
  198.     int sum = 0;
  199.     DLList<int>* l2 = new DLList<int>();
  200.     int t;
  201.  
  202.     for (int i = 0; i < l->Length(); i++)
  203.     {
  204.         t = l->Get(i);
  205.         cout << t << " ";
  206.         if (t > 0)
  207.         {
  208.             sum += t;
  209.             l2->Add(t);
  210.         }
  211.     }
  212.  
  213.     cout << "\n" << sum << "\n";
  214.     for (int i = 0; i < l2->Length(); i++)
  215.     {
  216.         cout << l2->Get(i) << " ";
  217.     }
  218.     cout << "\n--- --- ---\n";
  219. }
  220.  
  221. template<class Item> class Stack
  222. {
  223. private:
  224.     struct Element
  225.     {
  226.         Item inf;
  227.         Element* next;
  228.  
  229.         Element(Item data, Element* next) : inf(data), next(next){}
  230.     };
  231.  
  232.     Element* head;
  233.     int length;
  234. public:
  235.     Stack()
  236.     {
  237.         head = 0;
  238.         length = 0;
  239.     }
  240.  
  241.     int Length()
  242.     {
  243.         return length;
  244.     }
  245.  
  246.     void Add(Item data)
  247.     {
  248.         if (length == 0)
  249.         {
  250.             head = new Element(data, 0);
  251.             length++;
  252.             return;
  253.         }
  254.  
  255.         head = new Element(data, head);
  256.         length++;
  257.     }
  258.  
  259.     Item Get()
  260.     {
  261.         Element * t = head;
  262.         head = head->next;
  263.         length--;
  264.  
  265.         int res = t->inf;
  266.         delete t;
  267.  
  268.         return res;
  269.     }
  270. };
  271.  
  272. void with_stack(int* a, int n)
  273. {
  274.     cout << "--- Stack ---\n";
  275.  
  276.     Stack<int>* l = new Stack<int>();
  277.  
  278.     for (int i = 0; i < n; i++)
  279.     {
  280.         l->Add(a[i]);
  281.     }
  282.  
  283.     int sum = 0;
  284.     Stack<int>* l2 = new Stack<int>();
  285.     int t;
  286.  
  287.     for (int i = 0; l->Length() > 0; i++)
  288.     {
  289.         t = l->Get();
  290.         cout << t << " ";
  291.         if (t > 0)
  292.         {
  293.             sum += t;
  294.             l2->Add(t);
  295.         }
  296.     }
  297.  
  298.     cout << "\n" << sum << "\n";
  299.     for (int i = 0; l2->Length() > 0; i++)
  300.     {
  301.         cout << l2->Get() << " ";
  302.     }
  303.     cout << "\n--- --- ---\n";
  304. }
  305.  
  306. template<class Item> class Queue
  307. {
  308. private:
  309.     struct Element
  310.     {
  311.         Item inf;
  312.         Element* next;
  313.  
  314.         Element(Item data, Element* next) : inf(data), next(next) {}
  315.     };
  316.  
  317.     Element* head;
  318.     Element* tail;
  319.     int length;
  320. public:
  321.     Queue()
  322.     {
  323.         head = tail = 0;
  324.         length = 0;
  325.     }
  326.  
  327.     int Length()
  328.     {
  329.         return length;
  330.     }
  331.  
  332.     void Add(Item data)
  333.     {
  334.         if (length == 0)
  335.         {
  336.             head = tail = new Element(data, 0);
  337.             length++;
  338.             return;
  339.         }
  340.  
  341.         tail->next = new Element(data, 0);
  342.         tail = tail->next;
  343.         length++;
  344.     }
  345.  
  346.     Item Get()
  347.     {
  348.         Element* t = head;
  349.         head = head->next;
  350.         length--;
  351.  
  352.         int res = t->inf;
  353.         delete t;
  354.  
  355.         return res;
  356.     }
  357. };
  358.  
  359. void with_queue(int* a, int n)
  360. {
  361.     cout << "--- Queue ---\n";
  362.  
  363.     Queue<int>* l = new Queue<int>();
  364.  
  365.     for (int i = 0; i < n; i++)
  366.     {
  367.         l->Add(a[i]);
  368.     }
  369.  
  370.     int sum = 0;
  371.     Queue<int>* l2 = new Queue<int>();
  372.     int t;
  373.  
  374.     for (int i = 0; l->Length() > 0; i++)
  375.     {
  376.         t = l->Get();
  377.         cout << t << " ";
  378.         if (t > 0)
  379.         {
  380.             sum += t;
  381.             l2->Add(t);
  382.         }
  383.     }
  384.  
  385.     cout << "\n" << sum << "\n";
  386.     for (int i = 0; l2->Length() > 0; i++)
  387.     {
  388.         cout << l2->Get() << " ";
  389.     }
  390.     cout << "\n--- --- ---\n";
  391. }
  392.  
  393. int main()
  394. {
  395.     int n = 10;
  396.     int* a = new int[n];
  397.  
  398.     for (int i = 0; i < n; i++)
  399.     {
  400.         if (i % 2) a[i] = i;
  401.         else a[i] = -i;
  402.     }
  403.  
  404.     with_list(a, n);
  405.     with_dllist(a, n);
  406.     with_stack(a, n);
  407.     with_queue(a, n);
  408.  
  409.     //getc(stdin);
  410.     return 0;
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement