codegod313

Ssshka_kolechko

May 12th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include "pch.h"
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <locale.h>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. struct list {
  10.  
  11.     int data;
  12.     struct list *next;
  13.     struct list *prev;
  14.  
  15.  
  16. };
  17.  
  18.  
  19. void func(list *&head1, list *&head2) {
  20.     list *current1;
  21.     current1 = head1;
  22.     list *current2;
  23.     bool cb = false;
  24.     current2 = head2;
  25.     do {
  26.         current1 = head1;
  27.         if (current2->data > head1->prev->data)
  28.         {
  29.             list *c = current2->next;
  30.             head1->prev->next = current2;
  31.             current2->prev = head1->prev;
  32.             current2->next = head1;
  33.             head1->prev = current2;
  34.             current2 = c;
  35.             cb = true;
  36.         }
  37.         else
  38.         do {
  39.             if (current1->data == current2->data)
  40.                 break;
  41.             if (current2->data < current1->data) {
  42.                 list *c = current2->next;
  43.                 current1->prev->next = current2;
  44.                 current2->prev = current1->prev;
  45.                 current2->next = current1;
  46.                 current1->prev = current2;
  47.                 current2 = c;
  48.                 cb = true;
  49.                 if (current1 == head1)
  50.                     head1 = current1->prev;
  51.                 break;
  52.             }
  53.             current1 = current1->next;
  54.         } while (current1 != head1);
  55.         if (!cb)
  56.             current2 = current2->next;
  57.         else cb = false;
  58.     } while (current2 != head2);
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65. void add(list *&current)
  66. {
  67.  
  68.     current->next = (list *)malloc(sizeof(list));
  69.     if (!current->next) exit(-1);
  70.     cin >> current->next->data;
  71.     current->next->next = NULL;
  72.     current->next->prev = current;
  73.     current = current->next;
  74.  
  75. }
  76.  
  77. void listprint(list *add)
  78. {
  79.     struct list *p;
  80.     p = add;
  81.     do {
  82.         printf("%d ", p->data); // вывод значения узла p
  83.         p = p->next; // переход к следующему узлу
  84.     } while (p != add); // условие окончания обхода
  85. }
  86.  
  87. int main()
  88. {
  89.     setlocale(LC_ALL, "Russian");
  90.     list *head = NULL;
  91.     list *head1 = NULL;
  92.     list *current = NULL;
  93.     cout << "Введите кол-во элементов в 1 списке:" << endl;
  94.     int n, n1;
  95.     cin >> n;
  96.     cout << "Введите элементы:" << endl;
  97.     for (int i = 0; i < n; i++) {
  98.         if (i == 0) {
  99.             head = (list *)malloc(sizeof(list));
  100.             if (!head) exit(-1);
  101.             head->prev = NULL;
  102.             head->next = NULL;
  103.             cin >> head->data;
  104.             current = head;
  105.         }
  106.         else
  107.             add(current);
  108.  
  109.     }
  110.     current->next = head;
  111.     head->prev = current;
  112.     cout << "Введите кол-во элементов во 2 списке:" << endl;
  113.     cin >> n1;
  114.     cout << "Введите элементы:" << endl;
  115.     for (int i = 0; i < n1; i++) {
  116.         if (i == 0) {
  117.             head1 = (list *)malloc(sizeof(list));
  118.             if (!head1) exit(-1);
  119.             head1->prev = NULL;
  120.             head1->next = NULL;
  121.             cin >> head1->data;
  122.             current = head1;
  123.         }
  124.         else
  125.             add(current);
  126.  
  127.     }
  128.  
  129.     current->next = head1;
  130.     head1->prev = current;
  131.     cout << endl;
  132.     listprint(head);
  133.     cout << endl;
  134.     listprint(head1);
  135.     func(head, head1);
  136.     cout << endl;
  137.     listprint(head);
  138.  
  139.     return 0;
  140.  
  141.  
  142. }
Add Comment
Please, Sign In to add comment