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
//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
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
//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
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
Get step-by-step solutions from verified subject matter experts
