Advertisement
evcamels

MILA

Mar 28th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. /*
  7. * односв€зный список
  8. * реализаци€
  9. */
  10.  
  11.  
  12. template<typename T>
  13. class List
  14. {
  15. public:
  16.     List();
  17.     ~List();
  18.  
  19.     //удаление первого элемента в списке
  20.     void pop_front();
  21.  
  22.     //добавление элемента в конец списка
  23.     void push_back(T data);
  24.  
  25.     // очистить список
  26.     void clear();
  27.  
  28.     // получить количество елементов в списке
  29.     int GetSize() { return Size; }
  30.  
  31.     // перегруженный оператор []
  32.     T& operator[](const int index);
  33.    
  34.     //добавление элемента в начало списка
  35.     void push_front(T data);
  36.  
  37.     //добавление элемента в список по указанному индексу
  38.     void insert(T data, int index);
  39.  
  40.     //удаление элемента в списке по указанному индексу
  41.     void removeAt(int index);
  42.    
  43.     //удаление последнего элемента в списке
  44.     void pop_back();
  45.  
  46. private:
  47.  
  48.  
  49.     template<typename T1>
  50.     class Node
  51.     {
  52.     public:
  53.         Node * pNext;
  54.         T data;
  55.  
  56.         Node(T1 data = T1(), Node *pNext = nullptr)
  57.         {
  58.             this->data = data;
  59.             this->pNext = pNext;
  60.         }
  61.     };
  62.     int Size;
  63.     Node<T> *head;
  64. };
  65.  
  66.  
  67. template<typename T>
  68. List<T>::List()
  69. {
  70.     Size = 0;
  71.     head = nullptr;
  72. }
  73.  
  74.  
  75. template<typename T>
  76. List<T>::~List()
  77. {
  78.     clear();
  79. }
  80.  
  81.  
  82. template<typename T>
  83. void List<T>::pop_front()
  84. {
  85.     Node<T> *temp = head;
  86.  
  87.     head = head->pNext;
  88.  
  89.     delete temp;
  90.  
  91.     Size--;
  92.  
  93. }
  94.  
  95. template<typename T>
  96. void List<T>::push_back(T data)
  97. {
  98.     if (head == nullptr)
  99.     {
  100.         head = new Node<T>(data);
  101.     }
  102.     else
  103.     {
  104.         Node<T> *current = this->head;
  105.  
  106.         while (current->pNext != nullptr)
  107.         {
  108.             current = current->pNext;
  109.         }
  110.         current->pNext = new Node<T>(data);
  111.  
  112.     }
  113.  
  114.     Size++;
  115. }
  116.  
  117. template<typename T>
  118. void List<T>::clear()
  119. {
  120.     while (Size)
  121.     {
  122.         pop_front();
  123.     }
  124. }
  125.  
  126.  
  127. template<typename T>
  128. T & List<T>::operator[](const int index)
  129. {
  130.     int counter = 0;
  131.  
  132.     Node<T> *current = this->head;
  133.  
  134.     while (current != nullptr)
  135.     {
  136.         if (counter == index)
  137.         {
  138.             return current->data;
  139.         }
  140.         current = current->pNext;
  141.         counter++;
  142.     }
  143.     return current->data;
  144. }
  145.  
  146. template<typename T>
  147. void List<T>::push_front(T data)
  148. {
  149.     head = new Node<T>(data, head);
  150.     Size++;
  151. }
  152.  
  153. template<typename T>
  154. void List<T>::insert(T data, int index)
  155. {
  156.  
  157.     if (index == 0)
  158.     {
  159.         push_front(data);
  160.     }
  161.     else
  162.     {
  163.         Node<T> *previous = this->head;
  164.  
  165.         for (int i = 0; i < index - 1; i++)
  166.         {
  167.             previous = previous->pNext;
  168.         }
  169.  
  170.         Node<T> *newNode = new Node<T>(data, previous->pNext);
  171.  
  172.         previous->pNext = newNode;
  173.  
  174.         Size++;
  175.     }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. }
  182.  
  183. template<typename T>
  184. void List<T>::removeAt(int index)
  185. {
  186.     if (index == 0)
  187.     {
  188.         pop_front();
  189.     }
  190.     else
  191.     {
  192.         Node<T> *previous = this->head;
  193.         for (int i = 0; i < index - 1; i++)
  194.         {
  195.             previous = previous->pNext;
  196.         }
  197.  
  198.        
  199.         Node<T> *toDelete = previous->pNext;
  200.  
  201.         previous->pNext = toDelete->pNext;
  202.  
  203.         delete toDelete;
  204.  
  205.         Size--;
  206.     }
  207.  
  208. }
  209.  
  210. template<typename T>
  211. void List<T>::pop_back()
  212. {
  213.     removeAt(Size - 1);
  214. }
  215.  
  216.  
  217.  
  218. int main()
  219. {
  220.  
  221.     setlocale(LC_ALL, "ru");
  222.  
  223.  
  224.     List<string> w;
  225.     List<string> p;
  226.     int n;
  227.     string word,page;
  228.     cout << "Введите количество слов: ";
  229.     cin >> n;
  230.     cin.ignore();
  231.     for(int i=0;i<n;i++){
  232.         cout << "Введите слово: ";
  233.         getline(cin,word);
  234.         w.push_back(word);
  235.         cout << "Введите номера страниц: ";
  236.         getline(cin,page);
  237.         p.push_back(page);
  238.     }
  239.     cout << "Полученный предметный указатель: " << endl;
  240.     for(int i=0;i<n;i++){
  241.         cout << w[i] << " : ";
  242.         cout << p[i] << " ";
  243.         cout << endl;
  244.     }
  245.     cout << "Введите слово для которого хотите узнать страницы: ";
  246.     string wo;
  247.     getline(cin,wo);
  248.     for(int i=0;i<n;i++){
  249.         if(wo == w[i]){
  250.             cout <<"Номера страниц для этого слова: " << p[i] << endl;
  251.         }
  252.     }
  253.     cout << "Слово с максимальным количеством страниц: ";
  254.     int max = 0;
  255.     int count = 0;
  256.     for(int i=0;i<n;i++){
  257.         if(p[i].length() > max){
  258.             max = p[i].length();
  259.             count = i;
  260.         }
  261.     }
  262.     cout << w[count] << endl;
  263.  
  264.    
  265.    
  266.  
  267.     return 0;
  268. }
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement