Ai
1 Star 4 Fork 5

Shapper/C Data Structure

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
c_queue.cpp 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
Shapper 提交于 2022-10-30 18:54 +08:00 . 添加队列相关内容
//
// Created by Win10 on 2022/10/30.
//
#include<iostream>
#include "c_queue.h"
using namespace std;
void InitQueue(CQueue &Q) {
Q.front = Q.rear = Q.size = 0;
}
bool QueueEmpty(CQueue Q) {
if (Q.size == 0) return true;
return false;
}
bool QueueFull(CQueue Q) {
if (Q.size == MaxSize) return true;
return false;
}
bool EnQueue(CQueue &Q, ElemType x) {
if (QueueFull(Q)) return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
Q.size++;
return true;
}
bool DeQueue(CQueue &Q, ElemType &x) {
if (QueueEmpty(Q)) return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.size--;
return true;
}
bool GetHead(CQueue Q, ElemType &x) {
if (QueueEmpty(Q)) {
return false;
}
x = Q.data[Q.front];
return true;
}
int GetSize(CQueue Q) {
return Q.size;
}
int main() {
{
CQueue Q;
InitQueue(Q);
for (int i = 1; i <= 5; i++) {
bool f = EnQueue(Q, i);
if (!f) cout << i << endl;
}
ElemType x;
DeQueue(Q, x);
cout << x << endl;
DeQueue(Q, x);
cout << x << endl;
EnQueue(Q, 1);
EnQueue(Q, 2);
cout << Q.front << " " << Q.rear << endl;
if (!EnQueue(Q, 6)) {
cout << "队列已满" << endl;
}
GetHead(Q, x);
cout << x << endl << endl;
while (!QueueEmpty(Q)) {
DeQueue(Q, x);
cout << x << endl;
}
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/cloudcan/c-data-structure.git
git@gitee.com:cloudcan/c-data-structure.git
cloudcan
c-data-structure
C Data Structure
master

搜索帮助