Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. using namespace std;
  5. int o = 0;
  6. double s_t, e_t;
  7.  
  8. struct Node
  9. {
  10.     int number;
  11.     Node*next;
  12.     Node*back;
  13. };
  14.  
  15. struct List{
  16.     int count;
  17.     Node*head;
  18.     Node*tail;
  19. };
  20.  
  21. // Добавить ячейку
  22. void addNode(List *L){
  23.     Node *temp = new Node;
  24.     temp->number = rand()%10;
  25.     temp->next = NULL;
  26.     if(L->head != NULL){                           // Если список не пуст
  27.         temp->back = L->tail;
  28.         L->tail->next = temp;
  29.         L->tail = temp;
  30.     } else {                                        // Если пустой
  31.         temp->back = NULL;
  32.         L->head = L->tail = temp;
  33.     }
  34. }
  35.  
  36. void insertNode(List *L){
  37.     int n;
  38.     cout << "\nВведите номер для новой ячейки: ";
  39.     cin >> n;
  40.     while(n<0 || n>L->count)
  41.     {
  42.         cout<<"\nТакой ячейки нет. Введите номер ячейки заново: ";
  43.         cin>> n;
  44.     }
  45.     s_t = clock();
  46.     Node*temp=new Node;
  47.     temp->number=rand()%10;
  48.     if(n == 0){
  49.         // вставка в начало
  50.         L->head->back=temp;
  51.         temp->next=L->head;
  52.         L->head=temp;
  53.     } else if(n == L->count){
  54.         // вставка в конец
  55.         addNode(L);
  56.     } else {
  57.         // вставка в серидину
  58.         Node*random=L->head;
  59.         for(int i=0; i<n-1; i++)
  60.         {
  61.             random=random->next;
  62.         }
  63.         random->next->back=temp;
  64.         temp->back=random;
  65.         temp->next = random->next;
  66.         random->next=temp;
  67.     }
  68.     L->count++;
  69.     temp = L->head;
  70.     for(int i = 0; i < L->count; i++){
  71.         cout << temp->number << " ";
  72.         temp = temp->next;
  73.     }
  74.     cout << endl;
  75. }
  76.  
  77. void createList(List *L){
  78.     L->head = NULL;
  79.     L->tail = NULL;
  80.     int n;
  81.     cout << "Введите длину двусвязного списка: ";
  82.     cin >> n;
  83.     while(n < 1){
  84.         cout << "Введите заново: ";
  85.         cin >> n;
  86.     }
  87.     L->count = n;
  88.     for(int i = 0; i < n; i++){
  89.         addNode(L);
  90.     }
  91.     Node *temp = L->head;
  92.     for(int i = 0; i < n; i++){
  93.         cout << temp->number << " ";
  94.         temp = temp->next;
  95.     }
  96. }
  97.  
  98. void deleteNode(List *L){
  99.     int n;
  100.     cout << endl;
  101.     do{
  102.         cout << "Какую ячейку удалим?) Номер: ";
  103.         cin>>n;
  104.     } while(n < 0 || n > L->count);
  105.     s_t = clock();
  106.     if((n == 0) && (L->head->next)){               // Если удаляется Ячейка под номером 1, но помимо его есть еще и другие
  107.         Node *temp = L->head;                      // Указываем на начало списка
  108.         L->head=L->head->next;                    // Сдвигаем начало на пункт номер 2
  109.         L->count--;
  110.         Node *temp3 = L->head;
  111.         for(int i = 0; i < n; i++){
  112.             cout << temp3->number << " ";
  113.             temp3 = temp3->next;
  114.         }
  115.         return;                                     // Выходим из функции
  116.     } else if((n == 0) && (L->head=L->tail)){      // Если удаляется Ячейка под номером 1, при условии что в списке только он
  117.         L->head->next = NULL;                      // Обнуляем элементы
  118.         L->head = NULL;            
  119.         L->count--;
  120.         Node *temp3 = L->head;
  121.         for(int i = 0; i < n; i++){
  122.             cout << temp3->number << " ";
  123.             temp3 = temp3->next;
  124.         }
  125.         return;
  126.     }
  127.     if(n == L->count-1){                              // Если удаляют Ячейку из конца списка
  128.         Node *temp = L->tail;                    // Смещаем указатель последнего элемента на тот, который стоял перед ним
  129.         L->tail = L->tail->back;                    // Обнуляем крайний УДАЛЕННЫЙ элемент
  130.         L->tail->next = NULL;
  131.         L->count--;
  132.         Node *temp3 = L->head;
  133.         for(int i = 0; i < n; i++){
  134.             cout << temp3->number << " ";
  135.             temp3 = temp3->next;
  136.         }
  137.         return;
  138.     }
  139.     Node *temp = L->head, *temp2;
  140.     for(int i = 0; i < n; i++){ // Добираемся до нужного элемента
  141.         temp = temp->next;
  142.     }
  143.     temp2 = temp;                                   // Временно запоминаем адресс удаляемого элемента
  144.     temp2->back->next = temp->next;
  145.     temp2->next->back = temp->back;
  146.     L->count--;
  147.     Node *temp3 = L->head;
  148.     for(int i = 0; i < L->count; i++){
  149.         cout << temp3->number << " ";
  150.         temp3 = temp3->next;
  151.     }
  152. }
  153.  
  154. void findNode(List *L){
  155.     int n;
  156.     cout << endl;
  157.     cout << "Какую ячейку выведем?:) Номер: ";
  158.     cin >> n;
  159.     while(n < 0 || n > L->count){
  160.         cout << "Ошибка. Введите заново: ";
  161.         cin >> n;
  162.     }
  163.     s_t = clock();
  164.     Node *temp = L->head;
  165.     for(int i = 0; i < n; i++){
  166.         temp = temp->next;
  167.     }
  168.     cout << "Значение ячейки " << n << " равно " << temp->number << endl;
  169. }
  170.  
  171. //Вставка
  172. void mVstavka(int *A, int n) //x-номер, р- значение
  173. {  
  174.     int x,*B;
  175.     cout<<endl;
  176.     cout<<"ВСТАВКА"<<endl;
  177.     for(int i=0; i<n; i++)
  178.     { cout<<A[i]<<" ";
  179.     }
  180.     cout<<"\nВведите номер для новой ячейки: ";
  181.     cin>> x;
  182.     while(x<0 || x>n)
  183.     {cout<<"\nТакой ячейки нет. Введите номер ячейки заново: ";
  184.     cin>> x;
  185.     }
  186.     s_t = clock();
  187.     B=new int [n+1];
  188.     int m=0;
  189.     for(int i=0; i<n+1; i++)
  190.     {
  191.         if (i==x){
  192.         B[i]=rand()%10;
  193.         continue;
  194.         }
  195.         B[i]=A[m];
  196.         m++;
  197.     }
  198.     delete[]A;
  199.     A=new int [n+1];
  200.     for(int i=0; i<n+1; i++)
  201.     {
  202.     A[i]=B[i];
  203.     cout<<A[i]<<" ";
  204.     }
  205.     o++;   
  206. };
  207.  
  208. //Удаление
  209. void mDelete (int *A, int n)
  210. {
  211.     int x, *B;
  212.     cout<<endl<<endl;
  213.     cout<<"УДАЛЕНИЕ"<<endl;
  214.     for(int i=0; i<n; i++)
  215.     { cout<<A[i]<<" ";
  216.     }
  217.     cout<<"\nВведите номер ячейки, которую хотите удалить: ";
  218.     cin>> x;
  219.     while(x<0 || x>=n)
  220.     {cout<<"\nТакой ячейки нет. Введите номер ячейки заново: ";
  221.     cin>> x;
  222.     }
  223.     s_t = clock();
  224.     B=new int[n-1];
  225.     int c = 0;
  226.     for (int i=0; i<n; i++)
  227.     {
  228.         if(i == x) continue;
  229.         B[c] = A[i];
  230.         c++;
  231.     }
  232.     delete[]A;
  233.     A = new int[n-1];
  234.     for (int i=0; i<n-1; i++)
  235.     {
  236.         A[i] = B[i];
  237.         cout << A[i] << " ";
  238.     }
  239.     delete[]B;
  240.     o--;
  241. }
  242.  
  243. //Получение значения
  244. void mNumber(int *A, int n)
  245. {
  246.     int x;
  247.     cout<<endl<<endl;
  248.     cout<<"ПОЛУЧЕНИЕ ЗНАЧЕНИЯ"<<endl;
  249.     cout<<"Введите номер ячейки: ";
  250.     cin>>x;
  251.     while(x<0 || x>n){
  252.     cout<<"Неправильный номер ячейки. Введите новый: ";
  253.     cin>>x;
  254.     }
  255.     s_t = clock();
  256.     cout<<"Значение ячейки "<< x<<" Равно "<< A[x]<< endl;
  257. };
  258.  
  259.  
  260. void main()
  261. {
  262.     setlocale(LC_ALL, "Russian");
  263.     int t, *A;
  264.     bool m = 1;
  265.     List *L = new List;
  266.     int type = 0; // 1 - массив, 2 - список
  267.     cout << "Привет с:\n";
  268.     do{
  269.         do{
  270.             cout << "\nВыберите структуру данных:\n1. Динамический массив\n2. Двусвязный список\n0. Выход\n--> ";
  271.             cin >> t;
  272.         } while(t<0 || t > 2);
  273.         if(t == 0){
  274.             m = 0;
  275.             return;
  276.         }
  277.         type = t;
  278.         if(type == 1){
  279.             cout<<"Введите длину динамического массива: ";
  280.             cin>>o;
  281.         }
  282.         A=new int[o];
  283.         if(type == 1){
  284.             for(int i=0; i<o; i++)
  285.             {
  286.                 A[i]= rand()%10;
  287.             }
  288.         } else if(type == 2){
  289.             L = new List;
  290.             createList(L);
  291.         }
  292.         do{
  293.             cout << "\nВыберите операцию:\n1. Вставка\n2. Удаление\n3. Получение элемента\n0. Выход\n--> ";
  294.             cin >> t;
  295.         } while(t<0 || t > 3);
  296.         if(t == 0) m = 0;
  297.         switch(t){
  298.             case 1:{
  299.                 if(type == 1) mVstavka(A, o), e_t = clock();
  300.                 else if(type == 2) insertNode(L), e_t = clock();
  301.                 break;
  302.             }
  303.              case 2:{
  304.                 if(type == 1)mDelete(A, o), e_t = clock();
  305.                 else if(type == 2) deleteNode(L), e_t = clock();
  306.                 break;
  307.             }
  308.             case 3:{
  309.                 if(type == 1) mNumber(A, o), e_t = clock();
  310.                 else if(type == 2) findNode(L), e_t = clock();
  311.                 break;
  312.             }
  313.         }
  314.  
  315.         cout << endl << (e_t-s_t) << "мс." << endl;
  316.     }while(m);
  317.     system("pause");
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement