From 4d7f7d127630a904ae80f54f86c93a51462dfa4d Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Sun, 21 May 2023 16:38:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + LICENSE | 21 ++++++++++ README.md | 8 ++++ main.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e30bef3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# 项目排除路径 +/cmake-build-debug/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fd502d3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016-2023 筱锋xiao_lfeng + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..680530b --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# DataStructure_ArrayQueue_C + +高等院校计算机必修课(基于C语言数据结构) —— 线性表(队列) + +作者:[筱锋xiao_lfeng](https://www.x-lf.com/) + +注意:代码不更新,接收用户PR。 +若使用请遵循MIT开源协议 \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fab495b --- /dev/null +++ b/main.cpp @@ -0,0 +1,110 @@ +#include + +#define MaxSize 100 + +typedef int datatype; +typedef struct { + int rear; + int front; + datatype data[MaxSize]; +} SqQueue; + +// ʼ +void InitQueue(SqQueue &queue) { + queue.rear = 0; + queue.front = 0; +} + +// ж϶ǷΪ +int isEmpty(SqQueue queue) { + if (queue.rear == queue.front) return 1; + return 0; +} + +// ж϶ǷΪ +int isFull(SqQueue queue) { + if (queue.front-1 == queue.rear) return 1; + return 0; +} + +// +int enQueue(SqQueue &queue, datatype data) { + if (queue.front-1 == queue.rear) return 0; + queue.data[queue.rear] = data; + if ( (queue.rear+1)%MaxSize == 0 && (queue.rear+1)%MaxSize < queue.front) + queue.rear = (queue.rear+1)%MaxSize; + else queue.rear++; + return 1; +} + +// +int deQueue(SqQueue &queue) { + if (queue.rear == queue.front) return 0; + queue.data[queue.front] = NULL; + if ((queue.front+1)%MaxSize == 0 && (queue.front+1)%MaxSize < queue.rear) + queue.front = (queue.front+1)%MaxSize; + else queue.front++; + return 1; +} + +// ѯ +void selQueue(SqQueue queue) { + printf("[SELECT] б[ "); + for (int i = queue.front; i < queue.rear-1; ++i) { + printf("%d, ",queue.data[i]); + } + printf("%d ]\n",queue.data[queue.rear-1]); + printf("\n"); +} + +int main() { + int ch; + datatype e; + SqQueue queue; + InitQueue(queue); + Start: + printf("[INFO] в˵\n"); + printf("\t=========\n"); + printf("\t1. \n"); + printf("\t2. \n"); + printf("\t3. вѯ\n"); + printf("\t4. Ƿ\n"); + printf("\t5. Ƿӿ\n"); + printf("\t==========\n"); + printf("\tѡݣ"); + scanf_s("%d", &ch); + printf("\n"); + + switch (ch) { + case 1: + printf("[INSERT] ݣ"); + scanf_s("%d", &e); + if (enQueue(queue,e)) printf("\t[INFO] вɹ\n"); + else printf("\t[INFO] вʧܣ"); + printf("\n"); + goto Start; + case 2: + if (deQueue(queue)) printf("[INFO] ӳɹ\n"); + else printf("[INFO] ʧܣӿգ\n"); + printf("\n"); + goto Start; + case 3: + selQueue(queue); + goto Start; + case 4: + if (isFull(queue)) + printf("[SELECT] \n"); + else + printf("[SELECT] δ\n"); + printf("\n"); + goto Start; + case 5: + if (isEmpty(queue)) + printf("[SELECT] ӿ\n"); + else + printf("[SELECT] δ\n"); + printf("\n"); + goto Start; + default: break; + } +}