STACK PALINDROM


Nama    : Vivi Eka Juliatus Sholihah

NPM     : 22082010037

Kelas    : Pararel A


PALINDROME MENGGUNAKAN STACK

Source Code Class cData

package Palindromestack;

public class cData {

    String text;

    cData next,prev;

    

    public cData(String t){

        text = t;

    }

    public String getText(){

        return text;

    }

}


Source Code Class cStack

package Palindromestack;

public class cStack {

    cData top, bottom;

    int jumlah;

    

    //constructor stack

    cStack(){

        top = bottom = null;

        System.out.println(" ");

    }

    //push untuk memasukkan letter ke stack

    public void push(cData letter){

        if(top == null){ //jika kosong

            top = bottom = letter;

        }else{

            //penambahan paling atas

            letter.next = top;

            top.prev = letter;

            top = letter;

        }

    }

    public String pop(){    //yang dihilangkan elemen paling atas

        if(top == null){    //stack pada kondisi kosong

            System.out.println(" Stack Kosong ");

            return null;

        }

        else if(top.next == null){  //stack sisa satu

            cData t = top;

            top = bottom = null;

            t.next = null; 

            t.prev = null;

            return t.getText();

        }else{

            //jika stack >1

            cData t = top;

            top = top.next;

            top.prev = null;

            t.next = null;

            t.prev = null;

            return t.getText();

        }

    }

}


Source Code Class appPalindrom

package Palindromestack;

import java.util.Scanner;

public class appPalindrome {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        cStack stack = new cStack();

        int pilih = 0;

        

        do{

            String text = "";

            String convert = "";

            System.out.println("====================================");

            System.out.println("  PROGRAM UNTUK MENGECEK PALINDROM  ");

            System.out.println("====================================");

            System.out.println("1. Cek Palindrom");

            System.out.println("2. Selesai");

            System.out.println("------------------------------------");

            System.out.print("pilih: ");

            pilih = s.nextInt();

            System.out.println("------------------------------------");

            System.out.println("");

            

            switch(pilih){

                case 1:

                    System.out.println("<<<<<<<<<<< CEK PALINDROM >>>>>>>>>>");

                    System.out.print("Input Text : ");

                    text = s.next();

                    for(int i=0; i < text.length(); i++){

                        cData input = new cData(Character.toString(text.charAt(i)));

                        stack.push(input);

                    }

                    for(int i=0; i < text.length(); i++){

                        String output = stack.pop();

                        convert = convert + output;

                    }

                    System.out.print("Output     : ");

                    if(text.equalsIgnoreCase(convert)){

                        System.out.println("Palindrom");

                        System.out.println("");

                    }else{

                        System.out.println("Bukan Palindrom");

                        System.out.println("");

                    }

                    break;

                case 2:

                    System.out.println("\t Terima Kasih...");

                    break;

            }

        }while(pilih !=2);

    }

}


HASIL RUN



Komentar