Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import java.io.*;
  2. public class stack2 {
  3. public static void main(String [] args) throws IOException{
  4. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  5. int ar [] = new int[10];
  6. int top = -1;
  7. System.out.print("Angka yang dimasukkan ke stack : "); //memasukkan angka ke stack
  8. int angka = Integer.parseInt(br.readLine());
  9. System.out.println();
  10. boolean isEmpty;
  11. if(top == -1) isEmpty = true; // Memeriksa stack apakah kosong
  12. else isEmpty = false;
  13. boolean isFull;
  14. if(top == ar.length) isFull = true; // Memeriksa stack apakah penuh
  15. else isFull = false;
  16. // PUSH
  17. if(isFull == !true){
  18. top++; // top dinaikin
  19. ar[top] = angka;
  20. System.out.println("angka "+angka+" berhasil di input"); }
  21. else System.out.println("Stack penuh");
  22. // Menampilkan isi stack
  23. System.out.print("isi stack adalah ");
  24. for (int i = 0; i <= top; i++) {
  25. System.out.print( ar[i] + " "); }
  26. System.out.println();
  27. // POP
  28. if(isEmpty == true){
  29. int indexPop = top; //Menghapus index paling atas (pop)
  30. top--; //top diturunin
  31. System.out.println("stack index "+indexPop+" (angka "+ar[top+1]+")"+" berhasil di pop"); }
  32. else System.out.println("Stack kosong");
  33. // Menampilkan isi stack
  34. if(isEmpty == !true){
  35. System.out.print("isi stack adalah :");
  36. for (int i = 0; i <= top; i++) {
  37. System.out.print( ar[i] + " "); } }
  38. else System.out.println("isi stack adalah : kosong"); }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement