D_L3

Обръщане на списък

Jan 25th, 2024
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int data;
  6.     Node* next;
  7. };
  8.  
  9. struct List {
  10.     Node* head = nullptr;
  11.     Node* tail = nullptr;
  12.  
  13.     void add(int value) {
  14.         Node* temp = new Node;
  15.         temp->data = value;
  16.         temp->next = nullptr;
  17.  
  18.         if (head == nullptr and tail == nullptr) {
  19.             head = tail = temp;
  20.         }
  21.         else {
  22.             tail->next = temp;
  23.             tail = temp;
  24.         }
  25.     }
  26.  
  27.     //
  28.     int getCount() {
  29.         int count = 0;
  30.         Node* current = head;
  31.  
  32.         while (current) {
  33.             count++;
  34.             current = current->next;
  35.         }
  36.  
  37.         return count;
  38.     }
  39.  
  40.     void reverse(int from, int to) {
  41.         int count = getCount();
  42.  
  43.         to = min(to, count);
  44.  
  45.         if (from >= count || to == from || !head) {
  46.             return;
  47.         }
  48.  
  49.         Node* before = nullptr;
  50.         Node* after = nullptr;
  51.  
  52.         if (from != 1) {
  53.             before = head;
  54.             for (int i = 2; i < from; i++)
  55.                 before = before->next;
  56.         }
  57.  
  58.         if (to <= count) {
  59.             after = head;
  60.             for (int i = 0; i < to; i++)
  61.                 after = after->next;
  62.         }
  63.         Node* start = before ? before->next : head;
  64.  
  65.         Node* current = start;
  66.         Node* next = current->next;
  67.  
  68.         while (next != after) {
  69.             Node* newNext = next->next;
  70.             next->next = current;
  71.             current = next;
  72.             next = newNext;
  73.         }
  74.         if (before)
  75.             before->next = current;
  76.         else
  77.             head = current;
  78.         if (after)
  79.             start->next = after;
  80.         else {
  81.             tail = start;
  82.             start->next = nullptr;
  83.         }
  84.     }
  85.  
  86.     void print() {
  87.         Node* temp = head;
  88.  
  89.         while (temp) {
  90.             cout << temp->data << " ";
  91.             temp = temp->next;
  92.         }
  93.  
  94.         cout << endl;
  95.     }
  96.  
  97.     ~List() {
  98.         Node* curr = head;
  99.  
  100.         while (curr) {
  101.             Node* prev = new Node;
  102.             prev = curr;
  103.             curr = curr->next;
  104.             delete prev;
  105.         }
  106.  
  107.         delete curr;
  108.     }
  109. };
  110.  
  111. int main() {
  112.     ios_base::sync_with_stdio(false);
  113.     cin.tie(nullptr);
  114.  
  115.     List testList;
  116.  
  117.     int queries;
  118.     cin >> queries;
  119.  
  120.     for (int t = 0; t < queries; t++) {
  121.  
  122.         string op;
  123.         cin >> op;
  124.  
  125.         if (op == "add") {
  126.             int num;
  127.             cin >> num;
  128.             testList.add(num);
  129.         }
  130.  
  131.         if (op == "print") {
  132.             testList.print();
  133.         }
  134.  
  135.         if (op == "reverse") {
  136.             int from, to;
  137.             cin >> from >> to;
  138.             testList.reverse(from, to);
  139.         }
  140.     }
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment