title: Infinite loop in iterative preorder traversal of BST
body: <p>I am trying to iteratively traverse a BST in preorder, but this function is getting stuck in an infinite loop while printing. Could someone please tell me why this is happening?</p>
  
  <pre><code>1 typedef struct node_{
  2     int data;
  3     struct node_* left;
  4     struct node_* right;
  5 }BST; 
  
  22 void preorder(BST* tree){
  23     if(tree==NULL){
  24         return;
  25     }
  26     // create stack 
  27     stack* stack=create_stack();
  28     // push data onto stack
  29     push(stack,tree-&gt;data);
  30     while(isEmpty(stack)==0){
  31         struct node_ *node;
  32         node-&gt;data=top(stack);
  33         printf("%d ",node-&gt;data);
  34         // pop value off stack
  35         pop(stack);
  36         if(node-&gt;right!=NULL){
  37             //push right child onto stack
  38             push(stack,node-&gt;right-&gt;data);
  39         }
  40         if(node-&gt;left!=NULL){
  41             // push left child onto stack
  42             push(stack,node-&gt;left-&gt;data);
  43         }
  44     }
  45 }
  </code></pre>