队列的顺序存储
#include<iostream>
using namespace std;
#define MAXSize 10
typedef int Elemtype;
typedef struct
{
Elemtype data[MAXSize];
int front, rear;
}SqQueue;
bool IsEmpty(SqQueue& Q);
void initSqQueue(SqQueue& Q);
bool CreatQueue(SqQueue& Q);
void printSqQueue(SqQueue& Q);
bool EnQueue(SqQueue& Q);
bool OutSqQueue(SqQueue& Q);
int main()
{
SqQueue Q;
initSqQueue(Q);
CreatQueue(Q);
printSqQueue(Q);
EnQueue(Q);
printSqQueue(Q);
OutSqQueue(Q);
printSqQueue(Q);
system("pause");
return 0;
}
//初始化队列
void initSqQueue(SqQueue& Q)
{
Q.front = Q.rear = 0;
}
//判断队空
bool IsEmpty(SqQueue& Q)
{
if (Q.front == Q.rear)
return true;
else
return false;
}
//创建队列
bool CreatQueue(SqQueue& Q)
{
cout << "输入入队元素个数:" << endl;
int i;
cin >> i;
cout << "依次输入入队元素:" << endl;
Elemtype e;
if (i > MAXSize - 1)
{
cout << "队列以满:" << endl;
return false;
}
while (i!=0)
{
cin >> e;
Q.data[Q.rear] = e;
Q.rear = Q.rear + 1;
i--;
}
return true;
}
//显示队列元素
void printSqQueue(SqQueue& Q)
{
int i = Q.front;
cout << "队列元素为:" << endl;
while (i!=Q.rear)
{
cout << Q.data[i] << " ";
i++;
}
cout << endl;
}
//入队
bool EnQueue(SqQueue& Q)
{
if (Q.front ==(Q.rear + 1) % MAXSize)
{
cout << "队列已满 " << endl;
return false;
}
cout << "请输入入队元素 " << endl;
Elemtype e;
cin >> e;
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXSize;
return true;
}
//出队
bool OutSqQueue(SqQueue& Q)
{
if (Q.front == Q.rear)
{
cout << "队列已满:" << endl;
return false;
}
Elemtype e = Q.data[Q.front];
Q.front = (Q.front + 1) % MAXSize;
cout << "元素出队成功!" << endl;
return true;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了