将 pop() 的对象存储为变量

Store pop()'d objects as variables

我无法理解如何将从堆栈中弹出的对象存储为变量。

例如:

堆栈由数字组成:3、4、5 [3 在堆栈的顶部]

**我将整数放入名为 postfix 的 char[] 数组中。

***当我 pop() 时,我希望能够将它设置为一个变量 (x)。所以 x = 3

**** 弹出后:4、5

*****然后当我再次弹出()时,我想将其设置为另一个变量(y)。 y = 4

*****我想将它们设置为变量的原因是因为我计划使用它们来计算方程式,例如:x + y、x - y 等,以及 return两者的结果放在栈顶。对不起,我真的迷路了。

通用节点Class:

public class Node<T> {

// data fields (reference variables)
// data stores an object of any class
private T data;

// next points to the next node
private Node<T> next;

/**
 * Constructor - Used To Create Each Object & Initialize DAta Fields.
 * 
 * @param data2
 *            initializes the data reference variable.
 * @param next2
 *            initializes the next reference variable..
 */
public Node(T data2, Node<T> next2) {
    data = data2;
    next = next2;
}


public T getData() {
    return data;
}

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

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

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

通用 LinkedStack:

import java.util.EmptyStackException;

public class LinkedStack<T> implements StackInterface<T> {


  private Node<T> top = null;

   public LinkedStack() {
     //data fields already initialized 
  } 


   public boolean empty() {
     return top == null;
  }


   public T peek() throws EmptyStackException {
   //check to see if empty
     if(this.empty()){
        throw new EmptyStackException();
     }          
     return top.getData();
  } 

   public T pop() {  
     if(this.empty()){
        throw new EmptyStackException();
     }
     Node<T> node = top;        
     top = top.getNext();           
     return node.getData();
  }

   public void push(T item) {     
     top = new Node<T>(item, top);
  }


  ***********************************************************

接口:

   import java.util.EmptyStackException;

   public interface StackInterface<T>{

   /**Tests if the stack is empty
    * @return true/false if empty/not empty */
       public boolean empty();

   /**Looks at the object at the top of the stack 
   * without removing it from the stack.
   * @return the address to the top item on the stack 
   * @exception EmptyStackException if the stack is empty*/
   public T peek() throws EmptyStackException;

   /**Removes the object at the top of stack 
   * and returns the address of this object
   * @return the address to the top item on the stack 
   * @exception EmptyStackException if the stack is empty*/
   public T pop() throws EmptyStackException;    

   /**Pushes an item onto the top of this stack 
   * @param item the item that is pushed on the stack */
   public void push(T item);

   }//end interface

您将必须对弹出的对象执行特定操作。喜欢加法或减法或乘法。
这些操作具有固定数量的操作数。 2 例如总和。所以你只需将每个对象存储在一个变量中,在求和的情况下是 2 个变量,执行操作并推送结果。

例如。假设您已经在别处实现了这些方法

public static <T> T sum( T a, T b);
public static <T> T multiplication( T a, T b);

假设您希望计算表达式 5 + 4 * 3。
您可以使用以下代码执行此操作:

  LinkedStack<Integer> stack = new LinkedStack<>();
  stack.push( 5 );
  stack.push( 4 );
  stack.push( 3 );
  executeMultiplication( stack );
  executeSum( stack );
  int result = stack.pop();

executeMultiplication 的实现是

public static <T> void executeMultiplication( LinkedStack<T> stack )
{
  T a = stack.pop();
  T b = stack.pop();
  T c = multiplication( a, b );
  stack.push( c );
}

对于 executeSum,这将是一个类似的实现。
如您所见,executeMultiplication 中弹出的值存储在变量 a 和 b 中。变量 c 的乘法结果。最后,该结果被推回堆栈,以便下一个操作可以使用(弹出)。

首先创建堆栈并用数字填充它

LinkedStack<Integer> stack=new LinkedStack<Integer>();
//stack.push(...)
//stack.push(...)
//...

然后从堆栈中取出每对数字并进行计算

try {
    int i=0;            
    for(;;) {
        if(i%2==0) {
            x=stack.pop();
        } else {
            y=stack.pop();                  
            //Here you can also calculate x+y and x-y
        }
        i++;
    }
} catch(EmptyStackException) {
    //The stack is empty
}