daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct DList {
  7.     DList *next;
  8.     int val;
  9.     DList *prev;
  10.  
  11.     DList *appendL(int val);
  12.     void view();
  13. };
  14.  
  15. DList *DList::appendL(int _val) {
  16.     DList *newList = new DList;
  17.     newList -> next = nullptr;
  18.     newList -> val = _val;
  19.     newList -> prev = this;
  20.     this -> next = newList;
  21.     return newList;
  22. }
  23.  
  24. void DList::view() {
  25.     for (DList *currPtr = this; currPtr != nullptr; currPtr = currPtr -> prev) {
  26.         cout << currPtr -> val;
  27.     }
  28.     cout << endl;
  29. }
  30.  
  31.  
  32.  
  33. int main() {
  34.     // Вводим список
  35.     DList *dl = new DList;
  36.     dl -> prev = nullptr;
  37.     dl -> val = 0;
  38.     dl -> next = nullptr;
  39.     int x = 0;
  40.     while (cin >> x) {
  41.         dl = dl -> appendL(x);
  42.     }
  43.  
  44.     dl -> view();
  45.  
  46.     // Минимум и максимум
  47.     int min = dl -> val, max = dl -> val;
  48.     DList *minPtr = nullptr, *maxPtr = nullptr;
  49.     for (DList *currPtr = dl; currPtr != nullptr; currPtr = currPtr -> prev) {
  50.         if  (currPtr -> val < min) {
  51.             min = currPtr -> val; minPtr = currPtr;
  52.         }
  53.         if  (currPtr -> val > max) {
  54.             max = currPtr -> val; maxPtr = currPtr -> next;
  55.         }
  56.     }
  57.     vector<int> v;
  58.  
  59.     if (dl - maxPtr > dl - minPtr) {
  60.         for (DList *currPtr = maxPtr; currPtr != minPtr; currPtr = currPtr -> next) {
  61.             v.push_back(currPtr -> val);
  62.         }
  63.         for (int e : v) {
  64.             cout << e << endl;
  65.         }
  66.  
  67.         DList *changed_dl_start = new DList;
  68.         changed_dl_start -> next = nullptr;
  69.         changed_dl_start -> val = 0;
  70.         changed_dl_start -> prev = nullptr;
  71.         changed_dl_start = changed_dl_start -> appendL(v[0]);
  72.         DList *changed_dl_end = changed_dl_start;
  73.  
  74.         for (size_t i = 1; i < v.size(); ++i) {
  75.             changed_dl_start = changed_dl_start -> appendL(v[i]);
  76.         }
  77.  
  78.         // Кольцуем
  79.         changed_dl_start -> next = changed_dl_end;
  80.         changed_dl_end -> prev = changed_dl_start;
  81.  
  82.         changed_dl_start -> view();
  83.     } else {
  84.  
  85.         for (DList *currPtr = minPtr -> next; currPtr != maxPtr; currPtr = currPtr -> next) {
  86.             v.push_back(currPtr -> val);
  87.         }
  88.         for (int e : v) {
  89.             cout << e << endl;
  90.         }
  91.  
  92.         DList *changed_dl_start = new DList;
  93.         changed_dl_start -> prev = nullptr;
  94.         changed_dl_start -> val = 0;
  95.         changed_dl_start -> next = nullptr;
  96.         changed_dl_start = changed_dl_start -> appendL(v[0]);
  97.         DList *changed_dl_end = changed_dl_start;
  98.  
  99.         for (size_t i = 1; i < v.size(); ++i) {
  100.             changed_dl_start = changed_dl_start -> appendL(v[i]);
  101.         }
  102.  
  103.         // Кольцуем
  104.         changed_dl_end -> prev = changed_dl_start;
  105.         changed_dl_start -> next = changed_dl_end;
  106.  
  107.         changed_dl_start -> view();
  108.     }
  109.  
  110.  
  111.     return 0;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top