title: Enqueue function of queue using linked list in c
body: <p>I'm having a problem when using linked list to build a queue program. Here's the full code.</p>
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define ERROR_VALUE -300000
  
  typedef struct LinkedNode {
      int data;
      struct LinkdedNode* link;
  }Node;
  Node* front;
  Node* rear;
  
  void init_queue() { front = rear = NULL; }
  int is_empty() { return (front = NULL &amp;&amp; rear == NULL); }
  
  
  int size() {
      Node* p; 
      int count = 0;
      if (is_empty())
          return 0; 
      for (p = front; p != rear; p = p-&gt;link) {
          count++;
          return count + 1; 
      }
  }
  void enqueue(int e) { 
      Node* p = (Node*)malloc(sizeof(Node));
      p-&gt;data = e;
      p-&gt;link = NULL; 
      if (is_empty())
          front = rear = p;
      else {
          rear-&gt;link = p;
          rear = p;
      }
  }
  int dequeue() { 
      Node* p = front; 
      int e;
      if (is_empty()) {
          printf(&quot;Queue Empty Error!\n&quot;);
          return ERROR_VALUE;
      }
      else if (size() == 1) {
          front = rear = NULL;
      }
      else
          front = p-&gt;link;
      e = p-&gt;data;
      free(p);
      return e;
  }
  int peek() { 
      if (is_empty()) {
          printf(&quot;Queue Empty Error!\n&quot;);
          return ERROR_VALUE;
      }
      return front-&gt;data;
  }
  
  void print_queue() {
      Node* p;
      printf(&quot;QUEUE STATUS: size=%d\n&quot;, size());
      if (is_empty())
          return;
      for (p = front; p != NULL; p = p-&gt;link)
          printf(&quot;[%2d] &quot;, p-&gt;data);
      printf(&quot;\n&quot;);
  }
  int main(void) {
      int val, sel;
  
      init_queue();
      while (1) {
          do {
              printf(&quot;1.ENQUEUE 2.DEQUEUE 3.PEEK 4.STATUS 0.EXIT :&quot;);
              scanf(&quot;%d&quot;, &amp;sel);
          } while (sel &lt; 0 || sel &gt; 4);
          if (sel == 1) {
              printf(&quot;1.ENQUEUE VALUE ? &quot;);
              scanf(&quot;%d&quot;, &amp;val);
              enqueue(val);
          }
          else if (sel == 2) {
              val = dequeue();
              if (val != ERROR_VALUE)
                  printf(&quot;2.DEQUEUE VALUE = %d\n&quot;, val);
          }
          else if (sel == 3) {
              val = peek();
              if (val != ERROR_VALUE)
                  printf(&quot;3.PEEK VALUE = %d\n&quot;, val);
          }
          else if (sel == 4)
              print_queue();
          else if (sel == 0) break;
      }
      return 0;
  }
  </code></pre>
  <p>I didn't made is_full() function because linked list is &quot;dynamic&quot;. When debugging, the program stops when I try enqueuing value. My guess is that there is something wrong in enqueue function, but cannot find what.</p>