链表pop和push

huangapple go评论81阅读模式
英文:

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 &quot;pop&quot;? What is your suggestion?

```none
Exception in thread &quot;main&quot; 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=&quot;231*+9-&quot;; 
    System.out.println(&quot;postfix evaluation: &quot;+postfix(giris));    
    
}
static int postfix(String giris) 
{ 
    //create a stack 
    List&lt;Integer&gt; list1=new LinkedList&lt;&gt;(); 
      
    // Scan all characters one by one 
    for(int i=0;i&lt;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 - &#39;0&#39;); 
          
        //  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 &#39;+&#39;: 
                list1.add(val2+val1); 
                break; 
                  
                case &#39;-&#39;: 
                list1.add(val2- val1); 
                break; 
                  
                case &#39;/&#39;: 
                list1.add(val2/val1); 
                break; 
                  
                case &#39;*&#39;: 
                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&lt;Integer&gt; list1 = new LinkedList&lt;&gt;();

for (int i = 0; i &lt; giris.length(); i++) {
  char c = giris.charAt(i);

  if (Character.isDigit(c)) {
    list1.add(c - &#39;0&#39;);
  } else {
    int val1 = list1.removeLast();
    int val2 = list1.removeLast();

    switch (c) {
      case &#39;+&#39;:
        list1.add(val2 + val1);
        break;

      case &#39;-&#39;:
         list1.add(val2 - val1);
         break;

      case &#39;/&#39;:
        list1.add(val2 / val1);
        break;

      case &#39;*&#39;:
        list1.add(val2 * val1);
        break;
    }
  }
}
return list1.removeLast();

huangapple
  • 本文由 发表于 2020年4月8日 02:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定