Advertisement
MaksNew

Untitled

Mar 3rd, 2021
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <regex>
  4. #include <sstream>
  5. #include <filesystem>
  6. #include <fstream>
  7.  
  8. namespace SLList {
  9.     using namespace std;
  10.  
  11.     template<typename T>
  12.     class SinglyLinkedList
  13.     {
  14.     public:
  15.         SinglyLinkedList();
  16.         SinglyLinkedList(const SinglyLinkedList& other);
  17.         ~SinglyLinkedList();
  18.         void push_back(T data);
  19.         void push_front(T data);
  20.         void pop_front();
  21.         void clear();
  22.         void build_empty(size_t sizeOfList);
  23.         void removeAt(int index);
  24.         void pop_back();
  25.         int getSize() { return size; }
  26.         T operator[] (const int index);
  27.     private:
  28.         template<typename T>
  29.         class Node
  30.         {
  31.         public:
  32.             Node* pNext;
  33.             T data;
  34.             Node(T data = T(), Node* pNext = nullptr)
  35.             {
  36.                 this->data = data;
  37.                 this->pNext = pNext;
  38.             }
  39.         };
  40.         Node<T>* head;
  41.         size_t size;
  42.     };
  43.  
  44.     template<typename T>
  45.     SinglyLinkedList<T>::SinglyLinkedList()
  46.     {
  47.         size = 0;
  48.         head = nullptr;
  49.     }
  50.  
  51.     template<typename T>
  52.     SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& other) : SinglyLinkedList()
  53.     {
  54.         Node<T>* current = other.head;
  55.         while (current != nullptr)
  56.         {
  57.             push_back(current->data);
  58.             current = current->pNext;
  59.         }
  60.     }
  61.  
  62.     template<typename T>
  63.     SinglyLinkedList<T>::~SinglyLinkedList()
  64.     {
  65.         clear();
  66.     }
  67.  
  68.     template<typename T>
  69.     void SinglyLinkedList<T>::push_back(T data)
  70.     {
  71.         if (head == nullptr)
  72.         {
  73.             head = new Node<T>(data);
  74.         }
  75.         else
  76.         {
  77.             Node<T>* current = this->head;
  78.             while (current->pNext != nullptr)
  79.             {
  80.                 current = current->pNext;
  81.             }
  82.             current->pNext = new Node<T>(data);
  83.         }
  84.         size++;
  85.     }
  86.  
  87.     template<typename T>
  88.     void SinglyLinkedList<T>::pop_front()
  89.     {
  90.         Node<T>* temp = head;
  91.         head = head->pNext;
  92.         delete temp;
  93.         size--;
  94.     }
  95.  
  96.     template<typename T>
  97.     T SinglyLinkedList<T>::operator[](const int index)
  98.     {
  99.         size_t counter = 0;
  100.         Node<T>* current = this->head;
  101.         while (current != nullptr)
  102.         {
  103.             if (counter == index)
  104.             {
  105.                 return current->data;
  106.             }
  107.             ++counter;
  108.             current = current->pNext;
  109.         }
  110.     }
  111.  
  112.     template<typename T>
  113.     void SinglyLinkedList<T>::clear()
  114.     {
  115.         while (size)
  116.         {
  117.             pop_front();
  118.         }
  119.     }
  120.  
  121.     template<typename T>
  122.     void SinglyLinkedList<T>::build_empty(size_t sizeOfList)
  123.     {
  124.         for (size_t i = 0; i < sizeOfList; ++i)
  125.         {
  126.             push_back(T());
  127.         }
  128.     }
  129.  
  130.     template<typename T>
  131.     void SinglyLinkedList<T>::push_front(T data)
  132.     {
  133.         head = new Node<T>(data, head);
  134.         size++;
  135.     }
  136.  
  137.     template<typename T>
  138.     void SinglyLinkedList<T>::removeAt(int index)
  139.     {
  140.         if (index == 0)
  141.         {
  142.             pop_front();
  143.         }
  144.         else
  145.         {
  146.             Node<T>* previous = this->head;
  147.             for (size_t i = 0; i < index - 1; ++i)
  148.             {
  149.                 previous = previous->pNext;
  150.             }
  151.             Node<T>* temp = previous->pNext;
  152.             previous->pNext = temp->pNext;
  153.             delete temp;
  154.             size--;
  155.         }
  156.     }
  157.  
  158.     template<typename T>
  159.     void SinglyLinkedList<T>::pop_back()
  160.     {
  161.         removeAt(size - 1);
  162.     }
  163.  
  164.     template<typename T>
  165.     void print(SinglyLinkedList<T> list)
  166.     {
  167.         cout << endl << "№\t" << "элемент" << endl;
  168.         for (size_t i = 0; i < list.getSize(); ++i)
  169.         {
  170.             cout << i + 1 << "\t" << list[i] << endl;
  171.         }
  172.         cout << endl;
  173.     }
  174.  
  175.     template<typename T>
  176.     unsigned short showAllEntryOfElement(T element, SinglyLinkedList<T> list)
  177.     {
  178.         unsigned short counter = 0;
  179.         for (size_t i = 0; i < list.getSize(); ++i)
  180.         {
  181.             if (element == list[i])
  182.                 counter++;
  183.         }
  184.         return counter;
  185.     }
  186.  
  187.     string readFilePath(bool flag);
  188.  
  189.     bool isFileCorrect(string path);
  190.  
  191.     template<typename T>
  192.     void getListFromFile(SinglyLinkedList<T>& list)
  193.     {
  194.         string path = readFilePath(true);
  195.         string inputline;
  196.         cmatch res;
  197.         regex reg("(\\d+)\\s*");
  198.         fstream fin(path, std::ios_base::in);
  199.         while (!fin.eof())
  200.         {
  201.             getline(fin, inputline);
  202.             auto inputline_begin = sregex_iterator(inputline.begin(), inputline.end(), reg);
  203.             auto inputline_end = sregex_iterator();
  204.             for (auto i = inputline_begin; i != inputline_end; ++i)
  205.             {
  206.                 list.push_back(stoi((*i).str()));
  207.             }
  208.         }
  209.         fin.close();
  210.     }
  211.  
  212.     int readElement(int data);
  213.  
  214.     void getListElementFromConsole(SinglyLinkedList<int>& list);
  215.  
  216.     int getMainMenuItem(bool item5);
  217.  
  218.     int readSizeOfEmptyList();
  219.  
  220.     size_t readDeletableElementIndex(size_t index, SinglyLinkedList<int>& list);
  221.  
  222.     void saveList(SinglyLinkedList<int>& list);
  223.  
  224.     void deleteElement(SinglyLinkedList<int>& list);
  225.  
  226.     void selectMenuItem(int menuItem, SinglyLinkedList<int>& list);
  227.  
  228.     void showMenu(SinglyLinkedList<int> list);
  229.  
  230.     void selectMainMenuItem(int menuItem);
  231.  
  232.     void showMainMenu();
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement