3

I'm having trouble understanding how to store objects I pop from a stack as variables.

For example:

A stack consists of numbers: 3 , 4 , 5 [3 being on the top of the stack]

**I push my integers into my char[] array called postfix.

***When I pop() I want to be able to set it to a variable (x). so x = 3

**** After pop : 4 , 5

*****Then when I pop() again I want to set that as another variable(y). y = 4

*****The reason why I want to set them as variables is because I plan on using them to evaluate an equation like : x + y , x - y, etc. and return the result of the two on to the top of the stack. I'm sorry but I'm really lost.

Generic Node 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;
}

Generic 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);
  }


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

Interface:

   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
Xirol
  • 303
  • 4
  • 10
  • It seems you need an array again to store all the popped elements when ever they are. – SacJn Oct 23 '15 at 06:49
  • So one array for holding the pushed integers and one to hold the pop'd integers? – Xirol Oct 23 '15 at 06:51
  • No I won't suggest but its just that you need to perform + , - for two popped elements then manipulation is possible or else I am afraid you will require a data structure again – SacJn Oct 23 '15 at 06:54

2 Answers2

2

You will have to perform specific operations with those objects you pop. Like sum or substraction or multiplication.
Those operations have a fixed number of operands. 2 for sum in example. So you just store each object in a variable, 2 variables in the sum case, perform the operation and push the result.

In example. Let's assume you have implemented these methods elsewhere

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

Let's assume you wish to evaluate the expression 5 + 4 * 3.
You would do so with this code :

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

The implementation for executeMultiplication would be

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

And for executeSum it would be a similar implementation.
As you can see the poped values in executeMultiplication are stored in variables a and b. The result of the multiplication in variable c. And finally that result is pushed back to the stack so it can be used (poped) by the next operation.

Anonymous Coward
  • 3,140
  • 22
  • 39
0

First create the stack and fill it with numbers

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

Then take each pair of numbers from the stack and do your calculations

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
}
gdros
  • 393
  • 2
  • 10