Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- #include <iostream>
- using namespace std;
- struct list {
- int data;
- struct list *next;
- struct list *prev;
- };
- void func(list *&head1, list *&head2) {
- list *current1;
- current1 = head1;
- list *current2;
- bool cb = false;
- current2 = head2;
- do {
- current1 = head1;
- if (current2->data > head1->prev->data)
- {
- list *c = current2->next;
- head1->prev->next = current2;
- current2->prev = head1->prev;
- current2->next = head1;
- head1->prev = current2;
- current2 = c;
- cb = true;
- }
- else
- do {
- if (current1->data == current2->data)
- break;
- if (current2->data < current1->data) {
- list *c = current2->next;
- current1->prev->next = current2;
- current2->prev = current1->prev;
- current2->next = current1;
- current1->prev = current2;
- current2 = c;
- cb = true;
- if (current1 == head1)
- head1 = current1->prev;
- break;
- }
- current1 = current1->next;
- } while (current1 != head1);
- if (!cb)
- current2 = current2->next;
- else cb = false;
- } while (current2 != head2);
- }
- void add(list *¤t)
- {
- current->next = (list *)malloc(sizeof(list));
- if (!current->next) exit(-1);
- cin >> current->next->data;
- current->next->next = NULL;
- current->next->prev = current;
- current = current->next;
- }
- void listprint(list *add)
- {
- struct list *p;
- p = add;
- do {
- printf("%d ", p->data); // вывод значения узла p
- p = p->next; // переход к следующему узлу
- } while (p != add); // условие окончания обхода
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- list *head = NULL;
- list *head1 = NULL;
- list *current = NULL;
- cout << "Введите кол-во элементов в 1 списке:" << endl;
- int n, n1;
- cin >> n;
- cout << "Введите элементы:" << endl;
- for (int i = 0; i < n; i++) {
- if (i == 0) {
- head = (list *)malloc(sizeof(list));
- if (!head) exit(-1);
- head->prev = NULL;
- head->next = NULL;
- cin >> head->data;
- current = head;
- }
- else
- add(current);
- }
- current->next = head;
- head->prev = current;
- cout << "Введите кол-во элементов во 2 списке:" << endl;
- cin >> n1;
- cout << "Введите элементы:" << endl;
- for (int i = 0; i < n1; i++) {
- if (i == 0) {
- head1 = (list *)malloc(sizeof(list));
- if (!head1) exit(-1);
- head1->prev = NULL;
- head1->next = NULL;
- cin >> head1->data;
- current = head1;
- }
- else
- add(current);
- }
- current->next = head1;
- head1->prev = current;
- cout << endl;
- listprint(head);
- cout << endl;
- listprint(head1);
- func(head, head1);
- cout << endl;
- listprint(head);
- return 0;
- }
Add Comment
Please, Sign In to add comment