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
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.
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...
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...
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...
Using C++, Right down a code that calculates insuranceprice:* requires driver to enter their...
Using C++, Right down a code that calculates insurance price:* requires driver to enter their age and the amounts of accidents they have hadbased on that:*base price is $500*additional fees of $100 if driver is younger than 25*additional fees based on accidents:number of accidentsfees of accidents15021253225437555756 or moreno insurance*Use switch*if had 6 or more accidents won't be able to get insurance
Using MongoDB, what command would you enter to retrieve this document using only the zip code...
Using MongoDB, what command would you enter to retrieve this document using only the zip code 11242? db.inspections.find(???????) Database: city Collection: inspections {"_id":{"$oid":"56d61033a378eccde8a898ae"}, "id":"23536-2015-ENFO", "certificate_number":5373970, "business_name":"NZO CORP.", "date":"Apr 22 2015", "result":"Violation Issued", "sector":"Grocery-Retail - 808", "address":{"city":"BROOKLYN", "zip":11242, "street":"COURT ST", "number":26}}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT