C实现队列

今天又是在加班,无聊就写了一个队列的实现,具体代码如下:

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\n");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\n");return 1;}
   pnode *elem=mem_alloc(pnode,sizeof(pnode));
   if(!elem){msg_err("malloc for pnode fali\n");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\n");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("\t q->base.start = %p\n",q->base.start); 
   while(q->base.end!=NULL)
    {
        printf("\t cur pnode = %p ,",q->base.end);
        pnode *down=q->base.end->down;
        free(q->base.end);
        printf("free\n");
        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\n",p);
        switch(p->type)
        {
            case 'I':
                printf("\tpnode->data = %d\n",*((char *)p->data));
                break;
            case 'S':
                printf("\tpnode->data = %s\n",(char *)p->data);
                break;
            case 'C':
                printf("\tpnode->data = %c\n",*((char *)p->data));
                break;
            default:
                break;
        }
        printf("\tpnode->size = %d\n",p->size);
        printf("\tpnode->type = %c\n",p->type);
        printf("\tpnode->down = %p\n",p->down);
       // printf("\tque->len = %d\n",q->len);
        p=p->down;
    }
}
int main(void)
{
  printf("sizeof que:%d\n",sizeof(que)); 
  que *q=que_new();
  int x=100;
  char *s="133";
  char x1='P';
  printf("que_add(%p,%c,%c): %d\n",q,x1,x1,que_add(q,&x1,'C'));
  printf("que_add(%p,%d,%c): %d\n",q,x,'I',que_add(q,&x,'I'));
  printf("que_add(%p,%s,%c): %d\n",q,s,'S',que_add(q,s,'S'));
  que_prt(q);
  printf("que_destroy(%p): %d\n",q,que_destroy(q));
  q=que_new();
  printf("que_add(%p,%c,%c): %d\n",q,x1,x1,que_add(q,&x1,'C'));                                  
  printf("que_add(%p,%d,%c): %d\n",q,x,'I',que_add(q,&x,'I'));                                   
  printf("que_add(%p,%s,%c): %d\n",q,s,'S',que_add(q,s,'S'));      
  printf("que_pop(%p) :%d\n",q,que_pop(q));
  printf("que_pop(%p) :%d\n",q,que_pop(q));
  que_prt(q);
  printf("que->len :%d\n",q->len);
  printf("que_destroy(%p): %d\n",q,que_destroy(q));
  //que_destroy(q);
  return 0;
}

 

你可能感兴趣的:(C实现队列)