Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reverse Polish Notation with Linked List in Java For this assignment, you are to

ID: 3885477 • Letter: R

Question

Reverse Polish Notation with Linked List in Java

For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user.

You must use a linked list to maintain the stack for this program (array implementations of the stack will not receive full credit).

You must handle the following situations (errors):
Too many operators (+ - / *)
Too many operands (doubles)
Division by zero

The program will take in a Polish expression that separates the operators and operands by a single space, and terminates the expression with an equals sign.

The program will continue to take and evaluate expressions until the user enters a zero (0) on a line by itself followed by a new line.

Your sample output should show the handling of all the error conditions as well as make use of all of the operators.

Sample IO: (note: formatting of output isn’t a critical issue)
Input Output
10 15 + = 25
10 15 - = -5
2.5 3.5 + = 6 (or 6.0)
10 0 / = Error: Division by zero
10 20 * / = Error: Too many operators
12 20 30 / = Error: Too many operands
-10 -30 - = 20
100 10 50 25 / * - -2 / = -40

Explanation / Answer

. I'm trying to write my own LinkedList class and then implement it in Reverse Polish Calculator.

import java.util.Scanner;

public class ReversePolish

{

public static final String[] OPS = { "+", "-", "*", "/" };

public static boolean isOp(String s)

{

for(String op : OPS)

if(op.equals(s)) return true;

return false;

}

public static Double operation(String op, Double num1, Double num2)

{

if(op.equals("+")) return num1+num2;

else if(op.equals("-")) return num2-num1;

else if(op.equals("*")) return num1*num2;

else if(op.equals("/")) return num2/num1;

else System.out.println("Invalid operator");

return -999.999;

}

public static Double evaluate(String[] input)

{

LinkedList<Double> stack = new LinkedList<Double>();

for(String s : input)

{

if(isOp(s))

{

double num1 = stack.get(1);

double num2 = stack.get(2);

stack.remove(1);

stack.remove(2);

stack.add(operation(s, num1, num2));

}

else

stack.add(Double.parseDouble(s));

}

return stack.get(1);

}

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter numbers and operations: ");

String in = sc.nextLine();

System.out.println("Result: " + evaluate(in.split(" ")));

sc.close();

}

}

public class LinkedList<T>

{

private Node<T> head;

private int listCount;

public LinkedList()

{

head = new Node<T>(null);

listCount = 0;

}

public void add(T data)

{

Node<T> temp = new Node<T>(data);

Node<T> current = head;

while(current.getNext() != null)

current = current.getNext();

current.setNext(temp);

listCount++;

}

public void add(T data, int index)

{

Node<T> temp = new Node<T>(data);

Node<T> current = head;

for(int i = 1; i < index && current.getNext() != null; i++)

current = current.getNext();

temp.setNext(current.getNext());

current.setNext(temp);

listCount++;

}

public T get(int index)

{

if(index <= 0) return null;

Node<T> current = head.getNext();

for(int i = 1; i < index; i++)

{

if(current.getNext() == null) return null;

current = current.getNext();

}

return current.getData();

}

public boolean remove(int index)

{

if(index < 1 || index > size()) return false;

Node<T> current = head;

for(int i = 1; i < index; i++)

{

if(current.getNext() == null) return false;

}

current.setNext(current.getNext().getNext());

listCount--;

return true;

}

public int size() { return listCount; }

public String toString()

{

Node<T> current = head.getNext();

String output = "";

while(current != null)

{

output += "[" + current.getData().toString() + "]";

current = current.getNext();

}

return output;

}

private class Node<T>

{

Node<T> next;

T data;

public Node(T tData)

{

next = null;

data = tData;

}

public Node(T tData, Node<T> tNext)

{

next = tNext;

data = tData;

}

public Node<T> getNext() { return next; }

public void setNext(Node<T> tNext) { next = tNext; }

public T getData() { return data; }

public void setData(T tData) { data = tData; }

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote