JAVA。我正试图创建一个LinkedListStack来求解一个数学表达式。例如:(1+2)*4-3。不同的变量类型说它们是不兼容的,我遇到了麻烦。我写的pop和peek方法有一个问题,我不知道如何让它们发挥作用。我不能使用Java LinkedList或Stack类,所以我必须编写LLNode类和LinkedList类。以下是我得到的错误:
Line 120:method Peek in class LinkedList<T> cannot be applied to given types; required: Character. found: no arguments. reason: actual and formal argument lists differ in length. where T is a type-variable: T extends Object declared in class LinkedList
Line 121:')' expected. not a statement. method Peek in class LinkedList<T> cannot be applied to given types; required: Character. found: no arguments. reason: actual and formal argument lists differ in length. where T is a type-variable: T extends Object declared in class LinkedList
Line 122:';' expected method Peek in class LinkedList<T> cannot be applied to given types; required: Character. found: no arguments. reason: actual and formal argument lists differ in length. where T is a type-variable: T extends Object declared in class LinkedList
Line 134:method Peek in class LinkedList<T> cannot be applied to given types; required: Character. found: no arguments. reason: actual and formal argument lists differ in length. where T is a type-variable: T extends Object declared in class LinkedList
Line 135:method Peek in class LinkedList<T> cannot be applied to given types; required: Character. found: no arguments. reason: actual and formal argument lists differ in length. where T is a type-variable: T extends Object declared in class LinkedList
Line 153: method Peek in class LinkedList<T> cannot be applied to given types; required: Character. found: no arguments. reason: actual and formal argument lists differ in length. where T is a type-variable: T extends Object declared in class LinkedList
Line 175:incompatible types: void cannot be converted to int
Line 183:incompatible types: void cannot be converted to char
Line 185:incompatible types: void cannot be converted to int
Line 187:incompatible types: void cannot be converted to int
以下是我迄今为止所拥有的:事先非常感谢。
import java.util.*;
//class for linked list node
class LLNode<T>
{
protected T info; //create a variable info of object T
protected LLNode<T> link; //create a variable link of object LLNode<T>
//constructor
public LLNode(T info)
{
this.info=info; //initialize info to current object
link=null; //set the link to null
}
//method to put info into the node
public void setInfo(T info)
{
this.info=info; //initialize info to current object
}
//method to get info from the node
public T getInfo()
{
return info; //returns info
}
//method to link node to the next node
public void setLink(LLNode<T> link) //method to link nodes together into a list
{
this.link=link; //link node to current object
}
//method to get the link from the node
public LLNode<T> getLink()
{
return link; //returns the link
}
}
//class for linked list
class LinkedList<T>
{
protected LLNode<T> top; //reference to the top of the stack
//method to check if the stack is empty
public boolean isEmpty()
{
return(top==null); //sets the top to null
}
//method to check if the stack is full
public boolean isFull()
{
return false; //false since a linked list stack cannot be full
}
//LinkedList method
public LinkedList()
{
top=null; //sets the top to null
}
//method to push an element onto the stack
public void push(T element)
{
LLNode<T> newNode=new LLNode<T>(element); //create newNode of object LLNode<T>
newNode.setLink(top); //links new node to the top element
top=newNode; //sets the top to the new node
}
//method to remove an element from the stack
public void pop()
{
if(isEmpty()) //check if stack is empty
throw new EmptyStackException(); //throws exception if stack is empty
else
top=top.getLink(); //sets the top to the next element
}
//method to peek at the next element without removing it
public T peek(T element)
{
element=top.getInfo(); //checks the info in the top element
return element; //returns the info
}
}
//class to evaluate the expression
public class EvaluateExpression
{
//main method
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in); //create new Scanner object
//ask the user to enter the expression
System.out.println("Enter the expression with spaces between each "
+ "character entered: ");
//set info user entered to the expression variable
String expression=keyboard.nextLine();
//call the evaluateExpression method and display result to user
System.out.println("The result of the expression: " + expression +
" is: " + evaluateExpression(expression));
}
//method to evaluate the expression
public static int evaluateExpression(String expression)
{
//create a new LinkedList for the operands
LinkedList<Integer> operandStack=new LinkedList<>();
//create a new LinkedList for the operators
LinkedList<Character> operatorStack=new LinkedList<>();
//create an array to hold the values the user entered by splitting the String
//into individual operators and operands
String[] elements=expression.split("\s+");
//for loop to go through elements
for(String element : elements)
{
//if there is a blank space then continue to the while loop
if(element.length()==0)
continue;
//checks if the element is a + or - operator
else if(element.charAt(0)=='+'|| element.charAt(0)=='-')
{
//while the stack is not empty and the element is a operator
while(!operatorStack.isEmpty() && (operatorStack.peek() == '+' ||
operatorStack.peek()--'-' || operatorStack.peek() == '*'
|| operatorStack.peek() == '/'))
{
//call the calulcate method to process operators
calculate(operandStack, operatorStack);
}
//push the operator onto the operator stack
operatorStack.push(element.charAt(0));
}
//checks if the element is a * or / operator
else if(element.charAt(0)=='*' || element.charAt(0)=='/')
{
//while the stack is not empty and the element is a * or /
while(!operatorStack.isEmpty() && (operatorStack.peek() == '*'
|| operatorStack.peek() == '/'))
{
//call the calculate method to process * or / operators
calculate(operandStack, operatorStack);
}
//push the operator to the operator stack
operatorStack.push(element.charAt(0));
}
//check if the element is a ( operator
else if (element.charAt(0)=='(')
{
//pushes the ( onto the operator stack
operatorStack.push('(');
}
//check if the element is a ) operator
else if(element.charAt(0)==')')
{
//while the operator is not (
while(operatorStack.peek() !='(')
{
//call calculate method to process operators until ( operator
calculate(operandStack, operatorStack);
}
//pop ( operator from the stack
operatorStack.pop();
}
//check if the element is an operand
else
{
//push the operand to the operand stack
operandStack.push(new Integer(element));
}
}
//check if the stack is not empty
while(!operatorStack.isEmpty())
{
//call the calculate method to process operators
calculate(operandStack, operatorStack);
}
//return the final result
return operandStack.pop();
}
//method to calculate the operator and the 2 operands on the top of the stack
public static void calculate(LinkedList<Integer> operandStack,
LinkedList<Character> operatorStack)
{
//declare a variable ch and initialize to call pop method
char ch = operatorStack.pop();
//declare a variable value1 and initialize to call pop method
int value1 = operandStack.pop();
//declare a variable value2 and initialize to call pop method
int value2 = operandStack.pop();
//if element is +
if (ch == '+')
//add the 2 values
operandStack.push(value2 + value1);
//if element is -
else if (ch == '-')
//subtract the 2 values
operandStack.push(value2 - value1);
//if element is *
else if (ch == '*')
//multiply the 2 values
operandStack.push(value2 * value1);
//if element is /
else if (ch == '/')
//divide the 2 values
operandStack.push(value2 / value1);
}
}
如果我修复了代码中的明显问题,它就会运行,并且不会给我任何错误。它甚至似乎为我尝试的简单表达提供了正确的答案。
我所做的主要更改是修复您的peek
和pop
方法。固定版本如下:
public T pop()
{
if(isEmpty()) //check if stack is empty
throw new EmptyStackException(); //throws exception if stack is empty
LLNode<T> r = top;
top=top.getLink();
return r.getInfo();
}
public T peek()
{
T element=top.getInfo(); //checks the info in the top element
return element; //returns the info
}
我还修复了一个你有--
的地方,而你显然想要==
。
在进行了这些简单的更改之后,您的代码运行得非常好。以下是一些运行示例:
Enter the expression with spaces between each character entered:
3 * 5
The result of the expression: 3 * 5 is: 15
Enter the expression with spaces between each character entered:
2 * 3 + 4
The result of the expression: 2 * 3 + 4 is: 10
Enter the expression with spaces between each character entered:
12 - 2 * 4
The result of the expression: 12 - 2 * 4 is: 4
你仍然有一些问题,比如在上面两个方法中的一个方法中,你不检查空堆栈,但在另一个方法里你检查。但总的来说,这是一项不错的工作!
我认为问题与您对堆栈本身的思考有关。
运算符没有一个堆栈,操作数没有另一个堆栈。
这是整件事的一堆。