Advertisement
Kwwiker

СиАОД 2 Вариант 6

Mar 16th, 2021
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     double info;
  8.     Node* next;
  9. };
  10.  
  11. void printList(Node* head) {
  12.     Node* current = head;
  13.     while (current != NULL) {
  14.         cout << current->info << " ";
  15.         current = current->next;
  16.     }
  17.     cout << endl;
  18. }
  19.  
  20. void createList(Node*& head, int n) {
  21.     Node* newNode, * temp = NULL;
  22.     for (int i = 0; i < n; i++) {
  23.         newNode = new Node;
  24.         cout << "Введите значение узла: ";
  25.         cin >> newNode->info;
  26.         newNode->next = NULL;
  27.         if (head == NULL) {
  28.             head = newNode;
  29.             temp = head;
  30.         }
  31.         else {
  32.             temp->next = newNode;
  33.             temp = temp->next;
  34.         }
  35.     }
  36. }
  37.  
  38. Node* getNode(Node* head, int index) {
  39.     Node* elem = head;
  40.     int i = 0;
  41.     while (i < index && elem != NULL) {
  42.         elem = elem->next;
  43.         i++;
  44.     }
  45.     return elem;
  46. }
  47.  
  48. void insert2NodesBeforeLast(Node*& head, double firstValue, double secondValue) {
  49.     Node* first, * second;
  50.     first = new Node;
  51.     second = new Node;
  52.     first->next = second;
  53.     second->next = NULL;
  54.     Node* elem = head;
  55.     if (head == NULL) {
  56.         first->info = firstValue;
  57.         second->info = secondValue;
  58.         head = first;
  59.     }
  60.     else {
  61.         while (elem->next != NULL) {
  62.             elem = elem->next;
  63.         }
  64.         elem->next = first;
  65.         double temp = elem->info;
  66.         elem->info = firstValue;
  67.         first->info = secondValue;
  68.         second->info = temp;
  69.     }
  70. }
  71.  
  72. void delFirstNegative(Node*& head) {
  73.     Node* elem = head;
  74.     int i = 0;
  75.     int indexOfNegative;
  76.     while (elem != NULL) {
  77.         if (elem->info < 0) {
  78.             if (elem == head) {
  79.                 head = head->next;
  80.                 return;
  81.             }
  82.             indexOfNegative = i;
  83.             getNode(head, indexOfNegative - 1)->next = getNode(head, indexOfNegative + 1);
  84.             return;
  85.         }
  86.         elem = elem->next;
  87.         i++;
  88.     }
  89. }
  90.  
  91. void moveMaxToEnd(Node*& head) {
  92.     Node* elem = head;
  93.     Node* max = head;
  94.     int i = 0;
  95.     int indexOfMax = 0;
  96.     while (elem->next != NULL) {
  97.         if (elem->info > max->info) {
  98.             max = elem;
  99.             indexOfMax = i;
  100.         }
  101.         elem = elem->next;
  102.         i++;
  103.     }
  104.     if (elem->info > max->info) {
  105.         max = elem;
  106.         indexOfMax = i;
  107.     }
  108.     if (max->next != NULL) {
  109.         if (max == head) {
  110.             head = head->next;
  111.         }
  112.         elem->next = max;
  113.         getNode(head, indexOfMax - 1)->next = getNode(head, indexOfMax + 1);
  114.         max->next = NULL;
  115.     }
  116. }
  117.  
  118. int main() {
  119.     SetConsoleCP(1251);
  120.     SetConsoleOutputCP(1251);
  121.     Node* head = NULL;
  122.     int size;
  123.     cout << "Введите длину списка для создания: ";
  124.     cin >> size;
  125.     createList(head, size);
  126.     cout << "Получившийся список:" << endl;
  127.     printList(head);
  128.     double firstValue, secondValue;
  129.     cout << "Введите 2 значения, которые нужно вставить перед последним элементом: ";
  130.     cin >> firstValue >> secondValue;
  131.     insert2NodesBeforeLast(head, firstValue, secondValue);
  132.     cout << "Список с добавленными элементами:" << endl;
  133.     printList(head);
  134.     delFirstNegative(head);
  135.     cout << "Список после удаления первого отрицательного элемента:" << endl;
  136.     printList(head);
  137.     moveMaxToEnd(head);
  138.     cout << "Список после перемещения максимального элемента в конец:" << endl;
  139.     printList(head);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement