Advertisement
evcamels

lab-3_var-17

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