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
| #include<stdio.h> #define MaxSize 10 typedef struct { int data[MaxSize]; int front, rear; } SqQueue; void InitQueue(SqQueue &Q); bool QueueEmpty(SqQueue Q); bool EnQueue(SqQueue &Q, int x); bool DeQueue(SqQueue &Q, int &x); bool GetHead(SqQueue Q, int &x); int main(void) { SqQueue Q; int x; InitQueue(Q); EnQueue(Q, 1); EnQueue(Q, 2); EnQueue(Q, 3); EnQueue(Q, 4); EnQueue(Q, 5); EnQueue(Q, 6); EnQueue(Q, 7); EnQueue(Q, 8); EnQueue(Q, 9); EnQueue(Q, 10); EnQueue(Q, 11); EnQueue(Q, 12); GetHead(Q, x); printf("x=%d\n", x); DeQueue(Q, x); DeQueue(Q, x); DeQueue(Q, x); DeQueue(Q, x); printf("x=%d\n", x); return 0; }
void InitQueue(SqQueue &Q) { Q.rear=Q.front = 0; }
bool QueueEmpty(SqQueue Q) { if (Q.rear == Q.front) { return true; } else { return false; } }
bool EnQueue(SqQueue &Q, int x) { if (Q.rear == MaxSize) { return false; } Q.data[Q.rear] = x; Q.rear = (Q.rear + 1) % MaxSize; return true; }
bool DeQueue(SqQueue &Q, int &x) { if (Q.rear == Q.front) { return false; } x = Q.data[Q.front]; Q.front = (Q.front + 1) % MaxSize; }
bool GetHead(SqQueue Q, int &x) { if (Q.rear == Q.front) { return false; } x = Q.data[Q.front]; return true; }
|