Preview:
#include <stdlib.h>
#include <stdio.h>
#define MIN_QUEUE_SIZE 1

struct Queue_t{
  int length; 
  int head,tail;
  char *array;
};

typedef struct Queue_t* Queue;

/*
 * create a empty stack according to the given len
 * return :the pointer to the stack
 */
Queue Create_Queue(size_t len)
{
  if(len<MIN_QUEUE_SIZE)
  {
    return NULL;
  }

  Queue q=(Queue)malloc(sizeof(struct Queue_t));
  if(q==NULL)
  {
    return NULL;
  }

  char *array=(char *)malloc(sizeof(char)*len+1);
  if(array==NULL)
  {
    return NULL;
  }

  q->head=0;
  q->tail=0;

  q->array=array;
  q->length=len+1;

  return q;
}

/* 
 * delete the given stack
 */
int Delete_Queue(Queue q)
{
  if(q==NULL)
  {
    return -1;
  }

  free(q->array);

  free(q);
  return 1;

}
/*
 * return 0:empty
 */
int IsEmptyQueue(Queue q)
{
  if(q==NULL)
  {
    return -1;
  }
  if(q->head==q->tail)
  {
    return 0;
  }
  return 1;
}

/*
 * push a char to stack s
 */
int Enqueue(Queue q,char c)
{
  if(q==NULL)
  {

    return -1;
  }

  //tail arrive to the end?
  if(q->tail==q->length-1)
  {
    //array[0] is empty?
    if(q->head!=0)
    {
      q->array[q->tail]=c;
      q->tail=0;
    }else{

      return -1;
    }

  }else{

    //head is before tail?
    if((q->tail+1)==q->head)
    {

      return -1;
    }
    q->array[q->tail]=c;
    q->tail++;
  }

  return 1;
}
/*
 * pop a char from stack s
 */
char Dequeue(Queue q)
{
  if(IsEmptyQueue(q)<1)
  {
    return -1;
  }

  char temp = q->array[q->head];

  //is head to the end?
  if(q->head==q->length-1)
  {
    q->head=0;
  }else{
    q->head++;
  }


  return temp;
}
/*
 * get a char from stack s
 */
char GetQueueHead(Queue q)
{
  if(IsEmptyQueue(q)<1)
  {
    return -1;
  }
  return q->array[q->head];
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter