Using stack and queue A string is said to be a palindrome if

Using stack and queue.

A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 45811854, and   ABLE WAS I ERE I SAW ELBA are palindromes. Write a program that can read a number of sentences, each of which may contain spaces, numbers, punctuations. Use the Queue class and Stack class in your program to process each sentence, as the following procedures show: read a sentence, check each character to see if it is a letter or number, if it is, pushing the character onto a stack and add it to a queue at the same time. When the end of the sentence check is encountered, the program should use the basic stack and queue operations to determine if the string is a palindrome. Your program need to be able to read our provided text file, find which line is palindrome and which line is not, and output all of palindromes line by line to a new file.

- make it simple pls

Solution

Hi, Please find my implementation.

I have implemented Generic Stack and Queue classes , and I have used these classes to palindrome.

######### Stack.java ########

public class Stack<E> {

private int N;

   private Node<E> top;

  

   public Stack() {

       N = 0;

       this.top =null;

   }

/*

   * places element on the top of the stack

   */

   public void push(E element){

       Node<E> temp = new Node<E>(element);

       temp.setNext(top);

       top = temp;

       N++;

   }

/*

   * remove the top node and return its contents

   */

   public E pop(){

      

       if(top == null)

           return null;

      

       E e = top.getElement();

       top = top.getNext();

       N--;

       return e;

   }

/*

   * Look at the top element of the Stack and return it, without removing

   */

   public E peek(){

      

       if(top == null)

           return null;

      

       return top.getElement();

   }

//returns the size of the stack

   public int size(){

       return N; //replace

   }

  

   public boolean isEmpty(){

       return top==null;

   }

}

########## Queue.java ###############

public class Queue<E> {

   private Node<E> front;

   private Node<E> back;

private int N;

  

   public Queue() {

       this.front = null;

       this.back = null;

       N = 0;

   }

/*

   * places element in the back of the Queue

   */

   public void enqueue(E element){

       Node<E> temp = new Node<E>(element);

       if(front == null) {

           front = temp;

           back = temp;

       }

       else{

           back.setNext(temp);

           back = temp;

       }

      

       N++;

   }

/*

   * remove the front node of the queue and return it

   */

   public E dequeue(){

       if(front == null) {

           return null;

       }

      

       E item = front.getElement();

      

       front = front.getNext();

       // if we had only one element

       if(front == null){

           back = null;

       }

      

       N--;

       return item;

   }

/*

   * Look at the front of the queue and return it, without removing

   */

   public E peek(){

       if(back == null)

           return null;

      

       return back.getElement();

   }

//returns the size of the queue

   public int size(){

       return N;

   }

  

   public boolean isEmpty(){

       return front==null;

   }

}

############# Palindrome.java ###################

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class Palindrome

{

   public static void main(String[ ] args) throws IOException

   {

       Scanner input = new Scanner(System.in);

      

       System.out.print(\"Enter input file name: \");

       String fileName = input.next();

      

       // opening input file

       FileReader fr = new FileReader(fileName);

       BufferedReader br = new BufferedReader(fr);

      

       String line;

      

       // reading line by line

       while((line = br.readLine()) != null){

           // checking whether current line is palindrome or not

           System.out.println(line);

           if (isPalindrome( line )){

               System.out.println(\"   Yes it is a palindrome.\");

           }

           else{

               System.out.println(\"   No this is not a palindrome.\");

           }

       }

      

       br.close();

       fr.close();

      

   }

  

   public static boolean isPalindrome(String input)

   {

       Queue<Character> q = new Queue<Character> ();

       Stack<Character> s = new Stack<Character> ();

       char letter;

       int i;

       // iterating over input string

       for (i = 0; i < input.length( ); i++)

       {

           letter = input.charAt(i);

          

           // if current character is digit or letter then push into stack and add into queue

           if(Character.isLetterOrDigit(letter)){

               q.enqueue(letter);

               s.push(letter);

           }

       }

      

       // now pop from stack and dequeue from queue character by character and compare

       while (!q.isEmpty( ))

       {

           if (q.dequeue() != s.pop( ))

               return false;

       }

       return true;

   }

}

/*

Sample Output:

Enter input file name: input.txt

MADAM

   Yes it is a palindrome.

45811854

   Yes it is a palindrome.

ABLE WAS I ERE I SAW ELBA

   Yes it is a palindrome.

xys 423 ewedwe d

   No this is not a palindrome.

*/

####### input.txt ############

MADAM
45811854
ABLE WAS I ERE I SAW ELBA
xys 423 ewedwe d

Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458
Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458
Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458
Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458
Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458
Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458
Using stack and queue. A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 458

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site