Question: Java Program: Use the program below to start coding Instantiate a stack of characters and implement the following functions : 1- isPalindrome: Checks if a

Java Program: Use the program below to start coding

Instantiate a stack of characters and implement the following functions:

1- isPalindrome: Checks if a given string is a palindrome or not.

2- PrintSameOrder: prints the stack contents in the same order in which they were pushed into it.

3- Screenshot your output.

1. StackInterface

public interface StackInterface { public void push(T entry); public T pop(); public T peek(); public boolean isEmpty(); public void popN(int n); }

2. NodeStack.java

public class NodeStack implements StackInterface { private Node topNode; public NodeStack() { topNode = null; } @Override public void push(T entry) { Node newNode = new Node(entry, topNode); topNode = newNode; } @Override public T pop() { T top = peek(); if (topNode != null) { topNode = topNode.getNext(); } return top; } @Override public T peek() { T top = null; if (topNode != null) { top = topNode.getItem(); } return top; } @Override public boolean isEmpty() { return topNode == null; } class Node { public T getItem() { return item; } public void setItem(T item) { this.item = item; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } T item; Node next; Node(T item) { this(item, null); } Node(T item, Node next) { this.item = item; this.next = next; } @Override public String toString() { return item.toString(); } } @Override public void popN(int n) { } } 

3. NodeStackTest.java

public class NodeStackTest { static void printStack(NodeStack s) { NodeStack backup = new NodeStack<>(); System.out.println("Stack contents:"); while (!s.isEmpty()) { int t = (Integer)s.pop(); backup.push(t); System.out.print(t + " "); } System.out.println(); while(!backup.isEmpty()) s.push(backup.pop()); } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int choice = -1; NodeStack stk = new NodeStack<>(); do { System.out.println("[1] To push an item into the stack"); System.out.println("[2] To pop an item from the stack"); System.out.println("[3] To print the top of stack"); System.out.println("[4] To check of stack is empty"); System.out.println("[5] To print bag contents"); System.out.println("[6] To Exit"); System.out.println("Enter your choice:"); Scanner in = new Scanner(System.in); choice = in.nextInt(); switch (choice) { case 1: System.out.println("Enter an item"); int num = in.nextInt(); stk.push(num); break; case 2: if (!stk.isEmpty()) { stk.pop(); } break; case 3: if (!stk.isEmpty()) { System.out.println("Top of the stack is " + stk.peek()); } else { System.out.println("Stack is empy "); } break; case 4: if (stk.isEmpty()) { System.out.println("Stack is empty"); } else { System.out.println("Stack is not empty"); } break; case 5: printStack(stk); break; case 6: System.out.println("GoodBye!"); break; default: System.out.println("Invalid choice! Enter a number in the range [1-8]"); break; } } while (choice != 6); } }

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!