Advertisement
juxtapositions

Untitled

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