以下是数据结构中关于顺序队列的声明、初始化、判断空与满、出队、入队、遍历队等基础操作(编程风格参考严蔚敏数据结构)。
头文件及宏
#include<iostream>
#include<stdio.h>
using namespace std;
#define QElemType char
#define Status int//表示状态
#define OK 1
#define ERROR 0
#define MAXSIZE 7
为了测试方便,设队列尺寸为7个元素;
为了增加程序可读性,把Status定义为int类型,为的是和OK、ERROR这两个定义为整形的宏匹配。
统一把队列元素类型QElemType设置为char类型(这样就可以测试字母输入了)。
声明及初始化
typedef struct SeQueue{
QElemType *base;
int front;//标记队头
int rear;//标记队尾
}Squeue;
Status initial(Squeue &sq){
sq.base = new QElemType[MAXSIZE];
if(!sq.base){
cout<<"初始化失败\n";
return ERROR;
}
sq.front = sq.rear = 0;
return OK;
}
步骤:
- 声明顺序队列sq;
- 给sq的base数组分配内存空间;
- 最初因为没有元素在数组内,队头队尾均设为0。
判断队空/队满
Status isNotFull(Squeue sq){
if(sq.rear-sq.front == MAXSIZE){
cout<<"队满!\n";
return ERROR;
}
return OK;
}
Status isNotEmpty(Squeue sq){
if(sq.front==sq.rear){
cout<<"队空!\n";
return ERROR;
}
return OK;
}
步骤:
- 如果队头和队尾的值相等,说明队列是空的;
- 如果队尾减去队头等于MAXSIZE,说明队列满队(这里不用MAXSIZE-1是因为元素入队完毕后尾指针已经自加一了,所以不用MAXSIZE-1)。
入队
Status Enqueue(Squeue &sq,QElemType e){
sq.base[sq.rear++] = e;
return OK;
}
void addElem(Squeue &sq){
QElemType elem = '0';
while(elem!='*' && isNotFull(sq)){
cout<<"请输入元素(输入*结束进队):";
cin>>elem;
if(elem!='*'){
Enqueue(sq,elem);
}
}
cout<<"入队完毕\n";
showSqueue(sq);
}
步骤:
- 判断是否队满;
- 将添加进队尾位置;
- 队尾自增1。
出队
Status Dequeue(Squeue &sq,QElemType &e){
e = sq.base[sq.front];
for(int i=sq.front;i<sq.rear-1;i++){
sq.base[i] = sq.base[i+1];
}
sq.rear--;
return OK;
}
void delElem(Squeue &sq){
QElemType elem;
if(isNotEmpty(sq)){
if(Dequeue(sq,elem))
cout<<"出队成功,本次出栈元素为:"<<elem<<endl;
}
showSqueue(sq);
}
步骤:
- 判断是否队空;
- 将队头位置的元素取出;
- 将后面的元素依次往前覆盖;
- 队尾减一。
遍历队
void showSqueue(Squeue sq){
cout<<"当前队元素:\n";
if(isNotEmpty(sq)){
for(int i=sq.front;i<sq.rear;i++){
cout<<sq.base[i]<<" ";
}
cout<<endl;
}
}
步骤:
- 判断队列是否为空;
- 从队头依次遍历到队尾,遍历结束。
运行截图
源代码
/*
广西师范大学 计算机科学与工程学院
GuangXi Normal University
College of Computer Science and Engineering
Student STZ
*/
#include<iostream>
#include<stdio.h>
using namespace std;
#define QElemType char
#define Status int//表示状态
#define OK 1
#define ERROR 0
#define MAXSIZE 7
typedef struct SeQueue{
QElemType *base;
int front;
int rear;
}Squeue;
Status initial(Squeue &sq){
sq.base = new QElemType[MAXSIZE];
if(!sq.base){
cout<<"初始化失败\n";
return ERROR;
}
sq.front = sq.rear = 0;
return OK;
}
Status isNotFull(Squeue sq){
if(sq.rear-sq.front == MAXSIZE){
cout<<"队满!\n";
return ERROR;
}
return OK;
}
Status isNotEmpty(Squeue sq){
if(sq.front==sq.rear){
cout<<"队空!\n";
return ERROR;
}
return OK;
}
void showSqueue(Squeue sq){
cout<<"当前队元素:\n";
if(isNotEmpty(sq)){
for(int i=sq.front;i<sq.rear;i++){
cout<<sq.base[i]<<" ";
}
cout<<endl;
}
}
Status Enqueue(Squeue &sq,QElemType e){
sq.base[sq.rear++] = e;
return OK;
}
void addElem(Squeue &sq){
QElemType elem = '0';
while(elem!='*' && isNotFull(sq)){
cout<<"请输入元素(输入*结束进队):";
cin>>elem;
if(elem!='*'){
Enqueue(sq,elem);
}
}
cout<<"入队完毕\n";
showSqueue(sq);
}
Status Dequeue(Squeue &sq,QElemType &e){
e = sq.base[sq.front++];
return OK;
}
void delElem(Squeue &sq){
QElemType elem;
if(isNotEmpty(sq)){
if(Dequeue(sq,elem))
cout<<"出队成功,本次出栈元素为:"<<elem<<endl;
}
showSqueue(sq);
}
void menu(Squeue &sq){
int c = 0;
while(1){
cout<<"请选择操作:"<<endl;
cout<<"1:入队"<<endl;
cout<<"2:出队"<<endl;
cout<<"3:遍历队"<<endl;
cout<<"4:退出"<<endl;
cin>>c;
switch(c){
case 1:addElem(sq);break;
case 2:delElem(sq);break;
case 3:showSqueue(sq);break;
case 4:return;
default:;
}
}
}
int main(){
Squeue sq;
initial(sq);
menu(sq);
return 0;
}