You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

162 lines
3.1 KiB

  1. #include <fstream>
  2. #include <iostream>
  3. template <class T>
  4. class Node {
  5. public:
  6. Node<T>(T item) {
  7. value = item;
  8. next = NULL;
  9. }
  10. T value;
  11. Node<T>* next;
  12. };
  13. template <class T>
  14. class List {
  15. Node<T>* head;
  16. int sz;
  17. public:
  18. List<T>() {
  19. head = NULL;
  20. sz = 0;
  21. }
  22. // Create a list from nodes. O(n)
  23. List<T>(Node<T>* node) {
  24. head = node;
  25. sz = 1;
  26. while (node->next != NULL) sz++;
  27. }
  28. int size() const { return sz; }
  29. Node<T>* begin() const { return head; }
  30. // Return the n'th Node address O(n)
  31. Node<T>* seek(int pos) const {
  32. Node<T>* cur = head;
  33. for (int i = 0; i < pos; ++i) {
  34. cur = cur->next;
  35. }
  36. return cur;
  37. }
  38. // Remove the next node O(1)
  39. void remove(Node<T>* node) {
  40. Node<T>* nextNode = node->next;
  41. node->next = nextNode->next;
  42. delete nextNode;
  43. sz--;
  44. }
  45. // remove the n'th node O(n)
  46. void remove(int nodeNumber) {
  47. if (sz == 0) std::abort();
  48. Node<T>* cur = head;
  49. if (nodeNumber == 0) {
  50. head = head->next;
  51. delete cur;
  52. sz--;
  53. return;
  54. }
  55. for (int i = 0; i < nodeNumber - 1; ++i) {
  56. cur = cur->next;
  57. }
  58. remove(cur);
  59. }
  60. // insert a Node. O(n)
  61. void insert(Node<T>* node, int pos) {
  62. Node<T>* cur = head;
  63. if (pos > sz) std::abort();
  64. if (pos == 0) {
  65. node->next = head;
  66. head = node;
  67. sz++;
  68. return;
  69. }
  70. for (int i = 0; i < pos - 1; ++i) {
  71. cur = cur->next;
  72. }
  73. node->next = cur->next;
  74. cur->next = node;
  75. sz++;
  76. }
  77. // insert a Node. O(n)
  78. void insert(T item, int pos) {
  79. Node<T>* node = new Node<T>(item);
  80. insert(node, pos);
  81. }
  82. // insert a Node. O(n)
  83. void push_back(Node<T>* node) { insert(node, sz); }
  84. // insert an item. O(n)
  85. void push_back(T item) { insert(item, sz); }
  86. // Swap every two adjacent Nodes
  87. void swapAdjacent() {
  88. Node<T>*cur = head, *curNext = head->next, *curPrev = NULL;
  89. for (int i = 0; i < (sz % 2 ? sz - 1 : sz); i += 2) {
  90. if (curPrev == NULL)
  91. head = curNext;
  92. else
  93. curPrev->next = curNext;
  94. cur->next = curNext->next;
  95. curNext->next = cur;
  96. curPrev = cur;
  97. if (cur->next) {
  98. cur = cur->next;
  99. curNext = cur->next;
  100. }
  101. }
  102. }
  103. // convert the list to string. O(n)
  104. std::string toString() {
  105. std::string ans = "";
  106. Node<T>* cur = head;
  107. while (cur) {
  108. ans += std::to_string(cur->value) + " ";
  109. cur = cur->next;
  110. }
  111. return ans;
  112. }
  113. };
  114. int main() {
  115. std::ifstream in;
  116. in.open("input.txt");
  117. std::ofstream out;
  118. out.open("output.txt");
  119. List<int> l;
  120. int a;
  121. while (in >> a) {
  122. l.push_back(a);
  123. }
  124. l.swapAdjacent();
  125. out << l.toString() << '\n';
  126. }