Question

In: Computer Science

Queues, Deques and Priority Queues. Enter the necessary code where indicated (using: MyQues.java - below and...

Queues, Deques and Priority Queues. Enter the necessary code where indicated (using: MyQues.java - below and questions) How would you use a stack to verify whether a given string is a palindrome or not? How would you check if a string is a palindrome or not, using queues and deques?

Solutions

Expert Solution

Q:- How would you use a stack to verify whether a given string is a palindrome or not?
Java Code:--

import java.util.Stack;
import java.util.Scanner;
class Main {

public static void main(String[] args) {

   System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String inputString = in.nextLine();
Stack stack = new Stack();

for (int i = 0; i < inputString.length(); i++) {
stack.push(inputString.charAt(i));
}

String reverseString = "";

while (!stack.isEmpty()) {
reverseString = reverseString+stack.pop();
}

if (inputString.equals(reverseString))
System.out.println("The input String is a palindrome.");
else
System.out.println("The input String is not a palindrome.");

}
}


Q:- How would you check if a string is a palindrome or not, using queues and deques?
Java Code:-

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
class Main {

public static void main(String[] args) {

   System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String inputString = in.nextLine();
Queue queue = new LinkedList();

for (int i = inputString.length()-1; i >=0; i--) {
queue.add(inputString.charAt(i));
}

String reverseString = "";

while (!queue.isEmpty()) {
reverseString = reverseString+queue.remove();
}
if (inputString.equals(reverseString))
System.out.println("The input String is a palindrome.");
else
System.out.println("The input String is not a palindrome.");

}
}


Related Solutions

I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
Using the code below as a template, write a program that asks the user to enter...
Using the code below as a template, write a program that asks the user to enter two integers, and finds and prints their greatest common denominator (GCD) in Java! package cp213; import java.util.Scanner; public class Lab01 { /** * @param a * @param b * @return */ public static int gcd(int a, int b) { // your code here } /** * @param args */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int a = 0;...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
Give the VHDl code for your an 8-to-3 priority encoder using two 4-to-2 priority encoders and...
Give the VHDl code for your an 8-to-3 priority encoder using two 4-to-2 priority encoders and any additional necessary gates. Use port maps and code the structural behavior using logic gates not if else statements.
USING MATLAB Where indicated in the script below, write a function, called myflip, which accepts one...
USING MATLAB Where indicated in the script below, write a function, called myflip, which accepts one vector v (either a column or a row) and outputs the same vector v of the same dimensions, but with the values in reverse order (use MATLAB function flip()). In other words, v will be overwritten by its flipped version. In your function, you may only use length() and floor(). You only need one loop. %this code tests the function, myflip, which you will...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using...
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using gates.
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation Fn = Fn-1 + Fn-2: 0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13 The sequence would be the numbers in red (0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT