Animasi stack yang berisiPOP, PUSH,N Quit
import java.util.Scanner;
class Tumpukan{
public int max;
public String isi[];
public int top;
public Tumpukan(int S){
max = S;
isi = new String[max];
top = -1;
}
public void push(String s){
top++;
isi[top] = s;
}
public void pop(){
top--;
}
}
class Animasi{
public static void main(String [] args){
Scanner m = new Scanner(System.in);
Tumpukan T = new Tumpukan(10);
String huruf;
int pil;
do{
System.out.println("ANIMASI STACK : ");
System.out.println(" 1. Push");
System.out.println(" 2. Pop");
System.out.println(" 3. Keluar\n");
System.out.print("Pilihan [1/2/3] = ");
pil = m.nextInt();
switch(pil){
case 1 : System.out.print("Masukkan sebuah huruf : ");
huruf = m.next();
T.push(huruf);
System.out.println();
System.out.println(" /");
System.out.println(" "+T.isi[T.top]);
System.out.println(" \\");
System.out.println("\n");
for(int i=T.top; i>=0; i--){
System.out.println(" | "+T.isi[i]+" |");
}
System.out.println(" +-----+"); System.out.println("\n ");
break;
case 2 : System.out.println("\n ");
System.out.println(" \\");
System.out.println(" "+T.isi[T.top]);
System.out.println(" /");
System.out.println("\n");
T.pop();
for(int i=T.top; i>=0; i--){
System.out.println(" | "+T.isi[i]+" |");
}
System.out.println(" +-----+"); System.out.println("\n ");
break;
}
}while(pil !=3);
}
}
Tidak ada komentar:
Posting Komentar