fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int data;
  8. node *next;
  9. };
  10.  
  11. class linked_list
  12. {
  13. private:
  14. node *head,*tail;
  15. public:
  16. linked_list()
  17. {
  18. head = NULL;
  19. tail = NULL;
  20. }
  21.  
  22. void add_node(int n)
  23. {
  24. node *tmp = new node;
  25. tmp->data = n;
  26. tmp->next = NULL;
  27.  
  28. if(head == NULL)
  29. {
  30. head = tmp;
  31. tail = tmp;
  32. }
  33. else
  34. {
  35. tail->next = tmp;
  36. tail = tail->next;
  37. }
  38. }
  39.  
  40. node* getHead()
  41. {
  42. return head;
  43. }
  44. };
  45.  
  46. int main()
  47. {
  48. linked_list a;
  49. a.add_node(1);
  50. a.add_node(2);
  51. a.add_node(3);
  52. a.add_node(4);
  53. a.add_node(5);
  54. bool leave= false;
  55. int direction = 1;
  56. node* considered_node = new node(*(a.getHead()));
  57. while(true)
  58. {
  59. if (considered_node->next == NULL)
  60. {
  61. break;
  62. }
  63.  
  64. if (direction*considered_node->next->data < direction*considered_node->data)
  65. {
  66. int tmp = considered_node->next->data;
  67. considered_node->next->data = considered_node->data;
  68. considered_node->data = tmp;
  69. }
  70. considered_node = considered_node->next;
  71. direction = -1*direction;
  72. }
  73.  
  74. considered_node = considered_node->next;
  75. delete considered_node;
  76.  
  77. node* cur = a.getHead();
  78. while(cur != NULL) {
  79. cout << cur->data << " ";
  80. cur = cur->next;
  81. }
  82.  
  83. return 0;
  84. }
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
1 3 2 5 4