Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

No Output show in after running this what is wrong? import java.util.Scanner; interface StackInterface { void push ( T entry ) ; T pop (

No Output show in after running this what is wrong?
import java.util.Scanner;
interface StackInterface {
void push(T entry);
T pop();
T peek();
boolean isEmpty();
void clear();
}
class LinkedStack implements StackInterface {
private Node topNode;
public LinkedStack(){
topNode = null;
}
public void push(T newEntry){
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
}
public T pop(){
T top = peek();
if (topNode != null){
topNode = topNode.getNextNode();
}
return top;
}
public T peek(){
if (isEmpty()){
throw new IllegalStateException("Stack is empty");
}
return topNode.getData();
}
public boolean isEmpty(){
return topNode == null;
}
public void clear(){
topNode = null;
}
private class Node {
private T data;
private Node next;
private Node(T dataPortion){
this(dataPortion, null);
}
private Node(T dataPortion, Node nextNode){
data = dataPortion;
next = nextNode;
}
private T getData(){
return data;
}
private Node getNextNode(){
return next;
}
}
}
class ArrayStack implements StackInterface {
private T[] stack;
private int topIndex;
private static final int DEFAULT_CAPACITY =10;
private static final int MAX_CAPACITY =10000;
public ArrayStack(){
this(DEFAULT_CAPACITY);
}
public ArrayStack(int initialCapacity){
@SuppressWarnings("unchecked")
T[] tempStack =(T[]) new Object[initialCapacity];
stack = tempStack;
topIndex =-1;
}
public void push(T newEntry){
ensureCapacity();
topIndex++;
stack[topIndex]= newEntry;
}
public T pop(){
if (isEmpty()){
throw new IllegalStateException("Stack is empty");
}
T top = stack[topIndex];
stack[topIndex]= null;
topIndex--;
return top;
}
public T peek(){
if (isEmpty()){
throw new IllegalStateException("Stack is empty");
}
return stack[topIndex];
}
public boolean isEmpty(){
return topIndex <0;
}
public void clear(){
while (!isEmpty()){
pop();
}
}
private void ensureCapacity(){
if (topIndex >= stack.length -1){
int newLength =2* stack.length;
ensureCapacity(newLength);
}
}
private void ensureCapacity(int newLength){
if (newLength > MAX_CAPACITY){
throw new IllegalStateException("Attempt to create a stack "+
"whose capacity exceeds "+
"allowed maximum of "+ MAX_CAPACITY);
}
stack = java.util.Arrays.copyOf(stack, newLength);
}
}
public class StackMath {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an infix or postfix expression:");
String expression = scanner.nextLine().trim();
System.out.println("Infix: "+ postfix2infix(expression));
System.out.println("Postfix: "+ infix2postfix(expression));
System.out.println("Evaluation (infix): "+ evaluateInfix(expression));
System.out.println("Evaluation (postfix): "+ evaluatePostfix(expression));
}
public static String infix2postfix(String infix){
// Implement conversion from infix to postfix
return "";
}
public static String postfix2infix(String postfix){
// Implement conversion from postfix to infix
return "";
}
public static int evaluatePostfix(String postfix){
// Implement evaluation of postfix expression
return 0;
}
public static int evaluateInfix(String infix){
// Implement evaluation of infix expression
return 0;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions

Question

3. Existing organizations and programs constrain behavior.

Answered: 1 week ago