Results 1 to 1 of 1
  1. #1
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default Need help please implementing a stack!

    I wonder why there is not even a single reply to my thread?!!:confused:

    I need to implement a stack that converts infix arithmetic exp. to prefix.
    I already implemented a stack class that has push, pop, peek and such methods. I made another class InToPre, below are my constructors:

    I already have the algorithm (but I'm still stuck!):

    If (operator stack is empty || operator > top of stack )
    Push operator on stack

    If ( operator < = top of stack || ( )
    Concatenate operand
    Push operand to operand stack
    While ( operator > top stack // stack is empty )
    Push operator to operator stack
    Case ) :
    Concatenate operand
    Push operand to operand stack
    Until (

    While (operator stack is not empty)
    Concatenate operand
    Push operand to operand stack
    Pop operand stack and write to output

    Algorithm Concatenate operand:
    ( the operator given )
    Pop right operand
    Pop left operand
    Operand = operator + left operand + right operand
    The problem is that I don't know how to check the priority of operands and in the concatenate operand, how to realize which one is left and which one is right?
    Please give me some pointers to get going with this. your help is much appreciated :)
    Below is what I have in my class InToPre:

    public InToPre(String in) // default constructor
    input = in;
    int stackSize = input.length();
    operatorStack = new StackInToPre(stackSize);
    operandStack = new StackInToPre(stackSize);
    public String InToPre() // another constructor
    for(int j=0; j<input.length(); j++) // for each char
    char ch = input.charAt(j); // get it

    if (Character.isDigit(ch)){
    operandStack.push(ch); // push it to the operator stack

    if (ch == '('){

    if (isOperand(ch)){ // I already wrote the method isOperand

    if ( !operandStack.isEmpty() || ch )
    } // ?????
    Last edited by sfe23; 02-24-2009 at 11:25 PM. Reason: Did not get a reply!

Similar Threads

  1. Implementing a Stack Using two Queues
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 10:28 AM
  2. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 03:33 PM
  3. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 06:42 PM
  4. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts