顺序队列
本程序能够对用户的输入做了合理的处理使得用户想要删除超过本队列的最大容量时会提醒用户该操作错误,从而让用户重新输入,更人性化的提供了两种删除队列元素的方法供用户选择…
/*
完成对队列的初始化 、判断队列的空和满、插入元素、获取队列的首元素、删除元素、销毁队列、
返回队列的长度、打印队列
*/
#include<iostream>
#include<Windows.h>
using namespace std;
#define MaxSize 100
typedef int DataType;
typedef struct Queue {
DataType arr[MaxSize];
int front; //前指针
int rear; //尾指针
}sQueue;
//队列的初始化
bool InitSq(sQueue* Sq) {
if (!Sq) return false;
Sq->front = Sq->rear = 0;
return true;
}
//判断队列是否为空
bool EmptySq(sQueue* Sq) {
if (!Sq) return false;
if(Sq->rear==Sq->front){
return true;
}
return false;
}
//判断队列是否为满
bool FullSq(sQueue* Sq) {
if (!Sq) return false;
if (Sq->rear - Sq->front == MaxSize) {
return true;
}
return false;
}
//插入元素
bool InsertSq(sQueue* Sq, int &Element) {
if (!Sq) return false;
if (FullSq(Sq)) { //判断队列是否还能插入元素
cout << "队列已满,无法插入元素! " << endl;
return false;
}
Sq->arr[Sq->rear] = Element; //将元素插入到对应的位置
Sq->rear++; //插入后为指针向后移动一位
return true;
}
//打印队列元素
void PrintSq(sQueue* Sq) {
int p = Sq->front;
cout << "顺序存储的队列里面的元素是: ";
while (p<Sq->rear) {
cout << " " << Sq->arr[p];
p++;
}
cout << endl;
return;
}
//获取队列的首元素
bool GetSqFirst(sQueue* Sq, int &firstElement){
if (!Sq) return false;
if (EmptySq(Sq)) {
cout << "获取队首元素失败!,这个队列是空队列!" << endl;
return false;
} else {
firstElement = Sq->arr[Sq->front];
return true;
}
}
//删除队列里元素,将后面的元素向前面移动
bool DelSq1(sQueue* Sq, int &Element) {
if (!Sq) return false;
if (EmptySq(Sq)) {
cout << "这是一个空队列 ";
return false;
}
Element = Sq->arr[Sq->front];
for (int i = Sq->front + 1; i < Sq->rear; i++) {
Sq->arr[i-1] = Sq->arr[i];
}
Sq->rear--;
return true;
}
//删除前面的元素front向后移动,但是可能会导致假溢出
bool DelSq2(sQueue* Sq,int &Element) {
if (!Sq) return false;
if (EmptySq(Sq)) {
cout << "这是个空队列 ";
return false;
}
Element = Sq->arr[Sq->front];
Sq->arr[Sq->front] = Sq->arr[++Sq->front];
return true;
}
//返回队列的长度
int LenSq(sQueue* Sq) {
return Sq->rear - Sq->front;
}
//销毁队列
void DestroSq(sQueue* Sq) {
Sq->front = Sq->rear = 0;
}
int main(void) {
sQueue *Sq = new sQueue;
if (InitSq(Sq)) { //初始化队列
cout << "队列初始化成功! " << endl;
} else {
cout << "队列初始化失败! " << endl;
}
int n; //用户想要插入的个数
cout << "请输入你想要插入的元素的个数: ";
cin >> n;
//直到用户输入正确的个数才能进行下面的程序
while (1) {
if (n > MaxSize) {
cout << "输入的个数超过队列所能容纳的最大个数,不能插入,请用户谨慎选择输入个数! " << endl;
} else {
break;
}
}
int Element=0; // 用户要插入的元素
//插入元素
for (int i = 0; i < n;i++){
cout << "请输入你想要插入的元素 :";
cin >> Element;
if (InsertSq(Sq,Element)) {
cout << "插入元素 " << Element << "成功! " << endl;
} else {
cout << "插入失败! " << endl;
}
}
//打印队列
PrintSq(Sq);
//获取队列的首元素
int firstElement; //首元素
if (GetSqFirst(Sq,firstElement)) {
cout << "获取队列的首元素成功! " << endl;
cout << "****************************" << endl;
cout << "首元素是: " << firstElement << endl;
cout << "****************************" << endl;
} else {
cout << "获取队列的首元素失败! " << endl;
}
cout << endl;
cout << "下面开始删除元素! " << endl;
int chose;
cout << "请输入你想要删除的方式: 1后面的元素覆盖前面的值, 2直接删除前面的值(可能会导致假溢出)" << endl;
cout << "注意: 恶意输入会导致程序停止运行!!!!" << endl;
cin >> chose;
int many=0; //要删除元素的个数
cout << "请输入你想要删除的元素的个数: ";
cin >> many;
while (1) {
if (many > n) {
cout << "要删除的个数超过本队列的最大容量,请重试! " << endl;
cin >> many;
continue;
} else {
break;
}
}
for (int m=0; m < many; m++) {
switch (chose) {
case 1:
//删除队列里的元素(后面的元素覆盖前面的元素)
if (DelSq1(Sq, Element)) {
cout << "删除队列的元素成功! " << endl;
cout << "****************************" << endl;
cout << "删除的元素是: " << Element << endl;
cout << "****************************" << endl;
} else {
cout << "删除元素失败,队列里没有该元素! " << endl;
}
break;
case 2:
//删除前面的元素front向后移动,但是可能会导致假溢出
if (DelSq2(Sq, Element)) {
cout << "删除队列的元素成功! " << endl;
cout << "****************************" << endl;
cout << "删除的元素是: " << Element << endl;
cout << "****************************" << endl;
} else {
cout << "删除元素失败,队列里没有该元素! " << endl;
}
break;
default:
cout << "恶意输入!!!!!!!!!!,程序结束!!!" << endl;
system("pause");
return 1;
}
}
PrintSq(Sq);
//返回队列的长度
int len =LenSq(Sq);
cout << "该队列的长度是: " << len << endl;
//销毁队列
cout << "销毁队列中... " << endl;
DestroSq(Sq);
PrintSq(Sq);
system("pause");
return 0;
}
用户恶意选择删除程序时:
用户恶意选择删除元素长度超过队列长度是:
第二种删除方式同样具有以上功能: