1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include<stdio.h> #include<malloc.h> typedef struct Queue { int *pBase; int front; int rear; } QUEUE; void init(QUEUE *pQ); bool en_queue(QUEUE *pQ, int val); bool full_queue(QUEUE *pQ); bool emput_queue(QUEUE *pQ); bool out_queue(QUEUE *pQ, int *pVal); void traverse_queue(QUEUE *pQ); 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); en_queue(&Q, 5); en_queue(&Q, 6); en_queue(&Q, 7); en_queue(&Q, 8); traverse_queue(&Q); if (out_queue(&Q, &val)) { printf("出队成功,队列出队的元素是:%d\n",val); } else { printf("出队失败!"); } 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; } printf("\n"); return; }
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; } }
|