Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- #include <climits>
- using namespace std;
- struct Deq {
- int beg = 65536;
- int end = 65536;
- int array[131072] = {};
- void init() {
- for (unsigned i = 0; i < 131072; ++i) {
- array[i] = INT_MIN;
- }
- }
- void PushBack(int n) {
- array[++end] = n;
- cout << "ok" << endl;
- }
- void PushFront(int n) {
- array[--beg] = n;
- cout << "ok" << endl;
- }
- void PopFront() {
- if (end - beg <= 0) {
- cout << "error" << endl;
- } else {
- cout << array[beg] << endl;
- beg++;
- }
- }
- void PopBack() {
- if (end - beg <= 0) {
- cout << "error" << endl;
- } else {
- cout << array[end] << endl;
- end--;
- }
- }
- void Front() {
- if ((end - beg <= 0) || (array[beg] == INT_MIN)) {
- cout << "error" << endl;
- } else {
- cout << array[beg] << endl;
- }
- }
- void Back() {
- if ((end - beg <= 0) || (array[end] == INT_MIN)) {
- cout << "error" << endl;
- } else {
- cout << array[end] << endl;
- }
- }
- void Size() {
- cout << end - beg << endl;
- }
- void Clear() {
- beg = 65536;
- end = 65536;
- for (unsigned i = 0; i < 131072; ++i) {
- array[i] = INT_MIN;
- }
- cout << "ok" << endl;
- }
- };
- int main() {
- int K;
- cin >> K;
- Deq A;
- A.init();
- int n;
- char b[11] = "push_front";
- char c[10] = "push_back";
- char d[10] = "pop_front";
- char t[9] = "pop_back";
- char x[6] = "front";
- char y[5] = "back";
- char u[5] = "size";
- char e[6] = "clear";
- char r[5] = "exit";
- for (int i = 0; i < K; i++) {
- char *a = new char[15];
- cin >> a;
- if (strcmp(a, b) == 0) {
- cin >> n;
- A.PushFront(n);
- }
- if (strcmp(a, c) == 0) {
- cin >> n;
- A.PushBack(n);
- }
- if (strcmp(a, d) == 0) {
- A.PopFront();
- }
- if (strcmp(a, t) == 0) {
- A.PopBack();
- }
- if (strcmp(a, x) == 0) {
- A.Front();
- }
- if (strcmp(a, y) == 0) {
- A.Back();
- }
- if (strcmp(a, u) == 0) {
- A.Size();
- }
- if (strcmp(a, e) == 0) {
- A.Clear();
- }
- if (strcmp(a, r) == 0) {
- cout << "bye";
- delete[] a;
- break;
- }
- delete[] a;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment