Question: Please write this in Java With using stacks (LinkedStack data structures) and queues (LinkedQueue data structures) , write a program to show an input string

Please write this in Java

With using stacks (LinkedStack data structures) and queues (LinkedQueue data structures) , write a program to show an input string is a palindrome string.

Below is all necessary code to complete palindrome.java

//StackInterface.java

/** An interface for the ADT stack. */ public interface StackInterface { /** Adds a new entry to the top of this stack. @param newEntry An object to be added to the stack. */ public void push(T newEntry); /** Removes and returns this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty before the operation. */ public T pop(); /** Retrieves this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty. */ public T peek(); /** Detects whether this stack is empty. @return True if the stack is empty. */ public boolean isEmpty(); /** Removes all entries from this stack. */ public void clear(); } // end StackInterface

//EmptyStackException

package LinkedListSolution;

/** A class of runtime exceptions thrown by methods to indicate that a stack is empty. */ public class EmptyStackException extends RuntimeException { public EmptyStackException() { this(null); } // end default constructor public EmptyStackException(String message) { super(message); } // end constructor } // end EmptyStackException

//LinkedStack.java

public final class LinkedStack implements StackInterface{ private Node topNode; //default constructor public LinkedStack(){ topNode = null; } // Implement the unimplemented methods private class Node{ T value; Node next; public Node(){ next = null; } }// end Node

public void push(T newEntry) { if(topNode == null){ topNode = new Node(); topNode.value = newEntry; } else{ Node newNode = new Node(); newNode.value = newEntry; newNode.next = topNode; topNode = newNode; } }

public T pop() { if(topNode == null){ throw new EmptyStackException("Pop operation is not possible on empty stack"); } else{ Node node = topNode; topNode = topNode.next; return node.value; } }

public T peek() { if(topNode == null){ throw new EmptyStackException("Peek operation is not possible on empty stack"); } else{ return topNode.value; } }

public boolean isEmpty() { return topNode == null; }

public void clear() { topNode = null; } }// end LinkedStack

//QueueInterface.java

/** An interface for the ADT queue. */ public interface QueueInterface { /** Adds a new entry to the back of this queue. @param newEntry An object to be added. */ public void enqueue(T newEntry); /** Removes and returns the entry at the front of this queue. @return The object at the front of the queue. @throws EmptyQueueException if the queue is empty before the operation. */ public T dequeue(); /** Retrieves the entry at the front of this queue. @return The object at the front of the queue. @throws EmptyQueueException if the queue is empty. */ public T getFront(); /** Detects whether this queue is empty. @return True if the queue is empty, or false otherwise. */ public boolean isEmpty(); /** Removes all entries from this queue. */ public void clear(); } // end QueueInterface

//EmptyQueueException.java

/** A class of runtime exceptions thrown by methods to indicate that a queue is empty. */ public class EmptyQueueException extends RuntimeException { public EmptyQueueException() { this(null); } // end default constructor public EmptyQueueException(String message) { super(message); } // end constructor } // end EmptyQueueException

//LinkedQueue.java

public class LinkedQueue implements QueueInterface { class Node { //data will be stored in the current node and //a reference to the next node T data; Node next; //constructor public Node(T data) { this.data = data; } }

private Node head, tail;

//constructor for initializing empty queue public LinkedQueue() { head = tail = null; }

@Override public void enqueue(T newEntry) { Node n = new Node(newEntry); if (isEmpty()) { head = tail = n; } else { tail.next = n; tail = n; } }

@Override public T dequeue() { //throws exception if the queue is empty if (isEmpty()) { throw new EmptyQueueException("Queue is empty!"); } T data = head.data; head = head.next; if (head == null) { tail = null; } return data; }

@Override public T getFront() { // throws exception if the queue is empty if (isEmpty()) { throw new EmptyQueueException("Queue is empty!"); } return head.data; }

@Override public boolean isEmpty() { return head == null; }

@Override public void clear() { head = tail = null; } }

Please complete palindrome.java with output screenshots! Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!