In: Computer Science
Java and please have screenshots with source code and output
\item[(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
\item[(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.
\item[(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?
\item[(4)] Implement a program that can input an expression in postfix notation and output its value.
Here is the code for the above problem,
These are 4 different questions and not a single problem to answer. Our guidelines don't allow us to answer more than 1 question at a time. But I have answered 3 problems leaving 3rd only.
Go through all and even if then you face any problem let me know in the comments, I would try to guide you but not copy-paste the code. I hope you understand.
Codes.java
import java.util.*;
public class Codes {
private static final Scanner sc = new Scanner(System.in);
// CODE 1
private static boolean isPalindrome(StringBuilder sb) {
int mid = sb.length() / 2;
int begin = sb.length() % 2 == 0 ? mid - 1 : mid;
int end = mid;
while (begin >= 0 && end < sb.length()) {
if (sb.charAt(begin) != sb.charAt(end)) return false;
begin--;
end++;
}
return true;
}
public static void reverseQueueItems(Queue<Integer> queue) {
Stack<Integer> stack = new Stack<>();
while (!queue.isEmpty()) stack.push(queue.poll());
while (!stack.isEmpty()) queue.add(stack.pop());
}
public static int evaluatePostfix(String exp) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < exp.length(); i++) {
char c = exp.charAt(i);
if (Character.isDigit(c))
stack.push(c - '0');
else {
int val1 = stack.pop();
int val2 = stack.pop();
switch (c) {
case '+':
stack.push(val2 + val1);
break;
case '-':
stack.push(val2 - val1);
break;
case '/':
stack.push(val2 / val1);
break;
case '*':
stack.push(val2 * val1);
break;
}
}
}
return stack.pop();
}
public static String printQueue(Queue<Integer> queue) {
if (queue.isEmpty()) return "{}";
StringBuilder sb = new StringBuilder();
for (int value : queue) sb.append(value).append(", ");
sb.setLength(sb.length() - 2);
sb.insert(0, "{");
sb.append("}");
return sb.toString();
}
public static void main(String[] args) {
// CODE 1 : Uncomment code to test
/*
StringBuilder sb = new StringBuilder();
System.out.println("Enter $ to quit the program. Else continue entering the character line by line.");
while (true){
char ch = sc.next().charAt(0);
if(ch == '$') break;
sb.append(ch);
boolean isPalindrome = isPalindrome(sb);
System.out.println("isPalindrome? : " + isPalindrome);
}
*/
// CODE 2 : Uncomment code to test
/*
Queue<Integer> queue = new LinkedList<>();
System.out.println("Enter -1 to quit and reverse the order of elements in queue. " +
"\nElse continue entering the elements line by line.");
while (true){
int element = sc.nextInt();
if(element == -1){
System.out.println("Elements in queue Before : "+printQueue(queue));
reverseQueueItems(queue);
System.out.println("Elements in queue After : "+printQueue(queue));
break;
}
queue.add(element);
}
*/
// CODE 4 : Uncomment code to test
/*
System.out.println("Enter correct postfix expression : ");
String postfixExp = sc.next();
int value = evaluatePostfix(postfixExp);
System.out.println("Postfix value for :"+postfixExp+" is : "+value);
*/
}
}
OUTPUTS:
Also, please do upvote the solution if I have answered your problem.