Я пытаюсь преобразовать инфикс в постфикс, используя ADT в формате двойного связанного списка, но продолжаю получать разные ошибки для разных строковых входов для преобразования. Если на это ответили, я прошу прощения. Я искал бесконечно, и мне не повезло понять, что я делаю неправильно (что я уверен, это много). Спасибо за любую помощь, спасибо.
import java.util.Scanner;
public class DoublyLinkedStack<T> implements StackInterface<T> {
private double value;
private int numberOfElements = 0;
private Node<T> head;
private Node<T> tail;
private class Node<T>
{ private T data;
private Node<T> prevNode = null;
private Node<T> nextNode = null;
public Node(T data) { this.data = data; }
public Node(T data, Node<T> prevNode, Node<T> nextNode)
{ this.data = data;
this.prevNode = prevNode;
this.nextNode = nextNode;
}
public T getData() { return data; }
public void setData(T data) { this.data = data; }
public Node<T> getPrevNode() { return prevNode; }
public void setPrevNode(Node<T> prevNode) { this.prevNode = prevNode; }
public Node<T> getNextNode() { return nextNode; }
public void setNextNode(Node<T> nextNode) { this.nextNode = nextNode; }
}
public T peek() { return head.getData(); }
public T pop()
{ T data = head.getData();
head = head.getNextNode();
numberOfElements--;
return data;
}
public void push(T entry)
{ Node<T> anchor = new Node<T>(entry);
if (head == null)
{ head = anchor; }
else
{ anchor.setNextNode(head);
head = anchor;
numberOfElements++;
}
}
public T dataIndex(int index)
{ if (index > numberOfElements)
{ System.out.println("Index too large, will cause infinite loop.");
return null;
}
else
{ tail = head;
for (int i = 0; i < index; i++)
{ tail = tail.getNextNode(); }
return tail.getData();
}
}
public int getCurrentSize() { return numberOfElements; }
public boolean order(String first, String second)
{ switch (first)
{ case "+":
case "-": { return false; }
case "*":
switch (second)
{ case "+":
case "-":
case "/": { return true; }
default: { return false; }
}
case "^":
switch (second)
{ case "+":
case "-":
case "*":
case "/": { return true; }
default: { return false; }
}
default: { return false; }
}
}
public double postfixCalculator(String input)
{ double first;
double second;
double finish;
DoublyLinkedStack<Double> calc = new DoublyLinkedStack<Double>();
String[] split = input.split(" ");
int i = 0;
String simpson;
while (i < split.length)
{ simpson = split[i];
switch(simpson)
{ case "+":
{ first = calc.pop();
second = calc.pop();
finish = first + second;
calc.push(finish);
break;
}
case "-":
{ first = calc.pop();
second = calc.pop();
finish = second - first;
calc.push(finish);
break;
}
case "*":
{ first = calc.pop();
second = calc.pop();
finish = first * second;
calc.push(finish);
break;
}
case "/":
{ first = calc.pop();
second = calc.pop();
finish = second/first;
calc.push(finish);
break;
}
case "^":
{ first = calc.pop();
second = calc.pop();
finish = Math.pow(second, first);
calc.push(finish);
break;
}
default: { calc.push(Double.parseDouble(simpson)); }
i++;
}
} return calc.peek();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
String expression;
String postfix = null;
Scanner console = new Scanner(System.in);
System.out.println("Enter Desired Expression:");
expression = console.nextLine();
String[] split = expression.split(" ");
DoublyLinkedStack<String> stack = new DoublyLinkedStack<String>();
for (int i = 0; i < split.length; i++) {
if (postfix == null) {
postfix = ("" + split[i]);
} else {
postfix = postfix + " " + split[i];
}
if (split[i].equals(")")) {
while (!stack.peek().equals("(")) {
postfix = postfix + " " + stack.pop();
}
stack.pop();
} else if (split[i].equals("]")) {
while (!stack.peek().equals("[")) {
postfix = postfix + " " + stack.pop();
}
stack.pop();
} else if (split[i].equals("}")) {
while (!stack.peek().equals("{")) {
postfix = postfix + " " + stack.pop();
}
stack.pop();
} else {
if (stack.getCurrentSize() == 0) {
stack.push(split[i]);
} else {
while (stack.order(stack.dataIndex(stack.getCurrentSize() - i), split[i]) == true) {
postfix = postfix + " " + stack.pop();
}
stack.push(split[i]);
}
}
}
while (stack.getCurrentSize() != 1) {
postfix = postfix + " " + stack.pop();
}
System.out.println("Postfix:" + postfix + "\nResult:" + stack.postfixCalculator(postfix));
}
}
public interface StackInterface <T> {
public void push (T newEntry);
public T pop();
public T peek();
public boolean isEmpty();
public void clear();
}