• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

promote-L

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

队列的顺序存储

 

 

#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;
}

 

 

posted on 2023-02-08 14:08  浪訫  阅读(15)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3