#include <iostream>
using namespace std;
struct tochd {
int inf;
tochd *left, *right;
};
void NewOchd(tochd **sl, tochd **sr) {
*sl = new tochd; *sr = new tochd;
(*sl)->left = NULL; (*sl)->right = *sr;
(*sr)->left = *sl; (*sr)->right = NULL;
}
void AddOchdLeft(tochd *sp, int inf) {
tochd *spt = new tochd;
spt->inf = inf;
spt->left = sp->left; spt->right = sp;
spt->left->right = spt; sp->left = spt;
}
int ReadOchdD(tochd *sp) {
int inf = sp->inf;
sp->left->right = sp->right;
sp->right->left = sp->left;
delete sp;
return inf;
}
void DelOchdAll(tochd **sl, tochd **sr) {
tochd *spt = (*sl)->right;
while (spt != *sr) {
ReadOchdD(spt);
spt = (*sl)->right;
}
delete *sl; *sl = NULL;
delete *sr; *sr = NULL;
}
void printOchd(tochd *sl, tochd *sr) {
tochd *p = sl->right;
while (p != sr) {
cout << p->inf << " ";
p = p->right;
}
cout << endl;
}
// Вспомогательная: вставка в конец (перед sr)
void addToEnd(tochd *sr, int val) {
AddOchdLeft(sr, val);
}
// Поиск указателя на максимальный элемент
tochd* findMax(tochd *sl, tochd *sr) {
tochd *p = sl->right;
if (p == sr) return nullptr;
tochd *maxPtr = p;
int maxVal = p->inf;
p = p->right;
while (p != sr) {
if (p->inf > maxVal) {
maxVal = p->inf;
maxPtr = p;
}
p = p->right;
}
return maxPtr;
}
int main() {
setlocale(LC_ALL, "Russian");
tochd *sl, *sr;
NewOchd(&sl, &sr);
int n;
cout << "Введите количество элементов: ";
cin >> n;
cout << "Введите элементы: ";
for (int i = 0; i < n; ++i) {
int val;
cin >> val;
addToEnd(sr, val);
}
cout << "Исходный список: ";
printOchd(sl, sr);
tochd *maxPtr = findMax(sl, sr);
if (!maxPtr) {
cout << "Список пуст.\n";
DelOchdAll(&sl, &sr);
return 0;
}
cout << "Максимальный элемент: " << maxPtr->inf << endl;
// Создаём вторую очередь
tochd *sl2, *sr2;
NewOchd(&sl2, &sr2);
// Переносим все элементы справа от максимального
tochd *cur = maxPtr->right;
while (cur != sr) {
int val = cur->inf;
tochd *next = cur->right;
ReadOchdD(cur); // удалить из первого списка
AddOchdLeft(sr2, val); // добавить в конец второго
cur = next;
}
cout << "Первый список после перемещения: ";
printOchd(sl, sr);
cout << "Второй список (элементы после максимального): ";
printOchd(sl2, sr2);
DelOchdAll(&sl, &sr);
DelOchdAll(&sl2, &sr2);
return 0;
}