Advertisement
Naimul_X

Untitled

May 31st, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<stack>
  4. using namespace std;
  5. #define MAX 1000;
  6.  
  7. struct stack{
  8.  
  9. int arr[1000];
  10. int top;
  11.  
  12. }A,B,C;
  13. int isFull1()
  14. {
  15. if(A.top==(1000-1))
  16. {
  17. return 1;
  18. }
  19.  
  20. return 0;
  21. }
  22.  
  23. int isEmpty1()
  24. {
  25. if(A.top==-1)
  26. {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31.  
  32.  
  33.  
  34. void push1(int value)
  35. {
  36.  
  37. if(!isFull1())
  38. {
  39. A.top=A.top+1;
  40. A.arr[A.top]=value;
  41. }
  42.  
  43. }
  44.  
  45.  
  46.  
  47. void pop1()
  48. {
  49. if(!isEmpty1())
  50. {
  51. // return st[top];
  52. A.top=A.top-1;
  53. }
  54. }
  55.  
  56. int peek1()
  57. {
  58. if(!isEmpty1())
  59. {
  60. // printf("%d",st[top]);
  61. return A.arr[A.top];
  62. }
  63. return -1;
  64. }
  65.  
  66.  
  67.  
  68. int isFull2()
  69. {
  70. if(B.top==(1000-1))
  71. {
  72. return 1;
  73. }
  74.  
  75. return 0;
  76. }
  77.  
  78. int isEmpty2()
  79. {
  80. if(B.top==-1)
  81. {
  82. return 1;
  83. }
  84. return 0;
  85. }
  86.  
  87.  
  88.  
  89. void push2(int value)
  90. {
  91.  
  92. if(!isFull2())
  93. {
  94. B.top=B.top+1;
  95. B.arr[B.top]=value;
  96. }
  97.  
  98. }
  99.  
  100.  
  101.  
  102. void pop2()
  103. {
  104. if(!isEmpty2())
  105. {
  106. // return st[top];
  107. B.top=B.top-1;
  108. }
  109. }
  110.  
  111.  
  112.  
  113. int peek2()
  114. {
  115. if(!isEmpty2())
  116. {
  117. // printf("%d",st[top]);
  118. return B.arr[B.top];
  119. }
  120. return -1;
  121. }
  122.  
  123. int isFull3()
  124. {
  125. if(C.top==(1000-1))
  126. {
  127. return 1;
  128. }
  129.  
  130. return 0;
  131. }
  132.  
  133. int isEmpty3()
  134. {
  135. if(C.top==-1)
  136. {
  137. return 1;
  138. }
  139. return 0;
  140. }
  141.  
  142.  
  143.  
  144. void push3(int value)
  145. {
  146.  
  147. if(!isFull3())
  148. {
  149. C.top=C.top+1;
  150. C.arr[C.top]=value;
  151. }
  152.  
  153. }
  154.  
  155.  
  156.  
  157. void pop3()
  158. {
  159. if(!isEmpty3())
  160. {
  161. // return st[top];
  162. C.top=C.top-1;
  163. }
  164. }
  165.  
  166. int peek3()
  167. {
  168. if(!isEmpty3())
  169. {
  170. // printf("%d",st[top]);
  171. return C.arr[C.top];
  172. }
  173. return -1;
  174. }
  175. int main()
  176. {
  177. A.top=-1;
  178. B.top=-1;
  179. C.top=-1;
  180. int n,a;
  181. cin>>n;
  182. // stack<int>A,B,C;
  183. for(int i=0;i<n;i++)
  184. {
  185. cin>>a;
  186. push1(a);
  187. }
  188. while(!isEmpty1())
  189. {
  190. int k = peek1();
  191. pop1();
  192. while(!isEmpty2()&& peek2()>k)
  193. {
  194. push1(peek2());
  195. pop2();
  196. }
  197. push2(k);
  198. }
  199. while(!isEmpty2())
  200. {
  201. int z=peek2();
  202. push3(z);
  203. pop2();
  204. }
  205. cout<<"\n";
  206. cout<<"Condition of stack C :";
  207. while(!isEmpty3())
  208. {
  209. cout<<peek3()<<" ";
  210. pop3();
  211.  
  212. }
  213. cout<<"\n";
  214. cout<<"Condition of stack B: Empty"<<"\n";
  215. cout<<"Condition of stack A: Empty";
  216.  
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement