#include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct Queue *QPtrl; typedef QPtrl List; struct Queue { ElemType data; QPtrl next; List Front, Rear; //分别是出队和入队的指针 }; //初始化 QPtrl InitQueue() { QPtrl L; L = (List)malloc(sizeof(struct Queue)); L->Front = L->Rear = NULL; printf("初始化成功\n"); return L; } int InQueue(QPtrl L, ElemType x) { List s; s = (List)malloc(sizeof(struct Queue)); s->data = x; s->next = NULL; if (L->Rear == NULL) { //如果是第一次入队 L->Front = L->Rear = s; } else { //非第一次入队 L->Rear->next = s; L->Rear = s; //Rear指向最后 } return 1; } //出队 int DeQueue(QPtrl L) { List P; ElemType e; if (L->Front == NULL) { //队列为空 return 0; } if (L->Front == L->Rear) { //队列只有一个元素 L->Front = L->Rear = NULL; } //有多个元素 P = L->Front; L->Front = L->Front->next; free(P); return 1; } //求队列长度 int QLength(QPtrl L) { int i = 0; List P; P = L->Front; while (P!=NULL) { i++; P = P->next; } printf("队列长%d\n", i); } int main() { QPtrl L; L = InitQueue(); InQueue(L, 1); InQueue(L, 2); InQueue(L, 3); DeQueue(L); DeQueue(L); QLength(L); }