Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class stack2 {
- public static void main(String [] args) throws IOException{
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int ar [] = new int[10];
- int top = -1;
- System.out.print("Angka yang dimasukkan ke stack : "); //memasukkan angka ke stack
- int angka = Integer.parseInt(br.readLine());
- System.out.println();
- boolean isEmpty;
- if(top == -1) isEmpty = true; // Memeriksa stack apakah kosong
- else isEmpty = false;
- boolean isFull;
- if(top == ar.length) isFull = true; // Memeriksa stack apakah penuh
- else isFull = false;
- // PUSH
- if(isFull == !true){
- top++; // top dinaikin
- ar[top] = angka;
- System.out.println("angka "+angka+" berhasil di input"); }
- else System.out.println("Stack penuh");
- // Menampilkan isi stack
- System.out.print("isi stack adalah ");
- for (int i = 0; i <= top; i++) {
- System.out.print( ar[i] + " "); }
- System.out.println();
- // POP
- if(isEmpty == true){
- int indexPop = top; //Menghapus index paling atas (pop)
- top--; //top diturunin
- System.out.println("stack index "+indexPop+" (angka "+ar[top+1]+")"+" berhasil di pop"); }
- else System.out.println("Stack kosong");
- // Menampilkan isi stack
- if(isEmpty == !true){
- System.out.print("isi stack adalah :");
- for (int i = 0; i <= top; i++) {
- System.out.print( ar[i] + " "); } }
- else System.out.println("isi stack adalah : kosong"); }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement