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<stdlib.h> typedef struct LinkNode { int data; struct LinkNode *next; }LinkNode; typedef struct { LinkNode *front, *rear; }LinkQueue; void InitQueue(LinkQueue &Q); void EnQueue(LinkQueue &Q, int x); bool DeQueue(LinkQueue &Q, int &x); bool IsEmpty(LinkQueue Q); int main(void) { LinkQueue 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); DeQueue(Q, x); DeQueue(Q, x); DeQueue(Q, x); DeQueue(Q, x); if (!IsEmpty(Q)) { printf("x=%d\n", x); } return 0; }
void InitQueue(LinkQueue &Q) { Q.front = Q.rear = (LinkNode *)malloc(sizeof(LinkNode)); Q.front->next = NULL; }
bool QueueEmpty(LinkQueue Q) { if (Q.rear == Q.front) { return true; } else { return false; } }
void EnQueue(LinkQueue &Q, int x) { LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode)); s->data = x; s->next = NULL; if (Q.front == NULL) { Q.front = s; Q.rear = s; } else { Q.rear->next = s; Q.rear = s; } }
bool DeQueue(LinkQueue &Q, int &x) { if (Q.front == Q.rear) { return false; } LinkNode *p = Q.front; x = p->data; Q.front = p->next; if (Q.rear == p) { Q.rear = NULL; Q.front = NULL; } free(p); return true; }
bool IsEmpty(LinkQueue Q) { if (Q.front == NULL) { return true; } else { return false; } }
|