fork download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. class LinkedList {
  5. private:
  6. struct node {
  7. int value;
  8. node* next;
  9. };
  10. node *first;
  11. node *p;
  12. node *q;
  13. public:
  14. LinkedList();
  15. void add(int value);
  16. void print();
  17. void query(int position);
  18. void insert(int position, int value);
  19. void remove(int position);
  20. void reverse(int i, int j);
  21. int getValue(int position);
  22. };
  23. LinkedList::LinkedList() {
  24. first = NULL; p = NULL; q = NULL;
  25. }
  26. void LinkedList::add(int value) {
  27. node* n = new node;
  28. n->next = first;
  29. n->value = value;
  30. if (first != NULL) {
  31. p = first;
  32. while (p->next != NULL && p->next != first) p = p->next;
  33. p->next = n;
  34. }
  35. else first = n;
  36. }
  37. void LinkedList::print() {
  38. p = first;
  39. (p->value < 10) ? cout << "0" << p->value << " " : cout << p->value << " ";
  40. p = p->next;
  41. while (p != first) {
  42. (p->value < 10) ? cout << "0" << p->value << " " : cout << p->value << " ";
  43. p = p->next;
  44. }
  45. cout << endl;
  46. }
  47. void LinkedList::query(int position) {
  48. p = first;
  49. for (int i = 0; i < position - 1; i++) p = p->next;
  50. cout << p->value << endl;
  51. }
  52. int LinkedList::getValue(int position) {
  53. p = first;
  54. for (int i = 0; i < position - 1; i++) p = p->next;
  55. return p->value;
  56. }
  57. void LinkedList::insert(int position, int value) {
  58. node* n = new node;
  59. n->value = value;
  60. p = first;
  61. for (int i = 0; i < position - 1; i++) p = p->next;
  62. n->next = p->next;
  63. p->next = n;
  64. }
  65. void LinkedList::remove(int position) {
  66. node * n = NULL;
  67. p = first;
  68. if (position == 1) first = first->next;
  69. else {
  70. for (int i = 0; i < position - 1; i++)
  71. {
  72. q = p;
  73. p = p->next;
  74. }
  75. n = p;
  76. p = p->next;
  77. q->next = p;
  78. delete n;
  79. }
  80. }
  81. void LinkedList::reverse(int i, int j) {
  82. stack <int> temp;
  83. for (int k = i; k < j; k++) {
  84. temp.push(getValue(i));
  85. remove(i);
  86. }
  87. for (int k = i; k < j; k++) {
  88. insert(k, temp.top());
  89. temp.pop();
  90. }
  91. }
  92. int main() {
  93. LinkedList list;
  94. int N;
  95. cin >> N;
  96. for (int size = 0; size < N; size++)
  97. {
  98. int value;
  99. cin >> value;
  100. list.add(value);
  101. }
  102. int Q, ref = 1;
  103. cin >> Q;
  104. for (int time = 0; time < Q; time++) {
  105. int command, position, value;
  106. cin >> command >> position;
  107. ref += position - 1;
  108. switch (command)
  109. {
  110. case 1:
  111. cin >> value;
  112. list.insert(ref++, value);
  113. N++;
  114. break;
  115. case 2:
  116. list.remove(ref--);
  117. N--;
  118. break;
  119. case 3:
  120. list.query(ref);
  121. break;
  122. case -1:
  123. return 0;
  124. default:
  125. break;
  126. }
  127. ref %= N; if (ref == 0)ref = N;
  128. //list.print();
  129. //for (int i = 1; i < ref%N; i++) cout << " "; cout << ref << " size " << N << endl;
  130. }
  131. return 0;
  132. }
Success #stdin #stdout 0s 15248KB
stdin
6
1 5 3 3 2 10
4
1 3 20
3 3
2 5
3 6
stdout
2
5