title: Why does this conversion of infix to postfix notation using stack as an ADT crash?
body: <pre><code>#include&lt;iostream&gt;
  #include&lt;cstring&gt;
  using namespace std;
  #define MAX 5
  struct node
  {
      char data;
      struct node *next;
  };
  class stack
  {
      public:
          node *top;
          stack()         //constructor
          {
              top=NULL;
          }
          char Top()     //return top element without popping
          {
              return(top-&gt;data);
          }
          int empty()     //check empty
          {
              if(top==NULL)
                  return(1);
              return(0);
          }
          void push(char x)     //push function
          {
              node *p;
              p=new node;
              p-&gt;data=x;
              p-&gt;next=top;
              top=p;
          }
          char pop()         //pop function
          {
              node *p;
              char x;
              p=top;
              top=top-&gt;next;
              x=p-&gt;data;
              delete(p);
              return(x);
          }
          int precedence(char x)   //check precedence of operators
          {
              if(x=='(')
                  return 0;
              else if(x=='+'||x=='-')
                  return 1;
              else if(x=='*'||x=='/'||x=='%')
                  return 2;
              return 3;
          }
          void infix_to_postfix(char infix[],char postfix[]);
  };
  void infix_to_postfix(char infix[],char postfix[])
  {
      stack s;
      char x,token;
      int i,j=0;
      for(i=0;infix[i]!='\0';i++)
      {
          token=infix[i];
          if(isalnum(token))
              postfix[j++]=token;
          else
              if(token=='(')
                  s.push(token);
              else
                  if(token==')')
                  {
                      while((x=s.pop())!='(')
                          postfix[j++]=x;
                  }
  
                  else
                  {
                      while(s.precedence(token)&lt;=s.precedence(s.Top())&amp;&amp; !s.empty())
                          postfix[j++]=s.pop();
                      s.push(token);
                  }
      }
      while(!s.empty())
      {
          x=s.pop();
          postfix[j++]=x;
      }
      postfix[j++]='\0';
  }
  int main()
  {
      char infix[30],postfix[30];
      cout&lt;&lt;"\nEnter the infix expression::\n";
      cin&gt;&gt;infix;
      infix_to_postfix(infix,postfix);
      cout&lt;&lt;"/nThe equivalent postfix expression is::"&lt;&lt;postfix;
      return 0;
  }
  </code></pre>
  
  <p>The above code is giving segmentation fault with gedit in Ubuntu...whereas when the same code when run with turbo C in Windows with the required changes as follows...runs correctly...</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  #include&lt;iostream.h&gt;
  #include&lt;string.h&gt;
  #include&lt;ctype.h&gt;
  #include&lt;stdlib.h&gt;
  #define MAX 5
  struct node
  {
      char data;
      struct node *next;
  };
  class stack
  {
      public:
          node *top;
          stack()         //constructor
          {
              top=NULL;
          }
          char Top()     //return top element without popping
          {
              return(top-&gt;data);
          }
          int empty()     //check empty
          {
              if(top==NULL)
                  return(1);
              return(0);
          }
          void push(char x)     //push function
          {
              node *p;
              p=new node;
              p-&gt;data=x;
              p-&gt;next=top;
              top=p;
          }
          char pop()         //pop function
          {
              node *p;
              char x;
              p=top;
              top=top-&gt;next;
              x=p-&gt;data;
              delete(p);
              return(x);
          }
          int precedence(char x)   //check precedence of operators
          {
              if(x=='(')
                  return 0;
              else if(x=='+'||x=='-')
                  return 1;
              else if(x=='*'||x=='/'||x=='%')
                  return 2;
              return 3;
          }
          void infix_to_postfix(char infix[],char postfix[]);
  };
  void infix_to_postfix(char infix[],char postfix[])
  {
      stack s;
      char x,token;
      int i,j=0;
      for(i=0;infix[i]!='\0';i++)
      {
          token=infix[i];
          if(isalnum(token))
              postfix[j++]=token;
          else
              if(token=='(')
                  s.push(token);
              else
                  if(token==')')
                  {
                      while((x=s.pop())!='(')
                          postfix[j++]=x;
                  }
  
                  else
                  {
                      while(s.precedence(token)&lt;=s.precedence(s.Top())&amp;&amp; !s.empty())
                          postfix[j++]=s.pop();
                      s.push(token);
                  }
      }
      while(!s.empty())
      {
          x=s.pop();
          postfix[j++]=x;
      }
      postfix[j++]='\0';
  }
  int main()
  {
      char infix[30],postfix[30];
      cout&lt;&lt;"\nEnter the infix expression::\n";
      cin&gt;&gt;infix;
      infix_to_postfix(infix,postfix);
      cout&lt;&lt;"\nEquivalent postfix expression is::"&lt;&lt;postfix;
      getch();
  return 0;
  }
  </code></pre>
  
  <p>Why is the problem arising in Ubuntu???</p>