Very basic Integer queue in C I've never written a queue before and literally just took the concepts and tried to code something up. I decided to write a basic integer queue and as an example/driver I use a queue of size 5 which implements insert(), delete(), and display().

Please critique these implementations and comment if there should be any additions or modifications. The reason for the simplicity is that I'm mostly concerned that I got the concepts correct at this point. This is not the most robust queue by any means.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define ERR(msg) fprintf(stderr, "%s\n", msg)
#define QUEUE_SIZE 5

static size_t const front = 0;
static size_t rear = 0;

int queue[QUEUE_SIZE];

int insert(int num);
int delete(void);
void display(void);

int main(void)
{
    insert(5);
    insert(8);
    insert(58);
    insert(9);
    insert(10);
    display();
    delete();
    display();
    delete();
    display();
    delete();
    display();
    delete();
    display();

    return EXIT_SUCCESS;
}

int insert(int num)
{
    if((rear + 1) &gt; QUEUE_SIZE)
    {
        ERR("Queue full, cannot insert item.");
        return -1;
    }
    queue[rear++] = num;
    return 0;
}

int delete(void) // Deletes the front one
{
    if(rear == front)
    {
        ERR("Cannot delete from empty queue.");
        return -1;
    }

    size_t i;

    for(i = front; (i+1) &lt; QUEUE_SIZE; i++)
    {
        queue[i] = queue[i+1];
    }
    rear--;
    return 0;
}

void display(void)
{
    size_t i;
    for(i = front; i &lt; rear; i++)
    {
        printf("%d\t", queue[i]);
    }
    putchar('\n');
    return;
}
The delete function is not right. It is very non-optimal to shift every item by one index after each deletion. Imagine the queue has 1 million items and the deletion of front element is needed.

From Wikipedia:


  Fixed length arrays are limited in capacity, but it is not true that
  items need to be copied towards the head of the queue. The simple
  trick of turning the array into a closed circle and letting the head
  and tail drift around endlessly in that circle makes it unnecessary to
  ever move items stored in the array. If n is the size of the array,
  then computing indices modulo n will turn the array into a circle.


So, you should use a Circular buffer.

This video has a good explanation of queue and circular buffer: Data structures: Array implementation of Queue.