数据结构——顺序队列1(声明、初始化、判断队空/队满、入队、出队、遍历队)

以下是数据结构中关于顺序队列的声明、初始化、判断空与满、出队、入队、遍历队等基础操作(编程风格参考严蔚敏数据结构)。

头文件及宏

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

步骤:

  1. 声明顺序队列sq;
  2. 给sq的base数组分配内存空间;
  3. 最初因为没有元素在数组内,队头队尾均设为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;
}

步骤:

  1. 如果队头和队尾的值相等,说明队列是空的;
  2. 如果队尾减去队头等于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. 判断是否队满;
  2. 将添加进队尾位置;
  3. 队尾自增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);
}

步骤:

  1. 判断是否队空;
  2. 将队头位置的元素取出;
  3. 将后面的元素依次往前覆盖;
  4. 队尾减一。

遍历队

void showSqueue(Squeue sq){
	cout<<"当前队元素:\n";
	if(isNotEmpty(sq)){
		for(int i=sq.front;i<sq.rear;i++){
			cout<<sq.base[i]<<" ";
		} 
		cout<<endl;
	}
}

步骤:

  1. 判断队列是否为空;
  2. 从队头依次遍历到队尾,遍历结束。

运行截图

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

源代码

/*
		广西师范大学 计算机科学与工程学院 
		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;
}
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芣苢的成长之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值