Advertisement
Guest User

Untitled

a guest
May 27th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6.     int x;
  7.     node* y;
  8. };
  9.  
  10. int main()
  11. {
  12.     node* head = NULL;
  13.     int k = 5;
  14.     head = new node{ rand()%100, NULL };
  15.     head->y = new node{ rand()%100, NULL };
  16.     head->y->y = new node{ rand()%100,NULL };
  17.     head->y->y->y = new node{ rand()%100,NULL };
  18.     head->y->y->y->y = new node{ rand()%100, NULL };
  19.     //head->y = new node{ rand()%100, head->y };
  20.     head->y->y = new node{ -1,head->y->y };
  21.  
  22.     int d = 0;
  23.     for (node* current = head; current != NULL;current = current->y ) {
  24.         d += current->x;
  25.     }
  26.     node* lessElement = head;
  27.  
  28.     for (node *current = head, *prev = head,*future=head->y; current != NULL; current = current->y)
  29.     {
  30.         if (current->x < lessElement->x)
  31.         {
  32.             prev->y = future->y;
  33.             lessElement = current;
  34.         }
  35.     }
  36.  
  37.     for (node* c = head; c != NULL; c = c->y)
  38.     {
  39.         for (node* prev = head, *current = head->y, *next = head->y->y;next->y != NULL; prev = prev->y, current = current->y, next = next->y)
  40.         {
  41.             if (next->x < current->x)
  42.             {
  43.                 prev->y = next;
  44.                 node* y = next->y;
  45.                 next->y = current;
  46.                 current->y = y;
  47.             }
  48.         }
  49.     }
  50.  
  51.     for (node* current = head; current != NULL; current = current->y)
  52.     {
  53.         cout <<"" << current->x << endl;
  54.     }
  55.     std::cout << "The least value is " << lessElement->x << std::endl;
  56.     std::cout << "Value of first node is " << head->x << std::endl;
  57.     std::cout << "Sum of nodes is " << d << std::endl;
  58.  
  59.     return 0;
  60. }
  61. // не сортируется и ничего не выводит на экран
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement