fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. class Node {
  8. public:
  9. int value;
  10. Node* next;
  11.  
  12. Node(int a) {
  13. value = a;
  14. next = nullptr;
  15. }
  16. };
  17.  
  18.  
  19. class ForwardList {
  20. public:
  21. Node* head;
  22. Node* tail; // последний элемент
  23.  
  24. ForwardList() {
  25. head = nullptr;
  26. tail = nullptr;
  27. }
  28.  
  29. ~ForwardList() {
  30. while (head != nullptr) pop_front();
  31. }
  32.  
  33. void pop_front() {
  34. if (head == NULL) return;
  35.  
  36. if (head == tail) {
  37. delete head;
  38. head = tail = nullptr;
  39.  
  40. return;
  41. }
  42.  
  43. Node* for_delete = head;
  44. head = for_delete->next;
  45. delete for_delete;
  46. }
  47.  
  48. Node* get_at(int x) {
  49. if (x < 0) {return nullptr;}
  50.  
  51. int n = 0;
  52. Node* pointer = head;
  53.  
  54. while (n < x && pointer->next)
  55. {
  56. pointer = pointer->next;
  57. n++;
  58. }
  59.  
  60. return (n == x) ? pointer : nullptr;
  61. }
  62.  
  63. void push_at(int x, int y) {
  64. Node* previous = get_at(x);
  65. Node* current = get_at(x + 1);
  66.  
  67. Node* new_node = new Node(y);
  68.  
  69. if (previous) {
  70. previous->next = new_node;
  71.  
  72. if (current) {new_node->next = current;}
  73. }
  74.  
  75.  
  76. if (head == nullptr) {head = tail = new_node;}
  77. if (tail == nullptr) {tail = new_node;}
  78. }
  79.  
  80. void pop_at(int x) {
  81. Node* previous = get_at(x - 1);
  82. Node* current = get_at(x);
  83.  
  84. if (previous) {previous->next = current->next;}
  85.  
  86. if (current) {
  87. if (head == current) {head == nullptr;}
  88. if (tail == current) {tail == nullptr;}
  89. delete current;
  90.  
  91. }
  92. }
  93.  
  94.  
  95. };
  96.  
  97.  
  98. void execute_query(ForwardList& list, int type, int x, int y) {
  99. if (type == 1) {
  100. list.push_at(x, y);
  101. } else if (type == 2) {
  102. cout << list.get_at(x) << "\n";
  103. } else {
  104. list.pop_at(x);
  105. }
  106. }
  107.  
  108. int main() {
  109. cin.tie(0);
  110. ios::sync_with_stdio(false);
  111.  
  112. freopen("input.txt", "r", stdin);
  113. freopen("output.txt", "w", stdout);
  114.  
  115. ForwardList FL;
  116.  
  117. int q;
  118. cin >> q;
  119.  
  120.  
  121. for (int i = 0; i < q; i++)
  122. {
  123. string query;
  124. getline(cin, query);
  125.  
  126.  
  127. int type = (int) (query[0] - '0');
  128. execute_query(
  129. FL,
  130. type,
  131. (int) (query[2] - '0'),
  132. (type == 1) ? ((int) (query[4] - '0')) : (0)
  133. );
  134. }
  135.  
  136. return 0;
  137. }
Success #stdin #stdout 0.01s 5316KB
stdin
6
1 0 1
1 1 2
1 2 3
2 0
2 1
2 2
stdout
Standard output is empty