fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7. int x;
  8. Node *next;
  9. };
  10.  
  11. void sort(Node *head)
  12. {
  13. Node *p = head;
  14. bool modified = false;
  15. while (1)
  16. {
  17. if (p->next == NULL) { if (!modified) { return; } p = head; modified = false; }
  18.  
  19. if (p->x < p->next->x) { swap(p->x, p->next->x); modified = true; }
  20.  
  21. p = p->next;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. Node *head = new Node;
  28. head->x = 5;
  29. head->next = new Node;
  30. head->next->x = 10;
  31. head->next->next = new Node;
  32. head->next->next->x = 2;
  33. head->next->next->next = new Node;
  34. head->next->next->next->x = 13;
  35. head->next->next->next->next = NULL;
  36. sort(head);
  37. for (Node *p = head; p != NULL; p = p->next) { cout << p->x << "\n"; }
  38. }
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
13
10
5
2