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 QNode{ int data; struct QNode *next; }QNode;
typedef struct { QNode *front; QNode *rear; }LiQueue;
void initLiQueue(LiQueue *&lqu){ lqu = (LiQueue *)malloc(sizeof(LiQueue)); lqu->front = lqu->rear = NULL; } int isQueueEmpty(LiQueue *lqu){ if(lqu->front == NULL || lqu->rear == NULL) return 1; else return 0; }
void enQueue(LiQueue *&lqu, int x){ QNode *node; node = (QNode *)malloc(sizeof(QNode)); node->data = x; node->next = NULL; if(lqu->rear == NULL) lqu->front = lqu->rear = node; else { lqu->rear->next = node; lqu->rear = node; } }
int deQueue(LiQueue *&lqu, int &x){ QNode *p; if(lqu->rear == NULL){ return 0; } else { p = lqu->front; } if(lqu->front == lqu->rear){ lqu->front=lqu->rear=NULL; }else{ lqu->front = lqu->front->next; } x=p->data; free(p); return 1; }
int main(){ LiQueue *liQueue; initLiQueue(liQueue); enQueue(liQueue, 77); enQueue(liQueue, 99); enQueue(liQueue, 88); int elem; if(deQueue(liQueue, elem)){ printf("删除成功,元素为:%d\n", elem); }else{ printf("删除失败\n"); } if(deQueue(liQueue, elem)){ printf("删除成功,元素为:%d\n", elem); }else{ printf("删除失败\n"); } return 0; }
|