队列-C语言实现-适用各种数据类型

#ifndef _QUEUE_TEST_H
#define _QUEUE_TEST_H


#include "stdio.h"
#include "stdlib.h"

struct queue_record
{
  int capacity;
  int front;
  int rear;
  int size;
  int type_size;
  void *array;
};
typedef struct queue_record *p_queue_t;

#define uint8_t unsigned char


int is_empty(p_queue_t q);
int is_full(p_queue_t q);
void make_empty( p_queue_t q );
p_queue_t createqueue(int maxelements, void* x, char type_size);
void enqueue(void* x, p_queue_t q);
void* dequeue(p_queue_t q);


  
	
#endif

#ifndef _APP1_H
#define _APP1_H


typedef struct
{
	int aa;
	int bb;
	int cc;
}app1_t;

void app1_init(void);
void app1_hander(void);
#endif

#include "app1.h"
#include "stdio.h"
#include "queue.h"

#define ARRAY_LENGTH  15

app1_t app1_array[ARRAY_LENGTH];

p_queue_t app1_queue;

void app1_init(void)
{
	app1_queue = createqueue(ARRAY_LENGTH, &app1_array[0],sizeof(app1_t));
}


void app1_hander(void)
{
	app1_t temp;
	int i;

	temp.aa = 1;
	temp.bb = 2;
	temp.cc = 3;
	for(i = 0; i < ARRAY_LENGTH;i++)
	{
		temp.aa++;
		temp.bb++;
		temp.cc++;
		enqueue(&temp, app1_queue);
	}
	printf("app1 hander:\r\n");

	for(i = 0; i < ARRAY_LENGTH ;i++)
	{
		temp = *(app1_t*)dequeue(app1_queue);
		printf("aa=%d,bb=%d,cc=%d\r\n",temp.aa, temp.bb, temp.cc);
	}
}
#ifndef _APP2_H
#define _APP2_H

typedef struct
{
	char aa[10];
	int* bb;
}app2_t;



void app2_init(void);
void app2_hander(void);
#endif


#include "app2.h"
#include "stdio.h"
#include "queue.h"

#define ARRAY_LENGTH  4

app2_t app2_array[ARRAY_LENGTH];

p_queue_t app2_queue;

void app2_init(void)
{
	app2_queue = createqueue(ARRAY_LENGTH, &app2_array[0],sizeof(app2_t));
}
void app2_hander(void)
{
	app2_t temp;
	int i,j;

	for(i = 0; i < 10; i++)
	{
		temp.aa[i] = 'A';
	}
	for(i = 0; i < ARRAY_LENGTH ;i++)
	{
		enqueue(&temp, app2_queue);
	}
	printf("app2 hander:\r\n");

	for(i = 0; i < ARRAY_LENGTH ;i++)
	{
		temp = *(app2_t*)dequeue(app2_queue);
		for(j = 0; j < 10; j++)
		{
			printf("%c",temp.aa[j]);
		}
		printf("\r\n");
	}
}

#include "app1.h"
#include "app2.h"





int main()
{
	app2_init();
	app2_hander();
	app1_init();
	app1_hander();
}

#include "queue.h"


int is_empty(p_queue_t q)
{
	return q->size == 0;
}
int is_full(p_queue_t q)
{
	return q->size == q->capacity;
}
void make_empty( p_queue_t q )
{
	q->size = 0;
	q->front = 1;
	q->rear = 0;
}

static int succ( int value, p_queue_t q )
{
	if ( ++value == q->capacity )
	{
		value = 0;
	}
	return value;
}
p_queue_t createqueue(int maxelements, void* x, char type_size)
{
	p_queue_t q;
	q = malloc( sizeof( struct queue_record ));
	if( q == NULL)
	{
		return NULL;
	} 
	q->array = x;
	q->capacity = maxelements;
	q->type_size = type_size;
	make_empty( q );

	return q;
}
void enqueue(void* x, p_queue_t q)
{
	int i;
	if(is_full( q ))
	{
		return;
	}

	q->size++;
	q->rear = succ( q->rear, q );
	for(i = 0; i < q->type_size;i++)
	{
		*(uint8_t *)((uint8_t *)(q->array) + q->rear*q->type_size + i) = *(uint8_t *)((uint8_t *)x + i);
	}
//	memcpy((uint8_t *)(q->array) + q->rear*q->type_size , x, q->type_size);
}
void* dequeue(p_queue_t q)
{
	void *temp;	
	
	if (is_empty( q ))
	{
		return NULL;
	}
	
	temp = (uint8_t *)(q->array) + q->front*q->type_size;
	
	q->size--;
	q->front = succ( q->front, q );

	return temp;
}

大部分代码参考 http://blog.csdn.net/shuxiao9058/article/details/7173262

你可能感兴趣的:(c语言)