Hi, Please find my code.
I have correted all of your classes.
Please let me know in case if do not get concpt.
######## Node.java ###########
public class Node {
private E element;
private Node next;
public Node() {
this.element = null;
this.next = null;
}
public Node(E e) {
this.element = e;
this.next = null;
}
public E getElement() {
return this.element;
}
public void setElement(E element) {
this.element= element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
########## Stack.java #############
public class Stack {
private int N;
private Node top;
public Stack() {
N = 0;
this.top =null;
}
/*
* places element on the top of the stack
*/
public void push(E element){
Node temp = new Node(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 {
private Node front;
private Node 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 temp = new Node(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.util.Scanner;
public class Palindrome
{
public static void main(String[ ] args)
{
Scanner input = new Scanner(System.in);
String inputString;
System.out.print(\"Please enter your string: \");
inputString = input.next( );
if (isPalindrome( inputString )){
System.out.println(\"Yes it is a palindrome.\");
}
else{
System.out.println(\"No this is not a palindrome.\");
}
}
public static boolean isPalindrome(String input)
{
Queue q = new Queue ();
Stack s = new Stack ();
char letter;
int i;
for (i = 0; i < input.length( ); i++)
{
letter = input.charAt(i);
q.enqueue(letter);
s.push(letter);
}
while (!q.isEmpty( ))
{
if (q.dequeue() != s.pop( ))
return false;
}
return true;
}
}
/*
Sample Output:
Please enter your string: MadaM
Yes it is a palindrome.
*/
Solution
Hi, Please find my code.
I have correted all of your classes.
Pl.
The Liver & Gallbladder (Anatomy & Physiology).pptx
Hi, Please find my code.I have correted all of your classes.Plea.pdf
1. Hi, Please find my code.
I have correted all of your classes.
Please let me know in case if do not get concpt.
######## Node.java ###########
public class Node {
private E element;
private Node next;
public Node() {
this.element = null;
this.next = null;
}
public Node(E e) {
this.element = e;
this.next = null;
}
public E getElement() {
return this.element;
}
public void setElement(E element) {
this.element= element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
########## Stack.java #############
public class Stack {
private int N;
private Node top;
2. public Stack() {
N = 0;
this.top =null;
}
/*
* places element on the top of the stack
*/
public void push(E element){
Node temp = new Node(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();
}
3. //returns the size of the stack
public int size(){
return N; //replace
}
public boolean isEmpty(){
return top==null;
}
}
############ Queue.java ############
public class Queue {
private Node front;
private Node 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 temp = new Node(element);
if(front == null) {
front = temp;
back = temp;
}
else{
back.setNext(temp);
back = temp;
}
N++;
4. }
/*
* 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;
5. }
}
########### Palindrome.java ############
import java.util.Scanner;
public class Palindrome
{
public static void main(String[ ] args)
{
Scanner input = new Scanner(System.in);
String inputString;
System.out.print("Please enter your string: ");
inputString = input.next( );
if (isPalindrome( inputString )){
System.out.println("Yes it is a palindrome.");
}
else{
System.out.println("No this is not a palindrome.");
}
}
public static boolean isPalindrome(String input)
{
Queue q = new Queue ();
Stack s = new Stack ();
char letter;
int i;
for (i = 0; i < input.length( ); i++)
{
letter = input.charAt(i);
q.enqueue(letter);
s.push(letter);
}
while (!q.isEmpty( ))
{
if (q.dequeue() != s.pop( ))
return false;
}
6. return true;
}
}
/*
Sample Output:
Please enter your string: MadaM
Yes it is a palindrome.
*/
Solution
Hi, Please find my code.
I have correted all of your classes.
Please let me know in case if do not get concpt.
######## Node.java ###########
public class Node {
private E element;
private Node next;
public Node() {
this.element = null;
this.next = null;
}
public Node(E e) {
this.element = e;
this.next = null;
}
public E getElement() {
return this.element;
}
public void setElement(E element) {
this.element= element;
}
public Node getNext() {
return next;
}
7. public void setNext(Node next) {
this.next = next;
}
}
########## Stack.java #############
public class Stack {
private int N;
private Node top;
public Stack() {
N = 0;
this.top =null;
}
/*
* places element on the top of the stack
*/
public void push(E element){
Node temp = new Node(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;
}
8. /*
* 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 {
private Node front;
private Node 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 temp = new Node(element);
9. 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;
10. return back.getElement();
}
//returns the size of the queue
public int size(){
return N;
}
public boolean isEmpty(){
return front==null;
}
}
########### Palindrome.java ############
import java.util.Scanner;
public class Palindrome
{
public static void main(String[ ] args)
{
Scanner input = new Scanner(System.in);
String inputString;
System.out.print("Please enter your string: ");
inputString = input.next( );
if (isPalindrome( inputString )){
System.out.println("Yes it is a palindrome.");
}
else{
System.out.println("No this is not a palindrome.");
}
}
public static boolean isPalindrome(String input)
{
Queue q = new Queue ();
Stack s = new Stack ();
char letter;
int i;
for (i = 0; i < input.length( ); i++)