From ee559427147b7959de5a3eb03bac39cdff6866cc 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:28:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E9=A2=98=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework3/LinkQueue.cpp | 13 ++++++++++--- homework3/LinkQueue.h | 2 +- homework3/test4.cpp | 8 ++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/homework3/LinkQueue.cpp b/homework3/LinkQueue.cpp index 465dba3..b9a35d3 100644 --- a/homework3/LinkQueue.cpp +++ b/homework3/LinkQueue.cpp @@ -7,6 +7,7 @@ using namespace std; void initQueue(linkQueue &q) { q.list = new Node; + q.list->next = NULL; q.front = q.rear = q.list; } @@ -17,9 +18,10 @@ bool isEmpty(linkQueue &q) { return false; } -bool enQueue(linkQueue &q, int e) { +void enQueue(linkQueue &q, int e) { + q.rear->next = new Node; + q.rear->next->data = e; q.rear = q.rear->next; - q.rear->data = e; } int deQueue(linkQueue &q) { @@ -27,7 +29,12 @@ int deQueue(linkQueue &q) { cout << "此队列为空" << endl; return NULL; } + Node *temp = q.front->next; + q.front = temp; int returnE = q.front->data; - q.front = q.front->next; + delete temp; + if (q.front->next == NULL) { + q.rear = q.front; + } return returnE; } diff --git a/homework3/LinkQueue.h b/homework3/LinkQueue.h index 38206fb..3753f36 100644 --- a/homework3/LinkQueue.h +++ b/homework3/LinkQueue.h @@ -18,7 +18,7 @@ void initQueue(linkQueue &q); bool isEmpty(linkQueue &q); -bool enQueue(linkQueue &q, int e); +void enQueue(linkQueue &q, int e); int deQueue(linkQueue &q); diff --git a/homework3/test4.cpp b/homework3/test4.cpp index 3c0dc82..cf68456 100644 --- a/homework3/test4.cpp +++ b/homework3/test4.cpp @@ -5,5 +5,13 @@ using namespace std; int main() { + linkQueue q; + initQueue(q); + if (isEmpty(q)) { + //判断队空 + cout << "此队列为空" << endl; + } + enQueue(q, 1); //入队 + cout << deQueue(q) << endl; //出队 return 0; }