Skip to content

Instantly share code, notes, and snippets.

@akashkumarcs19
Created February 22, 2021 18:19
Show Gist options
  • Save akashkumarcs19/a14ae3237236af1029fb551c7001a1b8 to your computer and use it in GitHub Desktop.
Save akashkumarcs19/a14ae3237236af1029fb551c7001a1b8 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class Stack{
char arr[];
int topOfStack;
char temp;
public Stack(int size){
arr = new char[size];
topOfStack=-1;
}
public boolean isEmpty(){
if(topOfStack==-1){
return true;
}
else
return false;
}
public boolean isFull(){
if(topOfStack==arr.length-1)
return true;
else return false;
}
public void push(char c){
if(isFull())
System.out.println("OverFlow");
else {
++topOfStack;
arr[topOfStack] = c;
}
}
public char pop(){
if(isEmpty())
System.out.println("UnderFloww");
else {
temp = arr[topOfStack];
topOfStack--;
}return temp;
}
}
class Queue{
char arr1[];
char temp;
int beginning,topOfQueue;
public Queue(int size){
arr1 = new char[size];
beginning = -1;
topOfQueue = -1;
}
public boolean isEmpty(){
if(beginning == -1 && topOfQueue == -1)
return true;
else return false;
}
public boolean isFull(){
if(topOfQueue==arr1.length-1) return true;
else return false;
}
public void enQueue(char c){
if(isEmpty()){
arr1[beginning+1] = c;
topOfQueue++;
}
else if (isFull()) System.out.println("OverFlow");
else{
arr1[topOfQueue+1] = c;
topOfQueue++;
}
}
public char deQueue(){
if(isEmpty()) System.out.println("UnderFlow");
else{
temp = arr1[beginning+1];
beginning++;
}
return temp;
}
}
class Pallindrome{
String str;
char temp[];
public Pallindrome(String str){
this.str = str;
temp = str.toCharArray();
}
public void checkPallindrome(){
Stack stack = new Stack(temp.length);
Queue queue = new Queue(temp.length);
boolean flag = true;
for (int i = 0; i < temp.length; i++) {
stack.push(temp[i]);
queue.enQueue(temp[i]);
}
for (int i = 0; i < temp.length; i++) {
if(stack.pop()!=queue.deQueue())
flag = false;
else
return;
}
if(flag == true) System.out.println("Pallindrome");
else System.out.println("Not");
}
}
public class PallindromeMain{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.next();
Pallindrome obj = new Pallindrome(str);
obj.checkPallindrome();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment