今天又是在加班,无聊就写了一个队列的实现,具体代码如下:
que.h
#define ut_base(TYPE) struct { TYPE *start; TYPE *end; }__attribute__((packed)) #define mem_alloc(TYPE,SIZE) (TYPE *)malloc(SIZE) #define msg_err(msg) fprintf(stderr,msg) typedef char byte; typedef unsigned int uint; typedef struct _pnode { void *data; uint size; char type; struct _pnode *down; }__attribute__((packed)) pnode; typedef struct _que { uint len; ut_base(struct _pnode) base; }__attribute__((packed)) que; /* create new queque*/ que *que_new(); /*add one element to queue */ int que_add(que *q,void *pdata,char type); /*remove one element from que */ int que_pop(que *q); /* destroy a queue*/ int que_destroy(que *q);
que.c的实现
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "que.h" que *que_new() { que *q=mem_alloc(que,sizeof(que)); if(!q){msg_err("malloc memory for que fali ");return NULL;} q->base.start=NULL; q->base.end=NULL; q->len=0; return q; } int que_add(que *q,void *pdata,char type) { if(!q){msg_err("queue header is null ");return 1;} pnode *elem=mem_alloc(pnode,sizeof(pnode)); if(!elem){msg_err("malloc for pnode fali ");return 1;} char dtype=0; int size=0; if(q->base.start==NULL) { q->base.start=elem; } switch(type) { case 'I': size=sizeof(int); dtype='I'; break; case 'S': size=strlen((char *)pdata)+1; dtype='S'; break; case 'C': size=sizeof(char); dtype='C'; break; default: dtype='X'; break; } elem->data=mem_alloc(byte,size); if(!elem->data){msg_err(" malloc memory for elem->data fail ");return 1;} memcpy(elem->data,(byte *)pdata,size); elem->size=size; elem->type=dtype; elem->down=q->base.end; q->base.end=elem; q->len++; return 0; } int que_pop(que *q) { pnode *top=q->base.end->down; pnode *prt=q->base.end; q->base.end=top; q->len--; free(prt); prt=NULL; return 0; } int que_destroy(que *q) { printf(" q->base.start = %p ",q->base.start); while(q->base.end!=NULL) { printf(" cur pnode = %p ,",q->base.end); pnode *down=q->base.end->down; free(q->base.end); printf("free "); q->base.end=down; } free(q); return 0; } void que_prt(que *q) { pnode *p=q->base.end; while(p!=NULL) { printf(" cur pnode = %p ",p); switch(p->type) { case 'I': printf(" pnode->data = %d ",*((char *)p->data)); break; case 'S': printf(" pnode->data = %s ",(char *)p->data); break; case 'C': printf(" pnode->data = %c ",*((char *)p->data)); break; default: break; } printf(" pnode->size = %d ",p->size); printf(" pnode->type = %c ",p->type); printf(" pnode->down = %p ",p->down); // printf(" que->len = %d ",q->len); p=p->down; } } int main(void) { printf("sizeof que:%d ",sizeof(que)); que *q=que_new(); int x=100; char *s="133"; char x1='P'; printf("que_add(%p,%c,%c): %d ",q,x1,x1,que_add(q,&x1,'C')); printf("que_add(%p,%d,%c): %d ",q,x,'I',que_add(q,&x,'I')); printf("que_add(%p,%s,%c): %d ",q,s,'S',que_add(q,s,'S')); que_prt(q); printf("que_destroy(%p): %d ",q,que_destroy(q)); q=que_new(); printf("que_add(%p,%c,%c): %d ",q,x1,x1,que_add(q,&x1,'C')); printf("que_add(%p,%d,%c): %d ",q,x,'I',que_add(q,&x,'I')); printf("que_add(%p,%s,%c): %d ",q,s,'S',que_add(q,s,'S')); printf("que_pop(%p) :%d ",q,que_pop(q)); printf("que_pop(%p) :%d ",q,que_pop(q)); que_prt(q); printf("que->len :%d ",q->len); printf("que_destroy(%p): %d ",q,que_destroy(q)); //que_destroy(q); return 0; }