英文:
LinkedList pop and push
问题
错误的原因是否有人可以理解?我猜我混淆了堆栈和列表。在替代“pop”之后我可以使用什么?你的建议是什么?
代码:
public class PostfixHesaplama {
public static void main(String[] args) {
String giris="231*+9-";
System.out.println("后缀表达式计算:" + postfix(giris));
}
static int postfix(String giris)
{
// 创建一个堆栈
List<Integer> list1 = new LinkedList<>();
// 逐个扫描所有字符
for(int i=0; i<giris.length(); i++)
{
char c = giris.charAt(i);
// 如果扫描到的字符是操作数(这里是数字),将其压入堆栈。
if(Character.isDigit(c))
list1.add(c - '0');
// 如果扫描到的字符是运算符,从堆栈中弹出两个元素并应用运算符
else
{
int val1 = list1.remove(i);
int val2 = list1.remove(i);
switch(c)
{
case '+':
list1.add(val2 + val1);
break;
case '-':
list1.add(val2 - val1);
break;
case '/':
list1.add(val2 / val1);
break;
case '*':
list1.add(val2 * val1);
break;
}
}
}
return list1.pop();
}
<details>
<summary>英文:</summary>
Can anyone understand the cause of this error? I guess I mixed up the stack and the list. What can I use instead of "pop"? What is your suggestion?
```none
Exception in thread "main" java.lang.IndexOutOfBoundsException:
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.remove(
Code :
public class PostfixHesaplama {
public static void main(String[] args) {
String giris="231*+9-";
System.out.println("postfix evaluation: "+postfix(giris));
}
static int postfix(String giris)
{
//create a stack
List<Integer> list1=new LinkedList<>();
// Scan all characters one by one
for(int i=0;i<giris.length();i++)
{
char c=giris.charAt(i);
// If the scanned character is an operand (number here),
// push it to the stack.
if(Character.isDigit(c))
list1.add(c - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = list1.remove(i);
int val2 = list1.remove(i);
switch(c)
{
case '+':
list1.add(val2+val1);
break;
case '-':
list1.add(val2- val1);
break;
case '/':
list1.add(val2/val1);
break;
case '*':
list1.add(val2*val1);
break;
}
}
}
return list1.pop();
}
答案1
得分: 1
如果您想要执行推入和弹出操作,使用Stack会更合适。不过,我认为您的问题在于remove(i)
。您可能想要使用pop(或removeLast?)来替代。
如果您继续使用LinkedList,可以使用addLast/removeLast或addFirst/removeFirst。
英文:
If you want to push and pop you are better served by a Stack. However, I think your problem is remove(i)
. You probably want to use pop (or removeLast?) instead.
If you stay with LinkedList, use addLast/removeLast or addFirst/removeFirst.
答案2
得分: 0
你的问题出在第二次移除对象上(i),你试图从相同位置两次移除对象(注意:i没有被减小,它保持相同的值)。你应该在第二次移除时使用remove(i-1)。
当前代码段:
int val1 = list1.remove(i);
int val2 = list1.remove(i);
应改为:
int val1 = list1.remove(i);
int val2 = list1.remove(i-1);
英文:
your problem is from from 2nd remove(i). You are trying to remove object twice from the same position(Note: i is not getting decremented. Its having same value). you should use remove(i-1) for the 2nd remove.
Current Code Segement:
int val1 = list1.remove(i);
int val2 = list1.remove(i);
It should be:
int val1 = list1.remove(i);
int val2 = list1.remove(i-1);
答案3
得分: 0
尝试这个。添加LinkedList声明并使用removeLast()方法:
LinkedList<Integer> list1 = new LinkedList<>();
for (int i = 0; i < giris.length(); i++) {
char c = giris.charAt(i);
if (Character.isDigit(c)) {
list1.add(c - '0');
} else {
int val1 = list1.removeLast();
int val2 = list1.removeLast();
switch (c) {
case '+':
list1.add(val2 + val1);
break;
case '-':
list1.add(val2 - val1);
break;
case '/':
list1.add(val2 / val1);
break;
case '*':
list1.add(val2 * val1);
break;
}
}
}
return list1.removeLast();
英文:
Try this. Add LinkedList declaration and use removeLast():
LinkedList<Integer> list1 = new LinkedList<>();
for (int i = 0; i < giris.length(); i++) {
char c = giris.charAt(i);
if (Character.isDigit(c)) {
list1.add(c - '0');
} else {
int val1 = list1.removeLast();
int val2 = list1.removeLast();
switch (c) {
case '+':
list1.add(val2 + val1);
break;
case '-':
list1.add(val2 - val1);
break;
case '/':
list1.add(val2 / val1);
break;
case '*':
list1.add(val2 * val1);
break;
}
}
}
return list1.removeLast();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论