circular queue的實現
1.先定義乙個結構體
typedef struct cirqueue
cirqueue;
2.幾個重要的巨集
//判斷隊列為空
#define queue_is_empty(_pxqueue) ((((cirqueue*)(_pxqueue))->numbers == 0)?1:0)
//判斷隊列為滿
#define queue_is_full(_pxqueue) ((((cirqueue*)(_pxqueue))->numbers >= ((cirqueue*)(_pxqueue))->depth)?1:0)
//佇列中元素總個數
#define queue_get_number(_pxqueue) (((cirqueue*)(_pxqueue))->numbers)
3.幾個重要的函式
//建立佇列
cirqueue* queuecreate(unsigned short depth,unsigned short itemsize);
//入隊函式
unsigned char queueinput(cirqueue* pqueue,const void *const pitem);
//出隊函式
unsigned char queueoutput(cirqueue* pqueue,void * const pitem);
//銷毀佇列
void queuedestory(cirqueue* &pqueue);
4.具體實現
//建立佇列
cirqueue* queuecreate(unsigned short depth,unsigned short itemsize)
pqueue->front = 0;
pqueue->rear = 0;
pqueue->numbers = 0;
pqueue->isfull = 0;
pqueue->depth = depth;
pqueue->itemsize = itemsize;
}return pqueue; }
//銷毀佇列
void queuedestory(cirqueue* &pqueue)
delete pqueue;
pqueue = nullptr; }}
//入隊函式
//pitem 需要入隊的指標
unsigned char queueinput(cirqueue* pqueue,const void * const pitem)
memcpy(ptr + (pqueue->rear * pqueue->itemsize),pitem,pqueue->itemsize);
pqueue->rear = (++pqueue->rear < pqueue->depth)?pqueue->rear:0;
if((pqueue->rear > pqueue->front || pqueue->rear == 0) && pqueue->numbers == pqueue->depth)
pqueue->numbers = (++pqueue->numbers > pqueue->depth)?pqueue->depth:pqueue->numbers;
return isfull;
}//出隊函式
//pitem 需要出隊的指標
unsigned char queueoutput(cirqueue* pqueue,void * const pitem)
memcpy(pitem,ptr+(pqueue->front * pqueue->itemsize),pqueue->itemsize);
memset(ptr + (pqueue->front * pqueue->itemsize),0,sizeof(unsigned char)*(pqueue->itemsize));
pqueue->front = (++pqueue->front < pqueue->depth)?pqueue->front:0;
pqueue->numbers--;
return 0;
}
迴圈佇列的實現(c )
環境 vs2010 較少,直接乙個原始檔 include using namespace std const int queuesize 100 template class cirqueue 撤銷迴圈佇列 void enqueue datatype x 隊不滿時,x進隊 datatype dequ...
C 迴圈佇列實現
乙個迴圈佇列的c語言實現,資料型別queue定義如下,注意在typedef structqueue 中queue為資料型別,而在struct queue 中queue為乙個變數名。front為隊首元素下標,始終指向隊首元素,tail為隊尾元素的下乙個位置的下標。初始狀態為front tail 0 t...
C 實現迴圈佇列
circularqueue.h pragma once pragma once ifndef circularqueue h define circularqueue h include include using std cout using std cin using std endl usin...