From 9448c773d44e9bf06a2bc0de3a2297dc6ba10937 Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Mon, 21 Mar 2022 16:47:04 +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=E4=B8=8D=E5=B8=A6=E5=A4=B4=E7=BB=93?= =?UTF-8?q?=E7=82=B9=EF=BC=89.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 队列/链队出队操作(不带头结点).c | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 队列/链队出队操作(不带头结点).c diff --git a/队列/链队出队操作(不带头结点).c b/队列/链队出队操作(不带头结点).c new file mode 100644 index 0000000..8f47dec --- /dev/null +++ b/队列/链队出队操作(不带头结点).c @@ -0,0 +1,65 @@ +#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 = NULL; + Q->rear = NULL; +} +//判断队列是否为空 +bool IsEmpty(LinkQueue Q){ + if(Q.front==NULL){ + return true; + }else{ + return false; + } +} +//入队(不带头结点) +void EnQueue(LinkQueue *Q,int x){ + LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode)); + s->data = x; + s->next = NULL; + if(IsEmpty(*Q)){ + Q->rear = s; + Q->front = s; + }else{ + Q->rear->next = s; + Q->rear = s; + } +} +//出队(不带头结点) +bool DeQueue(LinkQueue *Q){ + if(IsEmpty(*Q)){ + printf("当前链队为空,无法出队\n"); + return false; + } + LinkNode *p = Q->front; + int x = p->data; + printf("当前出队的元素为:%d\n",x); + Q->front = p->next; + 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; +}