fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef struct node{
  5. int val;
  6. node* next;
  7. node* prev;
  8. };
  9.  
  10. node* make_chain(int key){
  11. node* n = new node;
  12. n->val =key;
  13. n->next = NULL;
  14. n->prev = NULL;
  15. return n;
  16. }
  17.  
  18. class dqueue{
  19. public:
  20. int size;
  21. node* front;
  22. node* back;
  23.  
  24. dqueue(){
  25. size = 0;
  26. front = NULL;
  27. back = NULL;
  28. }
  29. bool isEmpty()
  30. {
  31. return size == 0;
  32. }
  33. void to_front(int key)
  34. {
  35. if(isEmpty())
  36. {
  37. node* n = make_chain(key);
  38. front = n;
  39. back = n;
  40. }
  41. else
  42. {
  43. node* n = make_chain(key);
  44. n->prev = front;
  45. front->next = n;
  46. front = n;
  47. }
  48. size++;
  49. }
  50. void push_back(int key)
  51. {
  52. if(isEmpty())
  53. {
  54. node* n = make_chain(key);
  55. front = n;
  56. back = n;
  57. }
  58. else
  59. {
  60. node* n = make_chain(key);
  61. n->next = back;
  62. back->prev = n;
  63. back = n;
  64. }
  65. size++;
  66. }
  67. void reverse()
  68. {
  69. // if(size>1)
  70. // {
  71. // vector<int>r;
  72. // node* temp = back;
  73. // while(temp)
  74. // {
  75. // r.push_back(temp->val);
  76. // temp = temp->next;
  77. // }
  78. // temp = front;
  79. // int i=0;
  80. // while(temp)
  81. // {
  82. // temp->val = r[i];
  83. // i++;
  84. // temp = temp->prev;
  85. // }
  86. // }
  87. }
  88. void pfront()
  89. {
  90. if(isEmpty())
  91. {
  92. cout<<"No job for Ada?"<<endl;
  93. return;
  94. }
  95. else
  96. {
  97. cout<<front->val<<endl;
  98. if(size>1)
  99. {
  100. node* temp = front;
  101. front = front->prev;
  102. front->next = NULL;
  103. free(temp);
  104. }
  105. else
  106. {
  107. node* temp = front;
  108. front = NULL;
  109. back = NULL;
  110. free(temp);
  111. }
  112. size--;
  113. }
  114. }
  115. void pback()
  116. {
  117. if(size == 0)
  118. {
  119. cout<<"NO job for Ada?"<<endl;
  120. return;
  121. }
  122. else
  123. {
  124. cout<<back->val<<endl;
  125. if(size>1)
  126. {
  127. node* temp = back;
  128. back = back->next;
  129. back->prev = NULL;
  130. //free(back);
  131. }
  132. else
  133. {
  134. node* temp = back;
  135. front = NULL;
  136. back = NULL;
  137. free(temp);
  138. }
  139. size--;
  140. }
  141. }
  142. };
  143.  
  144. int main()
  145. {
  146. int t;
  147. cin>>t;
  148. //vector<int>r;
  149. dqueue d;
  150. bool flag =0;
  151. while(t--)
  152. {
  153. string s;
  154. cin>>s;
  155. if(s[0] == 't')
  156. {
  157. int x;
  158. cin>>x;
  159. //r.insert(r.begin(),x);
  160. if(!flag)
  161. d.to_front(x);
  162. else
  163. d.push_back(x);
  164. }
  165. else if(s[0] == 'f')
  166. {
  167. if(!flag)
  168. d.pfront();
  169. else
  170. d.pback();
  171. }
  172. else if(s[0] == 'b')
  173. {
  174. if(!flag)
  175. d.pback();
  176. else
  177. d.pfront();
  178. }
  179. else if(s[0] == 'p')
  180. {
  181. int x;
  182. cin>>x;
  183. if(!flag)
  184. d.push_back(x);
  185. else
  186. d.to_front(x);
  187. }
  188. else
  189. {
  190. if(flag)
  191. flag = 0;
  192. else
  193. flag =1;
  194. }
  195. }
  196. }
Success #stdin #stdout 0s 4512KB
stdin
15
toFront 93
front
back
reverse
back
reverse
toFront 80
push_back 53
push_back 50
front
front
reverse
push_back 66
reverse
front
stdout
93
NO job for Ada?
No job for Ada?
80
53
66