Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. //Preprosesor
  2. #include<iostream>
  3. #include<conio.h>
  4. #include<stdio.h>
  5. #include<windows.h>
  6. using namespace std;
  7.  
  8. //deklarasi stack dengan struct dan array
  9. struct QUEUE_STACK
  10. {
  11. string data_queue[100];
  12. string data_stack[100];
  13. int top_queue;
  14. int top_stack;
  15. string kata_queue[100];
  16. string kata_stack[100];
  17. };
  18.  
  19. //deklarasi variabel antrian dari struct
  20. QUEUE_STACK antrian;
  21. QUEUE_STACK tumpukan;
  22.  
  23. //deklarasi fungsi operasi stack
  24. void inisialisasi_stack();
  25. void inisialisasi_queue();
  26. int IsEmpty_queue();
  27. int IsFull_stack();
  28. int IsEmpty_stack();
  29. int IsFull_queue();
  30. void enqueue(string data_queue);
  31. void dequeue();
  32. void push(string data_stack);
  33. void pop();
  34.  
  35. //fungsi main program
  36. int main()
  37. {
  38. int pilih, i;
  39. string input;
  40.  
  41. inisialisasi_queue();
  42. inisialisasi_stack();
  43. do{
  44. system("cls");
  45. cout<<"|QUEUE"<<endl;
  46. cout<<"|1. Enqueue"<<endl;
  47. cout<<"|2. Dequeue"<<endl;
  48. cout<<"|3. Print Queue"<<endl;
  49. cout<<"|4. Clear Queue"<<endl;
  50. cout<<"|STACK"<<endl;
  51. cout<<"|5. Push"<<endl;
  52. cout<<"|6. pop"<<endl;
  53. cout<<"|7. Print Stack"<<endl;
  54. cout<<"|8. Clear Stack"<<endl;
  55. cout<<"|9. Exit"<<endl;
  56. cout<<endl;
  57. cout<<"Pilih : ";
  58. cin>>pilih;
  59. switch(pilih)
  60. {
  61. case 1:
  62. {
  63. if(IsFull_queue() == 1)
  64. {
  65. cout<<"Antrian penuh !";
  66. }
  67. else
  68. {
  69. cout<<"Data yang akan di Enqueue : ";
  70. cin>>input;
  71. enqueue(input);
  72. }
  73. cout<<endl;
  74. getch();
  75. break;
  76. }
  77. case 2:
  78. {
  79. if(IsEmpty_queue() == 1)
  80. {
  81. cout<<"Antrian kosong !";
  82. }
  83. else
  84. {
  85. cout<<"Data yang akan di Dequeue = "
  86. <<antrian.data_queue[0]<<endl;
  87. dequeue();
  88. }
  89. cout<<endl;
  90. getch();
  91. break;
  92. }
  93. case 3:
  94. {
  95. if(IsEmpty_queue() == 1)
  96. {
  97. cout<<"Antrian kosong !"<<endl;
  98. }
  99. else
  100. {
  101. cout<<"Data : ";
  102. for(i=0;i<=antrian.top_queue;i++)
  103. {
  104. cout<<antrian.data_queue[i]<<" ";
  105. }
  106. cout<<endl;
  107. }
  108. cout<<endl;
  109. getch();
  110. break;
  111. }
  112. case 4:
  113. {
  114. inisialisasi_queue();
  115. cout<<"Antrian sudah kosong !"<<endl;
  116. cout<<endl;
  117. getch();
  118. break;
  119. }
  120. case 5:
  121. {
  122. if(IsFull_stack() == 1)
  123. {
  124. cout<<"Tumpukan penuh !";
  125. }
  126. else
  127. {
  128. cout<<"Data yang akan di push : ";
  129. cin>>input;
  130. push(input);
  131. }
  132. cout<<endl;
  133. getch();
  134. break;
  135. }
  136. case 6:
  137. {
  138. if(IsEmpty_stack() == 1)
  139. {
  140. cout<<"Tumpukan kosong !";
  141. }
  142. else
  143. {
  144. cout<<"Data yang akan di Pop = "
  145. <<tumpukan.data_stack[tumpukan.top_stack]<<endl;
  146. pop();
  147. }
  148. cout<<endl;
  149. getch();
  150. break;
  151. }
  152. case 7:
  153. {
  154. if(IsEmpty_stack() == 1)
  155. {
  156. cout<<"Tumpukan kosong !"<<endl;
  157. }
  158. else
  159. {
  160. cout<<"Data : ";
  161. for(i=0;i<=tumpukan.top_stack;i++)
  162. {
  163. cout<<tumpukan.data_stack[i]<<" ";
  164. }
  165. cout<<endl;
  166. }
  167. cout<<endl;
  168. getch();
  169. break;
  170. }
  171. case 8:
  172. {
  173. inisialisasi_stack();
  174. cout<<"Tumpukan sudah kosong !"<<endl;
  175. cout<<endl;
  176. getch();
  177. break;
  178. }
  179. case 9:
  180. {
  181. cout<<"Program berhenti";
  182. return 0;
  183. }
  184. break;
  185. default:
  186. {
  187. cout<<"Tidak ada dalam pilihan"<<endl;
  188. getch();
  189. }
  190. }
  191. }while(pilih>=1);
  192. getch();
  193. }
  194.  
  195.  
  196. // fungsi inisialisasi stack = kosong
  197. void inisialisasi_queue()
  198. {
  199. antrian.top_queue = -1;
  200. }
  201. void inisialisasi_stack()
  202. {
  203. tumpukan.top_stack = -1;
  204. }
  205.  
  206. //fungsi mengecheck apakah stack kosong
  207. int IsEmpty_queue()
  208. {
  209. if (antrian.top_queue == -1)
  210. {
  211. return 1;
  212. }
  213. else
  214. {
  215. return 0;
  216. }
  217. }
  218. int IsEmpty_stack()
  219. {
  220. if (tumpukan.top_stack == -1)
  221. {
  222. return 1;
  223. }
  224. else
  225. {
  226. return 0;
  227. }
  228. }
  229.  
  230. //fungsi mengecheck apakah stack penuh
  231. int IsFull_queue()
  232. {
  233. if (antrian.top_queue == 100-1)
  234. {
  235. return 1;
  236. }
  237. else
  238. {
  239. return 0;
  240. }
  241. }
  242. int IsFull_stack()
  243. {
  244. if (tumpukan.top_stack == 100-1)
  245. {
  246. return 1;
  247. }
  248. else
  249. {
  250. return 0;
  251. }
  252. }
  253.  
  254. //fungsi untuk menyisipkan data ke stack
  255. void enqueue(string data_queue)
  256. {
  257. antrian.top_queue++;
  258. antrian.data_queue[antrian.top_queue] = data_queue;
  259. }
  260.  
  261. void push(string data_stack)
  262. {
  263. tumpukan.top_stack++;
  264. tumpukan.data_stack[tumpukan.top_stack] = data_stack;
  265. }
  266.  
  267. //fungsi untuk mengeluarkan data dari stack
  268. void dequeue()
  269. {
  270. for(int i=0; i<100; i++)
  271. {
  272. antrian.data_queue[i] = antrian.data_queue[i+1];
  273. }
  274. antrian.top_queue = antrian.top_queue - 1;
  275. if(antrian.top_queue<0)
  276. {
  277. antrian.top_queue = -1;
  278. }
  279. }
  280.  
  281. void pop()
  282. {
  283. tumpukan.top_stack = tumpukan.top_stack - 1;
  284. if(tumpukan.top_stack<0)
  285. {
  286. tumpukan.top_stack = -1;
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement