【问题标题】:Store pop()'d objects as variables将 pop() 的对象存储为变量
【发布时间】:2016-01-22 15:14:56
【问题描述】:

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

例如:

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

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

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

**** 弹出后 : 4 , 5

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

*****我想将它们设置为变量的原因是因为我打算使用它们来评估一个等式,例如:x + y,x - y 等,并将两者的结果返回给栈顶。对不起,我真的迷路了。

通用节点类:

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

【问题讨论】:

  • 看来你需要一个数组来存储所有弹出的元素。
  • 那么一个数组用来保存推入的整数,一个用来保存弹出的整数?
  • 不,我不建议你,但它只是你需要执行 + , - 对于两个弹出的元素然后操作是可能的,否则我担心你会再次需要一个数据结构

标签: java stack


【解决方案1】:

您必须对弹出的对象执行特定操作。就像求和、减法或乘法一样。
这些操作具有固定数量的操作数。 2 为示例中的总和。因此,您只需将每个对象存储在一个变量中,在 sum 情况下为 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 中相乘的结果。最后,该结果被推回堆栈,以便下一个操作使用(弹出)。

【讨论】:

  • 如果这听起来很愚蠢,我很抱歉,但是 T 到底是什么?
  • 它是一个类型参数。检查stackoverflow.com/questions/6008241/…
  • 我明白了。顺便说一句,非常感谢您的帮助,我将尝试实现这一点。
【解决方案2】:

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

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多