Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5. int data;
  6. Node *prev;
  7. };
  8.  
  9. struct List {
  10. int size;
  11. Node *head;
  12. Node *tail;
  13. Node *mid;
  14. void push(int n);
  15. void push_mid(int n);
  16. int pop();
  17. List();
  18. };
  19.  
  20. List::List() {
  21. this->size = 0;
  22. this->head = 0;
  23. this->tail = 0;
  24. this->mid = 0;
  25. }
  26.  
  27. void List::push(int n) {
  28. if (this->size == 0) {
  29. Node *temp = new Node;
  30. temp->data = n;
  31. temp->prev = 0;
  32. this->head = temp;
  33. this->tail = this->head;
  34. this->mid = this->head;
  35. } else {
  36. Node *temp = new Node;
  37. temp->data = n;
  38. temp->prev = 0;
  39. this->tail->prev = temp;
  40. this->tail = temp;
  41. if (this->size % 2 == 0) {
  42. this->mid = this->mid->prev;
  43. }
  44. }
  45. this->size++;
  46. }
  47.  
  48. int List::pop() {
  49. int n = this->head->data;
  50. this->head = this->head->prev;
  51. this->size--;
  52. if (this->size % 2 == 1) {
  53. mid = mid->prev;
  54. }
  55. return n;
  56. }
  57.  
  58. void List::push_mid(int n) {
  59. Node *temp = new Node;
  60. temp->data = n;
  61. this->size++;
  62. if (this->size == 1) {
  63. temp->prev = 0;
  64. this->head = temp;
  65. this->tail = this->head;
  66. this->mid = this->head;
  67. } else if (this->size == 2) {
  68. temp->prev = 0;
  69. this->head->prev = temp;
  70. this->tail = temp;
  71. } else if (this->size != 1 && this->size != 2) {
  72. temp->prev = this->mid->prev;
  73. this->mid->prev = temp;
  74. if (this->size % 2 == 1) {
  75. mid = mid->prev;
  76. }
  77. }
  78. }
  79.  
  80. int main() {
  81. int n;
  82. cin >> n;
  83. List *Goblins = new List;
  84. for (int i = 0; i < n; ++i) {
  85. char com;
  86. cin >> com;
  87. if (com == '+') {
  88. int k;
  89. cin >> k;
  90. Goblins->push(k);
  91. } else if (com == '*') {
  92. int k;
  93. cin >> k;
  94. Goblins->push_mid(k);
  95. } else if (com == '-') {
  96. if (Goblins->size > 0) {
  97. cout << Goblins->pop() << endl;
  98. }
  99. }
  100. }
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement