Latihan Stack Palindrom
Nama: Kimberlie Cindy Kolopaking
NPM: 22082010200
Kelas: 2E
Source Code:
- appStack
package Stack;
import java.util.Scanner;
public class appStack {
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 : Kimberlie Cindy Kolopaking");
System.out.println("NPM : 22082010200");
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(" 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();
}
}
}
Output:
----------------------------------
Nama : Kimberlie Cindy Kolopaking
NPM : 22082010200
----------------------------------
===================================
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 SUCCESS
------------------------------------------------------------------------
Total time: 19.802 s
Finished at: 2023-06-15T22:13:15+07:00
------------------------------------------------------------------------
Tampilan Program:
.png)
Comments
Post a Comment