membuat program polindrom linkedlist dengan java


import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;

class PolindromLinkedList
{
 public static void main(String [] args) throws IOException
 {
  LinkedList list = new LinkedList();
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Masukkan Kata : ");
  String kata = in.readLine();

  int x = kata.length();
  // dapatkan titik tengah
  int y = (int) x / 2; 

  // masukkan kedalam list
  for(int i = 0; i < y;i++){
   list.addLast(kata.charAt(i));
  }


  int p;

  if(x % 2 == 0){
   p = y + 2;
  } else {
   p = y + 1;
  }
  
  boolean isPolindrom = true;
  for(int i = p; i < x;i++){
   if(kata.charAt(i) != list.removeLast()){
    isPolindrom = false;
   }
  }

  System.out.print("Kata \""+kata+"\" termasuk ");
  
  if(isPolindrom){
   System.out.println("Polindrom");
  } else {
   System.out.println("Tidak Polindrom");
  }
 } 
}



Post a Comment

0 Comments