#include#include #include typedef int ElemType; typedef struct LinkNode { ElemType data; struct LinkNode *next; } LinkNode; typedef struct { LinkNode *front,*rear;//队头指针,队尾指针 } LinkQueue; void InitQueue(LinkQueue *Q) { Q->front=Q->rear=(LinkNode*)malloc(sizeof(LinkNode));//建立头节点 Q->front->next=NULL; } bool IsEmpty(LinkQueue *Q) { if(Q->rear==Q->front)return true;//队头指针和队尾指针,指向同一片区域 return false; } //入队 void EnQueue(LinkQueue *Q,ElemType e) { LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode)); s->data=e; s->next=NULL; Q->rear->next=s; Q->rear=s;//尾插法 } bool DeQueue(LinkQueue *Q,ElemType *x) { if(Q->front==Q->rear)return false;//队空 LinkNode *p=Q->front->next; *x=p->data; Q->front->next=p->next; if(p==Q->rear)Q->rear=Q->front;//队列中只有一个元素 free(p);//删除p所指向的空间 return true; } void PrintQueue(LinkQueue *Q) { LinkNode *p=Q->front->next;//p指向首结点 while(p) { printf("%d---->",p->data); p=p->next; } printf("n"); } int main() { LinkQueue Q; InitQueue(&Q); //将1~10插入队列 for(int i=1; i<=10; i++) { EnQueue(&Q,i); } PrintQueue(&Q); //将1~5出队 for(int i=1; i<=5; i++) { int e; DeQueue(&Q,&e); printf("当前出队元素为:%dn",e); } PrintQueue(&Q); return 0; }



