In: Computer Science
IN JAVA -
[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate
[(2)] Let Q be a non-empty queue, and let S be an empty stack. Using only the stack and queue ADT functions and a single element variable X, write an algorithm to reverse the order of the elements in Q.
[(3)] Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in terms of the number of digits for the two operands of each function?
[(4)] Implement a program that can input an expression in postfix notation and output its value.
SOLUTION -
Code for Palindrome.java
Sample Output 1
Sample Output 2
Code for Palindrome.java
import java.io.*;
import java.util.Stack;
import java.util.LinkedList; //libraries required
import java.util.Queue;
import java.util.Scanner;
public class Palindrome{
public static boolean check_Palindrome(String str){
//method to check if the given string is palindrome or not
Queue<Character> queue = new LinkedList<Character>(); //declaring the queue
Stack<Character> stack = new Stack<Character>(); //declaring the stack
Character alphabet;
int count = 0;
for (int i = 0; i < str.length(); i++){
alphabet =
str.charAt(i);
queue.add(alphabet); //add each character to the queue and the
stack
stack.push(alphabet);
}
while (!queue.isEmpty()){
if
(queue.remove()!=stack.pop()) //removal of each character from
queue and stack must be same to say it's palindrome
count++;
}
if(count==0) //if the count
equal to zero then it is a palindrome
return
true;
else
return
false;
}
public static void main(String[] args) {
String str;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string : "); //taking string from user
str = sc.nextLine();
if(check_Palindrome(str))
System.out.println("True");
else
System.out.println("False");
}
}
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------