123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | #include<malloc.h>typedef struct //定义一个数据类型QUEUE{ int * pBase; int front; int rear;}QUEUE; 大专栏 数据结构队列的实现span>void init(QUEUE *); bool en_queue(QUEUE *, int val);//入队void traverse_queue(QUEUE *);//遍历队列bool full_queue(QUEUE *); //判断入队之前是不是已经满了bool out_queue(QUEUE *, int * ); //出队bool emput_queue(QUEUE *); //判断出队的时候是否为空int main(void){ QUEUE Q; int val; init(&Q); en_queue(&Q,1); en_queue(&Q,2); en_queue(&Q,3); en_queue(&Q,4); if(out_queue(&Q,&val)) { printf("出队成功,队列出队的元素是:%dn" , val); } else { printf("出队失败n"); } traverse_queue(&Q); return 0;}void init(QUEUE *pQ){ pQ->pBase = (int *)malloc(sizeof(int)*6); pQ->front = 0; pQ->rear = 0;}bool full_queue(QUEUE *pQ){ if((pQ->rear +1)%6 == pQ->front) return true; else return false;}bool en_queue(QUEUE * pQ, int val){ if(full_queue(pQ)) { return false; } else { pQ->pBase[pQ->rear] = val; pQ->rear = (pQ->rear + 1)%6; return true; }}void traverse_queue(QUEUE * pQ)//遍历队列{ int i = pQ->front; while(i != pQ->rear) { printf("%d ",pQ->pBase[i]); i = (i+1)%6; } }bool emput_queue(QUEUE *pQ){ if(pQ->front==pQ->rear) { return true; } else { return false; }}bool out_queue(QUEUE * pQ, int * pVal){ if(emput_queue(pQ)) { return false; } else { *pVal = pQ->pBase[pQ->front]; pQ->front = (pQ->front+1)%6; return true; }} |