Nama : Novia Citra Fadhlilla
NPM : 22082010216
source code :
(App Stack)
package stack;
import java.util.Scanner;
public class Stack {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
cStack Stack=new cStack();
int pilih=0;
System.out.println("===================================");
System.out.println("Nama : Novia Citra Fadhlilla");
System.out.println("NPM : 22082010216");
System.out.println("===================================");
do{
System.out.println("\n---------------------------------");
System.out.println(" MENU STACK");
System.out.println("-----------------------------------");
System.out.println("1. Cek Palindrom");
System.out.println("2. Exit");
System.out.print("Pilih : ");
pilih=s.nextInt();
switch(pilih){
case 1:
System.out.println("-----------------------------------");
System.out.println(" CEK PALINDROM");
System.out.println("-----------------------------------");
System.out.print("Input Kata : ");
String kata=s.next();
for(int i=0;i<kata.length();i++){
char k=kata.charAt(i);
cNode huruf=new cNode(Character.toString(k));
Stack.push(huruf);
}
System.out.print("");
String output="";
for(int i=0;i<kata.length();i++){
String hasil=Stack.Pop();
output=output+hasil;
}
System.out.print("");
if(output.matches(kata)){
System.out.println("\nOutput = PALINDROM");
}
else{
System.out.println("\nOutput = BUKAN PALINDROM");
}
break;
case 2:
System.out.println("===================================");
System.out.println(" TERIMA KASIH!");
System.out.println("===================================");
break;
}
}while(pilih!=2);
}
}
(cNode)
package stack;
public class cNode {
private String nama;
cNode next,prev;
cNode(String n){
nama=n;
System.out.println("Object "+n+" created...");
}
public String getNama(){
return nama;
}
}
(cStack)
package stack;
public class cStack {
cNode top,bottom;
int jumlah;
cStack(){
top=bottom=null;
jumlah=0;
System.out.println("Object Stack created...");
}
public void push(cNode baru){
if(top==null){
top=bottom=baru;
}
else{
top.prev=baru;
baru.next=top;
top=baru;
}
System.out.println("Push "+baru.getNama()+" Success");
}
public String Pop(){
if(top==null){
System.out.println("Stack Kosong...");
return null;
}
else if(top.next==null){
cNode t=top;
top=bottom=null;
System.out.println("");
System.out.println("Pop "+t.getNama()+" Success");
return t.getNama();
}
else{
cNode t=top;
top=top.next;
top.prev=null;
t.next=null;
System.out.println("");
System.out.println("Pop "+t.getNama()+" Success");
return t.getNama();
}
}
}
Hasil Run :
Object Stack created...
===================================
Nama : Novia Citra Fadhlilla
NPM : 22082010216
===================================
---------------------------------
MENU STACK
-----------------------------------
1. Cek Palindrom
2. Exit
Pilih : 1
-----------------------------------
CEK PALINDROM
-----------------------------------
Input Kata : KATAK
Object K created...
Push K Success
Object A created...
Push A Success
Object T created...
Push T Success
Object A created...
Push A Success
Object K created...
Push K Success
Pop K Success
Pop A Success
Pop T Success
Pop A Success
Pop K Success
Output = PALINDROM
---------------------------------
MENU STACK
-----------------------------------
1. Cek Palindrom
2. Exit
Pilih : 1
-----------------------------------
CEK PALINDROM
-----------------------------------
Input Kata : KAMAR
Object K created...
Push K Success
Object A created...
Push A Success
Object M created...
Push M Success
Object A created...
Push A Success
Object R created...
Push R Success
Pop R Success
Pop A Success
Pop M Success
Pop A Success
Pop K Success
Output = BUKAN PALINDROM
---------------------------------
MENU STACK
-----------------------------------
1. Cek Palindrom
2. Exit
Pilih : 2
===================================
TERIMA KASIH!
===================================
BUILD SUCCESSFUL (total time: 14 seconds)
No comments:
Post a Comment