HabKaffee

Untitled

Oct 25th, 2021 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <climits>
  4. using namespace std;
  5. struct Deq {
  6. int beg = 65536;
  7. int end = 65536;
  8. int array[131072] = {};
  9. void init() {
  10. for (unsigned i = 0; i < 131072; ++i) {
  11. array[i] = INT_MIN;
  12. }
  13. }
  14. void PushBack(int n) {
  15. array[++end] = n;
  16. cout << "ok" << endl;
  17. }
  18. void PushFront(int n) {
  19. array[--beg] = n;
  20. cout << "ok" << endl;
  21. }
  22. void PopFront() {
  23. if (end - beg <= 0) {
  24. cout << "error" << endl;
  25. } else {
  26. cout << array[beg] << endl;
  27. beg++;
  28. }
  29. }
  30. void PopBack() {
  31. if (end - beg <= 0) {
  32. cout << "error" << endl;
  33. } else {
  34. cout << array[end] << endl;
  35. end--;
  36. }
  37. }
  38. void Front() {
  39. if ((end - beg <= 0) || (array[beg] == INT_MIN)) {
  40. cout << "error" << endl;
  41. } else {
  42. cout << array[beg] << endl;
  43. }
  44. }
  45. void Back() {
  46. if ((end - beg <= 0) || (array[end] == INT_MIN)) {
  47. cout << "error" << endl;
  48. } else {
  49. cout << array[end] << endl;
  50. }
  51. }
  52. void Size() {
  53. cout << end - beg << endl;
  54. }
  55. void Clear() {
  56. beg = 65536;
  57. end = 65536;
  58. for (unsigned i = 0; i < 131072; ++i) {
  59. array[i] = INT_MIN;
  60. }
  61. cout << "ok" << endl;
  62. }
  63. };
  64. int main() {
  65. int K;
  66. cin >> K;
  67. Deq A;
  68. A.init();
  69. int n;
  70. char b[11] = "push_front";
  71. char c[10] = "push_back";
  72. char d[10] = "pop_front";
  73. char t[9] = "pop_back";
  74. char x[6] = "front";
  75. char y[5] = "back";
  76. char u[5] = "size";
  77. char e[6] = "clear";
  78. char r[5] = "exit";
  79. for (int i = 0; i < K; i++) {
  80. char *a = new char[15];
  81. cin >> a;
  82. if (strcmp(a, b) == 0) {
  83. cin >> n;
  84. A.PushFront(n);
  85. }
  86. if (strcmp(a, c) == 0) {
  87. cin >> n;
  88. A.PushBack(n);
  89. }
  90. if (strcmp(a, d) == 0) {
  91. A.PopFront();
  92. }
  93. if (strcmp(a, t) == 0) {
  94. A.PopBack();
  95. }
  96. if (strcmp(a, x) == 0) {
  97. A.Front();
  98. }
  99. if (strcmp(a, y) == 0) {
  100. A.Back();
  101. }
  102. if (strcmp(a, u) == 0) {
  103. A.Size();
  104. }
  105. if (strcmp(a, e) == 0) {
  106. A.Clear();
  107. }
  108. if (strcmp(a, r) == 0) {
  109. cout << "bye";
  110. delete[] a;
  111. break;
  112. }
  113. delete[] a;
  114. }
  115. return 0;
  116. }
Add Comment
Please, Sign In to add comment