From 2a858769de6efbfb6226c90a5086826697a74c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Thu, 24 Oct 2024 01:08:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=93=BE=E9=98=9F=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework3/LinkQueue.cpp | 27 +++++++++++++++++++++++++++ homework3/LinkQueue.h | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/homework3/LinkQueue.cpp b/homework3/LinkQueue.cpp index 32f8da1..465dba3 100644 --- a/homework3/LinkQueue.cpp +++ b/homework3/LinkQueue.cpp @@ -4,3 +4,30 @@ #include using namespace std; + +void initQueue(linkQueue &q) { + q.list = new Node; + q.front = q.rear = q.list; +} + +bool isEmpty(linkQueue &q) { + if (q.front == q.rear) { + return true; + } + return false; +} + +bool enQueue(linkQueue &q, int e) { + q.rear = q.rear->next; + q.rear->data = e; +} + +int deQueue(linkQueue &q) { + if (isEmpty(q)) { + cout << "此队列为空" << endl; + return NULL; + } + int returnE = q.front->data; + q.front = q.front->next; + return returnE; +} diff --git a/homework3/LinkQueue.h b/homework3/LinkQueue.h index 02d27b1..38206fb 100644 --- a/homework3/LinkQueue.h +++ b/homework3/LinkQueue.h @@ -3,5 +3,23 @@ #ifndef LINKQUEUE_H #define LINKQUEUE_H +typedef struct Node { + int data; + Node *next; +} Node, *LinkList; + +typedef struct { + LinkList list; + Node *front; + Node *rear; +} linkQueue; + +void initQueue(linkQueue &q); + +bool isEmpty(linkQueue &q); + +bool enQueue(linkQueue &q, int e); + +int deQueue(linkQueue &q); #endif //LINKQUEUE_H