Pemrograman 11 (Stack)
Nama: Adrian Noval Firmansyah
NPM: 22082010214
Kelas: 2E
Source Code:
-Main
package BP2LatSTACK;
import java.util.Scanner;
public class StackMain {
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 : Adrian Noval Firmansyah");
System.out.println("NPM : 22082010207");
System.out.println("================================");
do{
System.out.println("---------------------------------");
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("-----------------------------------");
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("-----------------------------------");
System.out.println(" Output = PALINDROM ");
System.out.println("-----------------------------------");
}
else{
System.out.println("-----------------------------------");
System.out.println(" Output = BUKAN PALINDROM ");
System.out.println("-----------------------------------");
}
break;
case 2:
System.out.println("--------------------------------");
System.out.println(" Terima Kasih ");
System.out.println("--------------------------------");
break;
}
}while(pilih!=2);
}
}
-cNode
package BP2LatSTACK;
public class cNode {
private String nama;
cNode next,prev;
cNode(String n){
nama=n;
System.out.println("Object "+n+" dibuat...");
}
public String getNama(){
return nama;
}
}
-cStack
package BP2LatSTACK;
public class cStack {
cNode top,bottom;
int jumlah;
cStack(){
top=bottom=null;
jumlah=0;
System.out.println("Object Stack dibuat...");
}
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("---------------------------------");
System.out.println(" Stack Kosong ");
System.out.println("---------------------------------");
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:
Hasil Run:
Komentar
Posting Komentar