代码如下:
#include<stdio.h> #include<stdlib.h> /*************************************** * 创建一个队列 * 两个结构体,一个是链表,另一个结构体由队头和队尾组成 * 申请空间 * 空队时,队头和队尾相同 * 空队时队头指向NULL * ************************************/ typedef struct QNode { char date; struct QNode *next; }QNode , *QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; void initQueue(LinkQueue *q) { q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); if (!q->front) { exit(0); } q->front->next = NULL; } int main() { LinkQueue q; initQueue(&q); if (q.front == q.rear) { printf("队列创建成功!"); }else { printf("队列创建失败!"); } return 0; }
运行结果: