title: Iterative Inorder Traversal
body: <p>Here is my function for iterative inorder traversal.
  But when I execute it I'm getting a segmentation fault.
  I'm using a stack for the traversal. In the given program I also have a recursive function for inorder traversal to check if my create() function is working.</p>
  
  <p>I'm pushing the node to the stack and moving to the left of the node and after that I'm popping the node from the stack and printing it and going to the right by doing
  <code>root=root-&gt;rlink</code>.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  typedef struct node
  {
  int data;
  struct node *llink;
  struct node *rlink;
  }Node;
  typedef struct Stack
  {
  Node *a[10];
  int top;
  }stack;
  void push(stack *s,Node *root)
  {
  if(s-&gt;top==9)
      printf("FULL");
  else
  {
      s-&gt;top++;
      s-&gt;a[s-&gt;top]=root;
  }
  }
  Node *pop(stack *s)
  {
      if(s-&gt;top==-1)
          printf("Empty");
      return s-&gt;a[s-&gt;top--];
  }
  void inorder(Node *root)
  {
      stack s;
      s.top=-1;
      int flag=1;
      while(flag)
      {
      if(s.top!=9)
      {
          push(&amp;s,root);
          root=root-&gt;llink;
      }
      else{
          if(s.top!=-1)
          {
              root=pop(&amp;s);
              printf("%d",root-&gt;data);
              root=root-&gt;rlink;
          }
          else
              flag=0;
      }
      }
  }
  void inor(Node *root)
  {
  if(root!=NULL)
  {
  inor(root-&gt;llink);
  printf("%d",root-&gt;data);
  inor(root-&gt;rlink);
  }
  }
  Node *create(Node *root,int key)
  {
  if(root==NULL)
  {
  root=(Node *)malloc(sizeof(Node));
  root-&gt;data=key;
  root-&gt;rlink=root-&gt;llink=NULL;
  }
  else
  {
  if(key&gt;root-&gt;data)
  {
      root-&gt;rlink=create(root-&gt;rlink,key);
  }
  else if(key&lt;root-&gt;data)
  {
  root-&gt;llink=create(root-&gt;llink,key);
  }
  }
  return root;
  }
  int main()
  {
      Node *h=NULL;
      h=create(h,5);
      h=create(h,1);
      h=create(h,3);
      h=create(h,8);
      h=create(h,12);
      h=create(h,51);
      inorder(h);
      //inor(h);
  }
  </code></pre>