Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- Node *prev;
- };
- struct List {
- int size;
- Node *head;
- Node *tail;
- Node *mid;
- void push(int n);
- void push_mid(int n);
- int pop();
- List();
- };
- List::List() {
- this->size = 0;
- this->head = 0;
- this->tail = 0;
- this->mid = 0;
- }
- void List::push(int n) {
- if (this->size == 0) {
- Node *temp = new Node;
- temp->data = n;
- temp->prev = 0;
- this->head = temp;
- this->tail = this->head;
- this->mid = this->head;
- } else {
- Node *temp = new Node;
- temp->data = n;
- temp->prev = 0;
- this->tail->prev = temp;
- this->tail = temp;
- if (this->size % 2 == 0) {
- this->mid = this->mid->prev;
- }
- }
- this->size++;
- }
- int List::pop() {
- int n = this->head->data;
- this->head = this->head->prev;
- this->size--;
- if (this->size % 2 == 1) {
- mid = mid->prev;
- }
- return n;
- }
- void List::push_mid(int n) {
- Node *temp = new Node;
- temp->data = n;
- this->size++;
- if (this->size == 1) {
- temp->prev = 0;
- this->head = temp;
- this->tail = this->head;
- this->mid = this->head;
- } else if (this->size == 2) {
- temp->prev = 0;
- this->head->prev = temp;
- this->tail = temp;
- } else if (this->size != 1 && this->size != 2) {
- temp->prev = this->mid->prev;
- this->mid->prev = temp;
- if (this->size % 2 == 1) {
- mid = mid->prev;
- }
- }
- }
- int main() {
- int n;
- cin >> n;
- List *Goblins = new List;
- for (int i = 0; i < n; ++i) {
- char com;
- cin >> com;
- if (com == '+') {
- int k;
- cin >> k;
- Goblins->push(k);
- } else if (com == '*') {
- int k;
- cin >> k;
- Goblins->push_mid(k);
- } else if (com == '-') {
- if (Goblins->size > 0) {
- cout << Goblins->pop() << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement