Skip to content

Instantly share code, notes, and snippets.

@Abreto
Last active December 27, 2015 04:29
Show Gist options
  • Save Abreto/7266948 to your computer and use it in GitHub Desktop.
Save Abreto/7266948 to your computer and use it in GitHub Desktop.
A testing queue.
/* A code for queue. */
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
#define DELTA_SIZE 5
typedef struct _queue
{
int size;
int *data;
int head, tail;
}queue, *p_queue;
void
init(p_queue q)
{
q->size = MAX_SIZE+1;
q->head = q->tail = 0;
q->data = (int *)malloc(q->size * sizeof(int));
}
void
clear(p_queue q)
{
q->head = q->tail = 0;
}
void
destory(p_queue q)
{
free(q->data);
}
int
is_empty(queue q)
{
if( q.head == q.tail )
return 1;
return 0;
}
int
is_full(queue q)
{
if( (q.tail+1)%(q.size) == q.head )
return 1;
return 0;
}
void
greaten(p_queue q)
{
int i = 0;
int tsize = q->size + DELTA_SIZE;
int *tmd = (int *)malloc(tsize * sizeof(int));
/* copying. */
for(i = q->head;(i)%(q->size) != q->tail;++i)
*(tmd+i-q->head) = *(q->data+(i)%(q->size));
q->tail = i-q->head;
q->head = 0;
free(q->data);
q->data = tmd;
q->size = tsize;
}
void
enqueue(p_queue q, int e)
{
if( is_full(*q) )
greaten(q);
q->data[q->tail] = e;
q->tail = (q->tail+1)%(q->size);
}
int
dequeue(p_queue q, int *x)
{
if( is_empty(*q) )
return 0;
*x = q->data[q->head];
q->head = (q->head+1)%(q->size);
return 1;
}
void
print(queue q)
{
int i = 0;
for(i = 0;i < q.size;++i)
printf("%s%s%d ", (i==q.head)?("*"):(""), i==q.tail?("$"):(""), q.data[i]);
printf("\n");
}
int main(void)
{
queue q;
return ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment