Advertisement
daniil_mironoff

lab4

May 23rd, 2020
1,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. template<class T>
  9. class container {
  10.     protected:
  11.         unsigned int SIZE;
  12.        
  13.     public:
  14.         container() : SIZE(0) { /* ... */ }
  15.  
  16.         virtual T operator[](unsigned int) =0;
  17.         virtual void push_back(const T&) =0;
  18.  
  19.         unsigned int size() { return SIZE; }
  20. };
  21.  
  22. ///////////////////////////////////////////////////////////
  23.  
  24. template<class T>
  25. struct link {
  26.     link<T>* next = NULL;
  27.     T value;
  28.  
  29. };
  30.  
  31. template<class T>
  32. class my_list : public container<T> {
  33.     protected:
  34.         link<T>* FIRST;
  35.         link<T>* LAST;
  36.  
  37.     public:
  38.         my_list() : container<T>(), FIRST(NULL), LAST(NULL) { /* ... */ }
  39.  
  40.         T operator[](unsigned int index) {
  41.             if (index >= container<T>::SIZE) {
  42.                 throw string (
  43.                     "Невалидный индекс для данного контейнера"
  44.                 );
  45.             }
  46.  
  47.             link<T>* cur_link = FIRST;
  48.  
  49.             while (index--) {
  50.                 cur_link = cur_link -> next;
  51.             }
  52.            
  53.             return cur_link -> value;
  54.         }
  55.  
  56.         void push_back(const T& value) {
  57.             // Проверка на одинаковые элементы
  58.             for (int i = 0; container<T>::SIZE > i; i++) {
  59.                 if ((*this)[i] == value) {
  60.                     throw string (
  61.                         "Одинаковое значение"
  62.                     );
  63.                 }
  64.             }
  65.  
  66.             link<T>* NEW_LINK = new link<T>();
  67.             NEW_LINK -> value = value;
  68.  
  69.             // Если нет элементов
  70.             if (container<T>::SIZE == 0) {
  71.                 FIRST = NEW_LINK;
  72.                 LAST = NEW_LINK;
  73.             } else
  74.            
  75.             // Если первый элемент больше
  76.             if (FIRST -> value > value) {
  77.                 NEW_LINK -> next = FIRST;
  78.                 FIRST = NEW_LINK;
  79.             } else
  80.  
  81.              // Если последний элемент меньше
  82.             if (LAST -> value < value) {
  83.                 LAST -> next = NEW_LINK;
  84.                 LAST = NEW_LINK;
  85.             }
  86.            
  87.             // ЕСЛИ ДВА И БОЛЬШЕ
  88.             else {
  89.                 link<T>* cur_link = FIRST;
  90.  
  91.                 while ((cur_link -> next -> value) > value) {
  92.                     cur_link = cur_link -> next;
  93.                 }
  94.  
  95.                 NEW_LINK -> next = cur_link -> next;
  96.                 cur_link -> next = NEW_LINK;
  97.             }
  98.  
  99.             container<T>::SIZE++;
  100.         }
  101. };
  102.  
  103. ///////////////////////////////////////////////////////////
  104.  
  105. int main() {
  106.     my_list<int> test_list;
  107.  
  108.     try {
  109.         unsigned int size_list;
  110.  
  111.         cout << "Выберите размер для контейнера типа forward_list: ";
  112.         cin >> size_list;
  113.  
  114.         for (int i = 0; size_list > i; i++) {
  115.             cout << "Введите значение для вставки в контейнер: ";
  116.             int value; cin >> value;
  117.             test_list.push_back(value);
  118.         }
  119.  
  120.         for (unsigned int i = 0; test_list.size() > i; i++) {
  121.             cout << "test_list[" << i << "]: " << test_list[i] << endl;
  122.         }
  123.        
  124.         unsigned int index;
  125.  
  126.         if (test_list.size()) {
  127.             cout << "Выберите индекс для отображения в контейнере: ";
  128.             cin >> index;
  129.             cout << "test_list[" << index << "]: " << test_list[index] << endl;
  130.         }
  131.     }
  132.  
  133.     catch(string err) {
  134.         cout << "Ошибка: " << err << endl;
  135.     }
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement