Advertisement
leo1553

IntList CPP

May 30th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. // CPPConsole.cpp : Define o ponto de entrada para a aplicaรงรฃo de console.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <functional>
  7. typedef std::function<void(int)> Action;
  8.  
  9. class List {
  10.     struct ListValue {
  11.         int value;
  12.         ListValue* next;
  13.  
  14.         ListValue(int val) {
  15.             value = val;
  16.             next = NULL;
  17.         }
  18.         ListValue(int val, ListValue* n) {
  19.             value = val;
  20.             next = n;
  21.         }
  22.     };
  23.  
  24.     int count = 0;
  25.     ListValue* root = NULL;
  26.  
  27. public:
  28.     bool Add(int value) {
  29.         if(root == NULL) {
  30.             root = new ListValue(value);
  31.             count++;
  32.             return true;
  33.         }
  34.         if(value < root->value) {
  35.             root = new ListValue(value, root);
  36.             count++;
  37.             return true;
  38.         }
  39.  
  40.         ListValue* current = root;
  41.         ListValue* previous = root;
  42.         while((current = current->next) != NULL) {
  43.             /*if(current->value == value)
  44.                 return false;
  45.             else*/ if(value < current->value) {
  46.                 previous->next = new ListValue(value, current);
  47.                 count++;
  48.                 return true;
  49.             }
  50.             previous = current;
  51.         }
  52.         previous->next = new ListValue(value);
  53.         count++;
  54.         return true;
  55.     }
  56.  
  57.     bool Remove(int value) {
  58.         if(root == NULL || value < root->value)
  59.             return false;
  60.         if(root->value == value) {
  61.             root = root->next;
  62.             count--;
  63.             return true;
  64.         }
  65.  
  66.         ListValue* current = root;
  67.         ListValue* previous = root;
  68.         while((current = current->next) != NULL && current->value <= value) {
  69.             if(current->value == value) {
  70.                 previous->next = current->next;
  71.                 count--;
  72.                 return true;
  73.             }
  74.             previous = current;
  75.         }
  76.         return false;
  77.     }
  78.  
  79.     bool Contains(int value) {
  80.         ListValue* current = root;
  81.         while(current != NULL && current->value <= value) {
  82.             if(current->value == value)
  83.                 return true;
  84.             current = current->next;
  85.         }
  86.         return false;
  87.     }
  88.  
  89.     void Iterate(Action action) {
  90.         for(ListValue* current = root; current != NULL; current = current->next)
  91.             action(current->value);
  92.     }
  93. };
  94.  
  95. int main() {
  96.     List* list = new List();
  97.     list->Add(5);
  98.     list->Add(10);
  99.     list->Add(1);
  100.     list->Add(7);
  101.     list->Add(-1);
  102.     list->Add(8);
  103.     list->Add(2);
  104.     list->Remove(5);
  105.     list->Remove(10);
  106.     list->Remove(-1);
  107.     list->Add(1);
  108.     list->Remove(1);
  109.     list->Remove(1);
  110.  
  111.     list->Iterate([](int i) {
  112.         printf("%d\n", i);
  113.     });
  114.  
  115.     getchar();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement