From 687d2906edd7d9b362c5523cd343345aaef702cc Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Mon, 21 Mar 2022 15:55:20 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E9=93=BE=E9=98=9F=E5=87=BA=E9=98=9F?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=EF=BC=88=E5=B8=A6=E5=A4=B4=E7=BB=93=E7=82=B9?= =?UTF-8?q?=EF=BC=89.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 队列/链队出队操作(带头结点).c | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 队列/链队出队操作(带头结点).c diff --git a/队列/链队出队操作(带头结点).c b/队列/链队出队操作(带头结点).c new file mode 100644 index 0000000..dc8ef2b --- /dev/null +++ b/队列/链队出队操作(带头结点).c @@ -0,0 +1,63 @@ +#include +#include +#include + +typedef struct Node{ + int data; + struct Node *next; +}LinkNode; +typedef struct Queue{ + 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.front==Q.rear){ + return true; + }else{ + return false; + } +} +//入队(带头结点) +void EnQueue(LinkQueue *Q,int x){ + LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode)); + s->data = x; + s->next = NULL; + Q->rear->next = s; + Q->rear = s; +} +//出队(带头结点) +bool DeQueue(LinkQueue *Q){ + if(IsEmpty(*Q)){ + printf("当前链队为空,无法出队\n"); + return false; + } + LinkNode *p = Q->front->next; + int x = p->data; + printf("当前出队的元素为:%d\n",x); + Q->front->next = p->next; + if(Q->rear==p){ //此次最后一个结点出队 + Q->rear = Q->front; + } + free(p); + return true; +} + +int main(){ + LinkQueue Q; + InitQueue(&Q); + printf("队列是否为空:%d\n",IsEmpty(Q)); + for(int i=0;i<=10;i++){ + EnQueue(&Q,i); + } + for(int i=0;i<=11;i++){ + DeQueue(&Q); + } + printf("队列是否为空:%d\n",IsEmpty(Q)); + return 0; +}