title: Creating a queue with structs in C
body: <p>I have a code (at the end of this post) that implements a circular queue system. Everything works perfectly, but as can be seen in the function <code>createQueue</code> the queue is implemented only for integers. I would like to modify this code to accept a struct informed by the user.</p>
  
  <p>I could create a known struct and replace all sites with integer, but this way I would be coupling the code to a known struct. Bad idea...</p>
  
  <p>How could pass to the function <code>createQueue</code> a struct for memory allocation without needing to know the struct previously? The struct Queue should also be changed, in <strong>int *elements</strong> the value should be changed from integer to void?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct Queue
  {
          int capacity;
          int size;
          int front;
          int rear;
          int *elements;
  }Queue;
  
  Queue * createQueue(int maxElements)
  {
          /* Create a Queue */
          Queue *Q;
          Q = (Queue *)malloc(sizeof(Queue));
          /* Initialise its properties */
          Q-&gt;elements = (int *)malloc(sizeof(int)*maxElements);
          Q-&gt;size = 0;
          Q-&gt;capacity = maxElements;
          Q-&gt;front = 0;
          Q-&gt;rear = -1;
          /* Return the pointer */
          return Q;
  }
  void Dequeue(Queue *Q)
  {
          /* If Queue size is zero then it is empty. So we cannot pop */
          if(Q-&gt;size==0)
          {
                  printf("Queue is Empty\n");
                  return;
          }
          /* Removing an element is equivalent to incrementing index of front by one */
          else
          {
                  Q-&gt;size--;
                  Q-&gt;front++;
                  /* As we fill elements in circular fashion */
                  if(Q-&gt;front==Q-&gt;capacity)
                  {
                          Q-&gt;front=0;
                  }
          }
          return;
  }
  int front(Queue *Q)
  {
          if(Q-&gt;size==0)
          {
                  printf("Queue is Empty\n");
                  exit(0);
          }
          /* Return the element which is at the front*/
          return Q-&gt;elements[Q-&gt;front];
  }
  void Enqueue(Queue *Q,int element)
  {
          /* If the Queue is full, we cannot push an element into it as there is no space for it.*/
          if(Q-&gt;size == Q-&gt;capacity)
          {
                  printf("Queue is Full\n");
          }
          else
          {
                  Q-&gt;size++;
                  Q-&gt;rear = Q-&gt;rear + 1;
                  /* As we fill the queue in circular fashion */
                  if(Q-&gt;rear == Q-&gt;capacity)
                  {
                          Q-&gt;rear = 0;
                  }
                  /* Insert the element in its rear side */ 
                  Q-&gt;elements[Q-&gt;rear] = element;
          }
          return;
  }
  int main()
  {
          Queue *Q = createQueue(5);
          Enqueue(Q,1);
          Enqueue(Q,2);
          Enqueue(Q,3);
          Enqueue(Q,4);
          printf("Front element is %d\n",front(Q));
          Enqueue(Q,5);
          Dequeue(Q);
          Enqueue(Q,6);
          printf("Front element is %d\n",front(Q));
  }
  </code></pre>