Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. import java.io.*;
  2. class coreStack
  3. {
  4. private int max_stack;
  5. public int top;
  6. private String array[];
  7.  
  8. public coreStack(int n)
  9. {
  10. max_stack = n;
  11. array = new String[n];
  12. top = -1;
  13. }
  14.  
  15. public void push(String value)
  16. {
  17. array[++top] = value;
  18. }
  19.  
  20. public String pop()
  21. {
  22. return array[top--];
  23. }
  24.  
  25. public String peek()
  26. {
  27. return array[top];
  28. }
  29.  
  30. public boolean isEmpty()
  31. {
  32. return (top==-1);
  33. }
  34.  
  35. public boolean isFull()
  36. {
  37. return (top==max_stack-1);
  38. }
  39.  
  40. public void cetak()
  41. {
  42. int i = top;
  43. while ( i>= 0)
  44. {
  45. System.out.print(array[i]);
  46. System.out.print(" ");
  47. i--;
  48. }
  49. System.out.println (" ");
  50. }
  51. }
  52.  
  53. class coreQueue
  54. {
  55. private int max_queue;
  56. private String array[];
  57. private int front;
  58. private int rear;
  59. public int count;
  60.  
  61. public coreQueue(int n)
  62. {
  63. max_queue = n;
  64. array = new String[n];
  65. front = 0;
  66. rear = -1;
  67. count = 0;
  68. }
  69.  
  70. public void enQueue(String value)
  71. {
  72. if(!isFull())
  73. {
  74. array[++rear] = value;
  75. count++;
  76. }
  77. else
  78. {
  79. System.out.println("Antrian sudah penuh");
  80. }
  81. }
  82.  
  83. public String deQueue()
  84. {
  85. String temp = array[0];
  86. if(!isEmpty())
  87. {
  88. for(int i=0; i<count; i++)
  89. {
  90. array[i] = array[i+1];
  91. --count;
  92. rear--;
  93. }
  94. }
  95. else
  96. {
  97. System.out.println("Antrian sudah kosong");
  98. }
  99. return temp;
  100. }
  101.  
  102. public String peekFront()
  103. {
  104. return array[front];
  105. }
  106.  
  107. public boolean isFull()
  108. {
  109. return (rear == max_queue-1);
  110. }
  111.  
  112. public boolean isEmpty()
  113. {
  114. return (count == 0);
  115. }
  116.  
  117. public int size()
  118. {
  119. return count;
  120. }
  121.  
  122. public void print()
  123. {
  124. for(int i=0; i<count; i++)
  125. {
  126. System.out.print(array[i] + " ");
  127. }
  128. System.out.println("");
  129. }
  130. }
  131.  
  132. class stack
  133. {
  134. public static void main(String[] args) throws IOException
  135. {
  136. BufferedReader io = new BufferedReader(new InputStreamReader(System.in));
  137. System.out.print("Masukkan maksimal stack : ");
  138. int n = Integer.parseInt(io.readLine());
  139.  
  140. System.out.println("=============================");
  141. System.out.println("==========Menu Stack=========");
  142. System.out.println("=============================");
  143. System.out.println("1. Push Stack\t\t (1)");
  144. System.out.println("2. Pop Stack\t\t (2)");
  145. System.out.println("3. Peek Stack\t\t (3)");
  146. System.out.println("4. Check isFull\t\t (4)");
  147. System.out.println("5. Check isEmpty\t (5)");
  148. System.out.println("6. Print Stack\t\t (6)");
  149. System.out.println("7. Input to Queue\t (7)");
  150. System.out.println("8. Print Queue\t\t (8)");
  151. System.out.println("9. Input to Queue + DeQueue + Print (9)");
  152. System.out.println("=============================");
  153. System.out.println("");
  154.  
  155. coreStack s = new coreStack(n);
  156. coreQueue q = new coreQueue(n);
  157.  
  158. for(String b="y"; b.equals("y")|| b.equals("Y");)
  159. {
  160. System.out.print("Tekan sesuai daftar untuk melakukan operasi stack.");
  161. System.out.println("");
  162.  
  163. int menu = Integer.parseInt(io.readLine());
  164. switch(menu)
  165. {
  166. case 1 : if(s.isFull() != true)
  167. {
  168. System.out.println("Masukkan data yang akan dipush ke stack : ");
  169. String data = io.readLine();
  170. s.push(data);
  171. System.out.println (" ");
  172. }
  173. else
  174. {
  175. System.out.println("Stack penuh.");
  176. System.out.println (" ");
  177. }
  178. break;
  179.  
  180. case 2 : if(s.isEmpty() != true)
  181. {
  182. System.out.println(s.pop());
  183. System.out.println (" ");
  184. }
  185. else
  186. {
  187. System.out.println("Stack kosong.");
  188. System.out.println (" ");
  189. }
  190. break;
  191.  
  192. case 3 : if(s.isEmpty() != true)
  193. {
  194. System.out.println(s.pop());
  195. System.out.println (" ");
  196. }
  197. else
  198. {
  199. System.out.println("Stack kosong.");
  200. System.out.println (" ");
  201. }
  202. break;
  203.  
  204. case 4 : if(s.isFull() != true)
  205. {
  206. System.out.println("Stack belum penuh.");
  207. System.out.println (" ");
  208. }
  209. else
  210. {
  211. System.out.println("Stack penuh.");
  212. System.out.println (" ");
  213. }
  214. break;
  215.  
  216. case 5 : if(s.isEmpty() != true)
  217. {
  218. System.out.println("Stack belum kosong.");
  219. System.out.println (" ");
  220. }
  221. else
  222. {
  223. System.out.println("Stack kosong.");
  224. System.out.println (" ");
  225. }
  226. break;
  227.  
  228. case 6 : s.cetak();
  229. System.out.println (" ");
  230. break;
  231.  
  232. case 7 : int i = s.top;
  233. while(i>=0)
  234. {
  235. q.enQueue(s.pop());
  236. i--;
  237. }
  238. System.out.println("Berhasil dimasukkan ke Queue");
  239. break;
  240.  
  241. case 8 : q.print();
  242. System.out.println (" ");
  243. break;
  244.  
  245. case 9 : int k = s.top;
  246. while(k>=0)
  247. {
  248. q.enQueue(s.pop());
  249. k--;
  250. }
  251. int l = q.count;
  252. for(int j=0; j<l; j++)
  253. {
  254. System.out.print(q.deQueue());
  255. --l;
  256. }
  257. System.out.println("");
  258. break;
  259. default : System.out.println("Maaf menu yang Anda masukkan salah.");
  260. System.out.println (" ");
  261. break;
  262. }
  263.  
  264. System.out.print("Ada operasi stack lain? (Y/n) : ");
  265. b = io.readLine();
  266. System.out.println (" ");
  267. }
  268. }
  269. }
Add Comment
Please, Sign In to add comment