1

Good day, everyone! I'm new in C++ (and here in stackoverflow as well) and I need help from you experts. There's something wrong with this code even if there is no error or warning. It just hangs whenever the program is being executed.

The program converts infix to postfix using linked lists (stacks).

# include <iostream>
# include <cstring>

 using namespace std;

 struct node {
    char data;
    node *next;
 };

 node *top=NULL;
 node *bottom=NULL;
 node *entry;
 node *last_entry;
 node *second_last_entry;

 void push(const char Symbol) {
    entry=new node;
    if(bottom==NULL) {
         entry->data=Symbol;
         entry->next=NULL;
         bottom=entry;
         top=entry;
    }
    else {
         entry->data=Symbol;
         entry->next=NULL;
         top->next=entry;
         top=entry;
    }
}

const char pop( ) {
    char Symbol=NULL;

    if(bottom==NULL)
        cout<<"\n\n\n\t ***  Error : Stack is empty. \n"<<endl;

    else {
        for (last_entry=bottom; last_entry->next!=NULL; last_entry=last_entry->next)
            second_last_entry=last_entry;

        if(top==bottom)
        bottom=NULL;

        Symbol=top->data;

        delete top;

        top=second_last_entry;
        top->next=NULL;
    }

    return Symbol;
}

void infix_to_postfix(const char *Infix) {
    char Infix_expression[100]={NULL};
    char Postfix_expression[100]={NULL};

    strcpy(Infix_expression,"(");
    strcat(Infix_expression,Infix);
    strcat(Infix_expression,")");

    char Symbol[5]={NULL};
    char Temp[5]={NULL};

    for(int count=0;count<strlen(Infix_expression);count++) {
        Symbol[0]=Infix_expression[count];

        if(Symbol[0]=='(')
            push(Symbol[0]);

        else if(Symbol[0]==')') {
           Symbol[0]=pop( );

           while(Symbol[0]!='(')
              {
             strcat(Postfix_expression,Symbol);

             Symbol[0]=pop( );
              }
        }

         else if(Symbol[0]=='^' || Symbol[0]=='*' || Symbol[0]=='/'
                    || Symbol[0]=='+' || Symbol[0]=='-')
        {
           if(Symbol[0]=='*' || Symbol[0]=='/')
              {
             Temp[0]=pop( );

             while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           else if(Symbol[0]=='+' || Symbol[0]=='-')
              {
             Temp[0]=pop( );

             while(Temp[0]!='(')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           push(Symbol[0]);
        }

         else
        strcat(Postfix_expression,Symbol);
      }

       cout<<"\n\n Postfix Expression : "<<Postfix_expression;
}

 int main( ) {
    char Infix_expression[100]={NULL};
    cout<<"\n\n Enter the Infix Expression : ";
    cin>>Infix_expression;
    infix_to_postfix(Infix_expression);
    return 0;
}

Please help me! I'm a newbie and I can't go far without you guys. Thank you very much!

Manishearth
  • 14,882
  • 8
  • 59
  • 76
First Lady
  • 79
  • 2
  • 3
  • 15
  • 1
    Do you know how to run your code in a debugger ? – J.N. Mar 18 '12 at 13:30
  • I'm afraid not. I'm using codeblocks by the way. – First Lady Mar 18 '12 at 13:31
  • 4
    I guess you need to learn how to debug before actually solving that problem. – J.N. Mar 18 '12 at 13:42
  • Your code seems pretty strange (at least it could certainly profit from several helper methods and restructuring). I assume you have read the [shunting yard article](http://en.wikipedia.org/wiki/Shunting_yard_algorithm#C.2B.2B_example) on wiki? – Voo Mar 18 '12 at 13:58

2 Answers2

1

I guess the help you need is to learn how to debug code using that IDE. The first thing you can try is add more printing (via cout) to see what's going on a bit more clearly and find you where the program hangs (or whether it's stuck in an infinite loop).

Here is a tutorial to show how to debug using codeblocks. Sadly I have no idea whether that will match your configuration though.

J.N.
  • 8,203
  • 3
  • 29
  • 39
  • Thank you, J.N.! But the codeblocks I'm using was just downloaded free and I can't access the build options to debug the code. – First Lady Mar 18 '12 at 13:54
  • @FirstLady You really should use an IDE that allows debugging. But looking at the wikipage for codeblocks.. it's released under GPL so I don't really understand your point. Anyhow there's also eclipse CDT (worsk fine for *nix, windows, no idea mac) and VS express for windows. – Voo Mar 18 '12 at 14:04
  • Thank you, Voo! I'll look for eclipse CDT. Thanks! :) – First Lady Mar 18 '12 at 14:10
1

For a stack you only need one stack pointer.

void push(const char Symbol) {
    entry = new node;
    entry->data = Symbol;
    entry->next = top;
    top = entry;
}


const char pop( ) {
    if (!top) {
        cout << "\n\n\n\t ***  Error : Stack is empty. \n" << endl;
        return ' ';
    }
    node* entry = top;
    top = top->next;
    char ch = entry->data;
    delete entry;
    return ch;
}

const bool is_empty() {
    return !top;
}

That should resolve some errors, but probably not all. Sometimes I have seen the same thing done with two stacks: one for operators, one for operands. I do not necessarily advise that. Rather start from ground zero, and keep the algorithm simple and abstract, maybe in pseudo-code first.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138