Advertisement
MaksNew

Untitled

Feb 15th, 2021
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <regex>
  3. #include <sstream>
  4. #include <filesystem>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. template<typename T>
  10. class SinglyLinkedList
  11. {
  12.     public:
  13.         SinglyLinkedList();
  14.         SinglyLinkedList(const SinglyLinkedList& other);
  15.         ~SinglyLinkedList();
  16.         void push_back(T data);
  17.         void push_front(T data);
  18.         void pop_front();
  19.         void clear();
  20.         void build_empty(size_t sizeOfList);
  21.         void insert(T data, int index);
  22.         void removeAt(int index);
  23.         void pop_back();
  24.         int getSize(){ return size;}
  25.         T operator[] (const int index);
  26.     private:
  27.         template<typename T>
  28.         class Node
  29.         {
  30.         public:
  31.             Node *pNext;
  32.             T data;
  33.             Node(T data = T(), Node *pNext = nullptr)
  34.             {
  35.                 this->data = data;
  36.                 this->pNext = pNext;
  37.             }
  38.         };
  39.         Node<T> *head;
  40.         size_t size;
  41. };
  42.  
  43. template<typename T>
  44. SinglyLinkedList<T>::SinglyLinkedList()
  45. {
  46.     size = 0;
  47.     head = nullptr;
  48. }
  49.  
  50. template<typename T>
  51. SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& other) : SinglyLinkedList()
  52. {
  53.     Node<T> *current = other.head;
  54.     while (current != nullptr)
  55.     {
  56.         push_back(current->data);
  57.         current = current->pNext;
  58.     }
  59. }
  60.  
  61. //template<typename T>
  62. //SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& other) : SinglyLinkedList() {
  63. //  for (auto current = other.head; current != nullptr; current = current->pNext) {
  64. //      push_back(current->data);
  65. //  }
  66. //}
  67.  
  68. template<typename T>
  69. SinglyLinkedList<T>::~SinglyLinkedList()
  70. {
  71.     clear();
  72. }
  73.  
  74. template<typename T>
  75. void SinglyLinkedList<T>::push_back(T data)
  76. {
  77.     if (head == nullptr)
  78.     {
  79.         head = new Node<T>(data);
  80.     }
  81.     else
  82.     {
  83.         Node<T> *current = this->head;
  84.         while (current->pNext != nullptr)
  85.         {
  86.             current = current->pNext;
  87.         }
  88.         current->pNext = new Node<T>(data);
  89.     }
  90.     size++;
  91. }
  92.  
  93. template<typename T>
  94. void SinglyLinkedList<T>::pop_front()
  95. {
  96.     Node<T> *temp = head;
  97.     head = head->pNext;
  98.     delete temp;
  99.     size--;
  100. }
  101.  
  102. template<typename T>
  103. T SinglyLinkedList<T>::operator[](const int index)
  104. {
  105.     size_t counter = 0;
  106.     Node<T> *current = this->head;
  107.     while (current != nullptr)
  108.     {
  109.         if (counter == index)
  110.         {
  111.             return current->data;
  112.         }
  113.         ++counter;
  114.         current = current->pNext;
  115.     }
  116. }
  117.  
  118. template<typename T>
  119. void SinglyLinkedList<T>::clear()
  120. {
  121.     while (size)
  122.     {
  123.         pop_front();
  124.     }
  125. }
  126.  
  127. template<typename T>
  128. void SinglyLinkedList<T>::build_empty(size_t sizeOfList)
  129. {
  130.     for (size_t i = 0; i < sizeOfList; ++i)
  131.     {
  132.         push_back(T());
  133.     }
  134. }
  135.  
  136. template<typename T>
  137. void SinglyLinkedList<T>::push_front(T data)
  138. {
  139.     head = new Node<T>(data, head);
  140.     size++;
  141. }
  142.  
  143. template<typename T>
  144. void SinglyLinkedList<T>::insert(T data, int index)
  145. {
  146.     if (index == 0)
  147.     {
  148.         push_front(data);
  149.     }
  150.     else
  151.     {
  152.         Node<T>* previous = this->head;
  153.         for (size_t i = 0; i < index - 1; ++i)
  154.         {
  155.             previous = previous->pNext;
  156.         }
  157.         Node<T> *newNode = new Node<T>(data, previous->pNext);
  158.         previous->pNext = newNode;
  159.         ++size;
  160.     }
  161. }
  162.  
  163. template<typename T>
  164. void SinglyLinkedList<T>::removeAt(int index)
  165. {
  166.     if (index == 0)
  167.     {
  168.         pop_front();
  169.     }
  170.     else
  171.     {
  172.         Node<T> *previous = this->head;
  173.         for (size_t i = 0; i < index - 1; ++i)
  174.         {
  175.             previous = previous->pNext;
  176.         }
  177.         Node<T> *temp = previous->pNext;
  178.         previous->pNext = temp->pNext;
  179.         delete temp;
  180.         size--;
  181.     }
  182. }
  183.  
  184. template<typename T>
  185. void SinglyLinkedList<T>::pop_back()
  186. {
  187.     removeAt(size - 1);
  188. }
  189.  
  190. template<typename T>
  191. int allEntryOfElement(T element, SinglyLinkedList<T> list)
  192. {
  193.     int counter = 0;
  194.     for (size_t i = 0; i < list.getSize(); ++i)
  195.     {
  196.         if (element == list[i])
  197.             counter++;
  198.     }
  199.     return counter;
  200. }
  201.  
  202. bool isFileCorrect(string path)
  203. {
  204.     string inputline;
  205.     regex regular("^(\\d+\\s*)+$");
  206.     fstream fin(path, ios_base::in);
  207.     bool isCorrect = true;
  208.     while (!fin.eof() && isCorrect)
  209.     {
  210.         getline(fin, inputline);
  211.         if (!(regex_match(inputline.c_str(), regular)))
  212.             isCorrect = false;
  213.     }
  214.     fin.close();
  215.     return isCorrect;
  216. }
  217.  
  218. string readFilePath(bool flag)
  219. {
  220.     string path;
  221.     bool isIncorrect;
  222.     do
  223.     {
  224.         isIncorrect = false;
  225.         cout << "Введите абсолютный путь к файлу: " << endl;
  226.         cin >> path;
  227.         if (!filesystem::exists(path))
  228.         {
  229.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  230.             isIncorrect = true;
  231.         }
  232.         else
  233.         {
  234.             if (flag && !isFileCorrect(path))
  235.             {
  236.                 cout << "Ошибка при чтении файла! Проверьте данные и попробуйте ещё раз!" << endl;
  237.                 isIncorrect = true;
  238.             }
  239.         }
  240.  
  241.     } while (isIncorrect);
  242.     return path;
  243. }
  244.  
  245. template<typename T>
  246. void getListFromFile(SinglyLinkedList<T> &list)
  247. {
  248.     bool flag = true;
  249.     string path = readFilePath(flag);
  250.     string inputline;
  251.     cmatch res;
  252.     regex reg("(\\d+)\\s*");
  253.     fstream fin(path, std::ios_base::in);
  254.     while (!fin.eof())
  255.     {
  256.         getline(fin, inputline);
  257.         auto inputline_begin = sregex_iterator(inputline.begin(), inputline.end(), reg);
  258.         auto inputline_end = sregex_iterator();
  259.         for (auto i = inputline_begin; i != inputline_end; ++i)
  260.         {
  261.             list.push_back(stoi((*i).str()));
  262.         }
  263.     }
  264.     fin.close();
  265. }
  266.  
  267. int getMainMenuItem()
  268. {
  269.     bool isIncorrect;
  270.     string inputLine;
  271.     int item = 0;
  272.     do
  273.     {
  274.         isIncorrect = false;
  275.         try
  276.         {
  277.             cin >> inputLine;
  278.             item = stoi(inputLine);
  279.         }
  280.         catch (...)
  281.         {
  282.             isIncorrect = true;
  283.             cout << "Введите целое число!" << endl;
  284.         }
  285.         if ((item != 1) && (item != 2) && (item != 3) && !isIncorrect)
  286.         {
  287.             cout << "Выберете один из пунктов!\n";
  288.             isIncorrect = true;
  289.         }
  290.     } while (isIncorrect);
  291.     return item;
  292. }
  293.  
  294. template<typename T>
  295. void print(SinglyLinkedList<T> list)
  296. {
  297.     cout << "Ваш список:" << endl;
  298.     for (size_t i = 0; i < list.getSize(); ++i)
  299.     {
  300.         cout << list[i] << " ";
  301.     }
  302.     cout << endl;
  303.     showMenu(getMainMenuItem());
  304. }
  305.  
  306. void showMenu(int menuItem)
  307. {
  308.     SinglyLinkedList<int> list;
  309.     switch (menuItem)
  310.     {
  311.     case 1:
  312.         getListFromFile(list);
  313.         break;
  314.     case 2:
  315.         getListFromConsole(list);
  316.         break;
  317.     case 3:
  318.         cout << "Введите размер списка\n";
  319.         list.build_empty(readSizeOfEmptyList());
  320.         break;
  321.     }
  322.     print(list);
  323. }
  324.  
  325. int readSizeOfEmptyList()
  326. {
  327.     bool isIncorrect;
  328.     string inputLine;
  329.     int size;
  330.     do
  331.     {
  332.         isIncorrect = false;
  333.         try
  334.         {
  335.             cin >> inputLine;
  336.             size = stoi(inputLine);
  337.         }
  338.         catch (...)
  339.         {
  340.             isIncorrect = true;
  341.             cout << "Введите целое число!" << endl;
  342.         }
  343.         if (!isIncorrect && size < 1 && size > 2000000)
  344.         {
  345.             cout << "Введите положительное число до 2*10^6!\n";
  346.             isIncorrect = true;
  347.         }
  348.     } while (isIncorrect);
  349.     return size;
  350. }
  351.  
  352. template<typename T>
  353. void getListFromConsole(SinglyLinkedList<T>& list)
  354. {
  355.     //cout <<
  356. }
  357.  
  358. void showMainMenu()
  359. {
  360.     int menuItem;
  361.     cout << "Выберете один из пунктов меню :" << endl;
  362.     cout << "1. Открыть список из файла." << endl;
  363.     cout << "2. Создать новый список." << endl;
  364.     cout << "3. Создать новый пустой список указанного размера." << endl;
  365.     menuItem = getMainMenuItem();
  366.     showMenu(menuItem);
  367. }
  368.  
  369. int main()
  370. {
  371.     setlocale(LC_ALL, "ru");
  372.     showMainMenu();
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement