Advertisement
juxtapositions

Untitled

Mar 18th, 2023
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node {
  4.     int data;
  5.     Node* next;
  6.     Node(int d) : data(d), next(nullptr) {}
  7. };
  8.  
  9. void appendList(Node*& head, Node* list) {
  10.     if (head == nullptr) {
  11.         head = list;
  12.     }
  13.     else {
  14.         Node* curr = head;
  15.         while (curr->next != nullptr) {
  16.             curr = curr->next;
  17.         }
  18.         curr->next = list;
  19.     }
  20. }
  21.  
  22.  
  23. void printList(Node* head) {
  24.     Node* curr = head;
  25.     while (curr != nullptr) {
  26.         std::cout << curr->data << " ";
  27.         curr = curr->next;
  28.     }
  29.     std::cout << std::endl;
  30. }
  31.  
  32. void moveNodesToEnd(Node** head_ref, int n) {
  33.     if (*head_ref == nullptr || n == 0) {
  34.         return;
  35.     }
  36.  
  37.     Node* current = *head_ref;
  38.  
  39.     // Find the nth node
  40.     for (int i = 1; i < n && current != nullptr; i++) {
  41.         current = current->next;
  42.     }
  43.  
  44.     // If nth node is not present, return
  45.     if (current == nullptr) {
  46.         return;
  47.     }
  48.  
  49.     // Save the nth node
  50.     Node* nthNode = current;
  51.  
  52.     // Find the last node of the list
  53.     while (current->next != nullptr) {
  54.         current = current->next;
  55.     }
  56.  
  57.     // Connect the last node to the head node
  58.     current->next = *head_ref;
  59.  
  60.     // Change head to (n+1)th node
  61.     *head_ref = nthNode->next;
  62.  
  63.     // Connect the last node to nullptr
  64.     nthNode->next = nullptr;
  65.  
  66.  
  67. }
  68.  
  69. int countNodesBefore(Node* head, int nodeNum) {
  70.     int count = 0;
  71.     Node* current = head;
  72.  
  73.     // Проходим по всем узлам списка
  74.     while (current != nullptr) {
  75.         // Если нашли нужный узел, возвращаем количество пройденных узлов
  76.         if (count == nodeNum) {
  77.             return count;
  78.         }
  79.  
  80.         // Иначе продолжаем двигаться по списку и увеличиваем счетчик
  81.         current = current->next;
  82.         count++;
  83.     }
  84.  
  85.     // Если узел с указанным номером не найден, возвращаем -1
  86.     return -1;
  87. }
  88.  
  89.  
  90. void flip_end_elements(Node*& head, int pos) {
  91.     if (!head || !head->next) {
  92.         return;  // Нечего переворачивать
  93.     }
  94.  
  95.     // Находим узел с нужным номером
  96.     Node* pos_node = head;
  97.     for (int i = 1; i < pos && pos_node; i++) {
  98.         pos_node = pos_node->next;
  99.     }
  100.     if (!pos_node) {
  101.         return;  // Неверный номер
  102.     }
  103.  
  104.     // Переворачиваем узлы
  105.     Node* prev = nullptr;
  106.     Node* curr = pos_node->next;
  107.     while (curr) {
  108.         Node* temp = curr->next;
  109.         curr->next = prev;
  110.         prev = curr;
  111.         curr = temp;
  112.     }
  113.  
  114.     // Соединяем перевернутые узлы со всем списком
  115.     pos_node->next = prev;
  116. }
  117.  
  118.  
  119. int main() {
  120.     Node* head = nullptr;
  121.     int n;
  122.     std::cout << "Enter the number of nodes: ";
  123.     std::cin >> n;
  124.     for (int i = 1; i <= n; i++) {
  125.         int value;
  126.         std::cout << "Enter the value of node " << i << ": ";
  127.         std::cin >> value;
  128.         std::cout<<"\n";
  129.         appendList(head, new Node(value));
  130.     }
  131.  
  132.     std::cout << "Original list: ";
  133.     printList(head);
  134.  
  135.     int value;
  136.     std::cout << "Enter the value to start the reformatted list: ";
  137.     std::cin >> value;
  138.  
  139.     int nodes = countNodesBefore(head,value);
  140.  
  141.     moveNodesToEnd(&head, nodes);
  142.  
  143.     std::cout << "Reformatted list: ";
  144.     printList(head);
  145.     std::cout<<"\n";
  146.  
  147.     flip_end_elements(head,value);
  148.     printList(head);
  149.  
  150.     exit(0);
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement