Advertisement
Guest User

list

a guest
Nov 7th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. /* односвязный список реализация */
  4. template<typename T>
  5. class List
  6. {
  7. public:
  8.     List();
  9.     ~List();
  10.     //удаление первого элемента в списке
  11.     void pop_front();
  12.     // очистить список
  13.     void clear();
  14.     // получить количество элементов в списке
  15.     int GetSize(){ return Size; }
  16.     // перегруженный оператор []
  17.     T& operator[](const int index);
  18.     //добавление элемента в начало списка
  19.     void push_front(T data);   
  20.     //замена местами 2-ух элементов в списке
  21.     void swap_val(int ind_1, int ind_2);
  22. private:
  23.     template<typename N>
  24.     class Node
  25.     {
  26.     public:
  27.         Node * pNext;
  28.         T data;
  29.  
  30.         Node(T data = T(), Node *pNext = NULL)
  31.         {
  32.             this->data = data;
  33.             this->pNext = pNext;
  34.         }
  35.     };
  36.     int Size;
  37.     Node<T> *head;
  38. };
  39.  
  40. template<typename T>
  41. List<T>::List()
  42. {
  43.     Size = 0;
  44.     head = NULL;
  45. }
  46.  
  47. template<typename T>
  48. List<T>::~List()
  49. {
  50.     clear();
  51. }
  52.  
  53. template<typename T>
  54. void List<T>::pop_front()
  55. {
  56.     Node<T> *temp = head;
  57.     head = head->pNext;
  58.     delete temp;
  59.     Size--;
  60. }
  61.  
  62. template<typename T>
  63. void List<T>::clear()
  64. {
  65.     while (Size)
  66.     {
  67.         pop_front();
  68.     }
  69. }
  70.  
  71. template<typename T>
  72. T & List<T>::operator[](const int index){
  73.     int counter = 0;
  74.     Node<T> *current = this->head;
  75.     while (current != NULL){
  76.         if (counter == index){
  77.             return current->data;}
  78.         current = current->pNext;
  79.         counter++;
  80.     }
  81. }
  82.  
  83. template<typename T>
  84. void List<T>::push_front(T data){
  85.     head = new Node<T>(data, head);
  86.     Size++;
  87. }
  88.  
  89. template<typename T>
  90. void List<T>::swap_val(int ind_1, int ind_2){
  91.         Node<T> *p1, *prev_p1, *p2, *prev_p2, *tmp_pos;
  92.         Node<T> *current = this->head;
  93.         prev_p1 = head;
  94.         prev_p2 = head;
  95.        
  96.         for(int i = 1; i < ind_1; i++) {
  97.             prev_p2 = prev_p2->pNext;
  98.         }
  99.        
  100.         p2 = prev_p2->pNext;
  101.        
  102.         for(int i = 1; i < ind_2; i++) {
  103.             prev_p1 = prev_p1->pNext;
  104.         }
  105.        
  106.         p1 = prev_p1->pNext;
  107.        
  108.         tmp_pos = p1->pNext;
  109.         p1->pNext = p2->pNext;
  110.         p2->pNext = tmp_pos;
  111.         prev_p1->pNext = p2;
  112.         prev_p2->pNext = p1;
  113.     }
  114.  
  115. int main(){
  116.     setlocale(LC_ALL, "ru");
  117.     List<int> lst;
  118.     lst.push_front(7);
  119.     lst.push_front(6);
  120.     lst.push_front(5);
  121.     lst.push_front(4);
  122.     lst.push_front(3);
  123.     lst.push_front(2);
  124.     lst.push_front(1);
  125.     lst.push_front(0);
  126.  
  127.     for (int i = 0; i < lst.GetSize(); i++){
  128.         cout << lst[i] << " ";
  129.     }
  130.  
  131.     cout << endl << "Введите индексы элементов которые хотите поменять местами" << endl << endl;
  132.     cout << endl << "Первый индекс: " << endl << endl;
  133.  
  134.     int first_ind;
  135.  
  136.     while(!(cin >> first_ind) || (first_ind > lst.GetSize() - 1)){
  137.         cout << "Ошибка, повторите ввод\n";
  138.         cin.clear();
  139.         fflush(stdin);
  140.     }
  141.    
  142.     cout << endl << "Второй индекс: " << endl << endl;
  143.    
  144.     int second_ind;
  145.    
  146.     while(!(cin >> second_ind) || (first_ind > lst.GetSize() - 1)){
  147.         cout << "Ошибка, повторите ввод\n";
  148.         cin.clear();
  149.         fflush(stdin);
  150.     }
  151.    
  152.     lst.swap_val(first_ind, second_ind);
  153.    
  154.     cout << endl << "swap_val(" << first_ind << ", " << second_ind << ")"<< endl << endl;
  155.    
  156.     for (int i = 0; i < lst.GetSize(); i++){
  157.         cout << lst[i] << " ";
  158.     }
  159.     return 0;
  160.    
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement