Advertisement
MaksNew

SLList.h

Feb 28th, 2021
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <regex>
  4. #include <sstream>
  5. #include <filesystem>
  6. #include <fstream>
  7.  
  8.     using namespace std;
  9.  
  10.     template<typename T>
  11.     class SinglyLinkedList
  12.     {
  13.     public:
  14.         SinglyLinkedList();
  15.         SinglyLinkedList(const SinglyLinkedList& other);
  16.         ~SinglyLinkedList();
  17.         void push_back(T data);
  18.         void push_front(T data);
  19.         void pop_front();
  20.         void clear();
  21.         void build_empty(size_t sizeOfList);
  22.         void insert(T data, int index);
  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>::insert(T data, int index)
  139.     {
  140.         if (index == 0)
  141.         {
  142.             push_front(data);
  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>* newNode = new Node<T>(data, previous->pNext);
  152.             previous->pNext = newNode;
  153.             ++size;
  154.         }
  155.     }
  156.  
  157.     template<typename T>
  158.     void SinglyLinkedList<T>::removeAt(int index)
  159.     {
  160.         if (index == 0)
  161.         {
  162.             pop_front();
  163.         }
  164.         else
  165.         {
  166.             Node<T>* previous = this->head;
  167.             for (size_t i = 0; i < index - 1; ++i)
  168.             {
  169.                 previous = previous->pNext;
  170.             }
  171.             Node<T>* temp = previous->pNext;
  172.             previous->pNext = temp->pNext;
  173.             delete temp;
  174.             size--;
  175.         }
  176.     }
  177.  
  178.     template<typename T>
  179.     void SinglyLinkedList<T>::pop_back()
  180.     {
  181.         removeAt(size - 1);
  182.     }
  183.  
  184.     template<typename T>
  185.     void print(SinglyLinkedList<T> list)
  186.     {
  187.         cout << endl << "№\t" << "элемент" << endl;
  188.         for (size_t i = 0; i < list.getSize(); ++i)
  189.         {
  190.             cout << i + 1 << "\t" << list[i] << endl;
  191.         }
  192.         cout << endl;
  193.     }
  194.  
  195.     template<typename T>
  196.     unsigned short allEntryOfElement(T element, SinglyLinkedList<T> list)
  197.     {
  198.         unsigned short counter = 0;
  199.         for (size_t i = 0; i < list.getSize(); ++i)
  200.         {
  201.             if (element == list[i])
  202.                 counter++;
  203.         }
  204.         return counter;
  205.     }
  206.  
  207.     bool isFileCorrect(string path);
  208.  
  209.     string readFilePath(bool flag);
  210.  
  211.     template<typename T>
  212.     void getListFromFile(SinglyLinkedList<T>& list)
  213.     {
  214.         bool flag = true;
  215.         string path = readFilePath(flag);
  216.         string inputline;
  217.         cmatch res;
  218.         regex reg("(\\d+)\\s*");
  219.         fstream fin(path, std::ios_base::in);
  220.         while (!fin.eof())
  221.         {
  222.             getline(fin, inputline);
  223.             auto inputline_begin = sregex_iterator(inputline.begin(), inputline.end(), reg);
  224.             auto inputline_end = sregex_iterator();
  225.             for (auto i = inputline_begin; i != inputline_end; ++i)
  226.             {
  227.                 list.push_back(stoi((*i).str()));
  228.             }
  229.         }
  230.         fin.close();
  231.     }
  232.  
  233.     int readElement(int data);
  234.  
  235.     void getListElementFromConsole(SinglyLinkedList<int>& list);
  236.  
  237.     int getMainMenuItem(bool item5);
  238.  
  239.     int readSizeOfEmptyList();
  240.  
  241.     size_t readDeletableElementIndex(size_t index, SinglyLinkedList<int>& list);
  242.  
  243.     string readFilePath();
  244.  
  245.     void saveList(SinglyLinkedList<int>& list);
  246.  
  247.     void selectMenuItem(int menuItem, SinglyLinkedList<int>& list);
  248.  
  249.     void showMenu(SinglyLinkedList<int> list);
  250.  
  251.     void selectMainMenuItem(int menuItem);
  252.  
  253.     void showMainMenu();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement