rear指针指向指向【队尾元素】的三种判断队列为空为满的方法
rear 指向队尾元素(就是插入操作完成后的那个位置)
A. 牺牲一个存储单元
//
// SqQueue2.cpp
// Third
//
// Created by Jeffery on 2024/3/31.
//
#include <stdio.h>
#include <iostream>
#define MaxSize 5
using namespace std;
typedef int ElemType;
/*
rear 指向队尾元素(就是插入操作完成后的那个位置)
A. 牺牲一个存储单元
*/
// 定义结构体
typedef struct {
ElemType data[MaxSize];
int front;
int rear;
}SqQueue;
// 初始化
void InitQueueA(SqQueue &Q){
Q.rear = MaxSize - 1;
Q.front = 0;
}
// 判断队列是否为满
// 头指针在尾指针的后面两个位置,就说明队列满了
bool IsFullQueueA(SqQueue Q){
if ((Q.rear + 2) % MaxSize == Q.front) return true;
else return false;
}
// 判断队列是否为空
bool IsEmptyQueueA(SqQueue Q){
if ((Q.rear + 1) % MaxSize == Q.front) return true;
else return false;
}
// 入队
bool InQueueA(SqQueue &Q, ElemType x){
if (IsFullQueueA(Q)) {
printf("满啦,入队不了\n");
return false;
}
// 这里需要注意,我们rear所指向的位置
Q.rear = (Q.rear + 1) % MaxSize;
Q.data[Q.rear] = x;
return true;
}
// 出队
bool OutQueueA(SqQueue &Q, ElemType &x){
if(IsEmptyQueueA(Q)) {
printf("空啦,出队不了\n");
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
// 计算队列长度
int LengthQueueA(SqQueue Q){
int length = (Q.rear + MaxSize - Q.front + 1) % MaxSize;
return length;
}
// 打印没有满的队列
void PrintQueue(SqQueue Q){
for (int i = Q.front; i <= Q.rear; i ++) {
printf("%d,", Q.data[i]);
}
printf("\n");
}
// 打印满了的队列
void PrintFullQueue(SqQueue Q){
for (int i = 0; i <= MaxSize - 1; i ++) {
printf("%d,", Q.data[i]);
}
printf("\n");
}
int main(int argc, const char * argv[]) {
SqQueue Q;
int x;
InitQueueA(Q);
InQueueA(Q, 1);
InQueueA(Q, 2);
InQueueA(Q, 3);
InQueueA(Q, 4);
InQueueA(Q, 5); //因为浪费了个空间,所以这个也是插不进去的。
PrintQueue(Q);
// InQueueA(Q, 6);
OutQueueA(Q, x); // 234
OutQueueA(Q, x); // 34
OutQueueA(Q, x); // 4
PrintQueue(Q);
printf("-----------------------------------");
printf("\n");
return 0;
}
B. 用size变量记录队列的长度
// SqQueue2.cpp
// Third
//
// Created by Jeffery on 2024/3/31.
//
#include <stdio.h>
#include <iostream>
#define MaxSize 5
using namespace std;
typedef int ElemType;
// 定义结构体
typedef struct {
ElemType data[MaxSize];
int front;
int rear;
int size;
}SqQueue;
// 初始化
void InitQueueB(SqQueue &Q){
Q.rear = MaxSize - 1;
Q.front = 0;
Q.size = 0;
}
// 判断队列是否为满
bool IsFullQueueB(SqQueue Q){
if (Q.size == MaxSize) return true;
else return false;
}
// 判断队列是否为空
bool IsEmptyQueueB(SqQueue Q){
if (Q.size == 0) return true;
else return false;
}
// 入队
bool InQueueB(SqQueue &Q, ElemType x){
if (IsFullQueueB(Q)) {
printf("满啦,入队不了\n");
return false;
}
// 这里需要注意,我们rear所指向的位置
Q.rear = (Q.rear + 1) % MaxSize;
Q.data[Q.rear] = x;
Q.size ++;
return true;
}
// 出队
bool OutQueueB(SqQueue &Q, ElemType &x){
if(IsEmptyQueueB(Q)) {
printf("空啦,出队不了\n");
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.size --;
return true;
}
// 计算队列长度
int LengthQueueB(SqQueue Q){
return Q.size;
}
// 打印没有满的队列
void PrintQueue(SqQueue Q){
for (int i = Q.front; i <= Q.rear; i ++) {
printf("%d,", Q.data[i]);
}
printf("\n");
}
// 打印满了的队列
void PrintFullQueue(SqQueue Q){
for (int i = 0; i <= MaxSize - 1; i ++) {
printf("%d,", Q.data[i]);
}
printf("\n");
}
int main(int argc, const char * argv[]) {
SqQueue Q;
int x;
InitQueueB(Q);
InQueueB(Q, 1);
InQueueB(Q, 2);
InQueueB(Q, 3);
InQueueB(Q, 4);
InQueueB(Q, 5);
PrintQueue(Q);
// InQueueA(Q, 6);
OutQueueB(Q, x); // 2345
OutQueueB(Q, x); // 345
printf("队列的长度为 = %d\n", LengthQueueB(Q));
OutQueueB(Q, x); // 4
PrintQueue(Q);
printf("-----------------------------------");
printf("\n");
return 0;
}
C. 用一个标识的变量tag,去记录最后一次操作是入队操作还是出队操作
这里需要注意的是,计算队列的长度的时候,可能如果用A方法的计算列表的长度的函数,可能会出现当队列为空的时候,它计算出的值为Maxsize,这一点我们需要注意,为了方便,我们直接进行if判断就行。
/*
rear 指向队尾元素(就是插入操作完成后的那个位置)
C.用一个标识的变量tag,去记录最后一次操作是入队操作还是出队操作
*/
//
// SqQueue2.cpp
// Third
//
// Created by Jeffery on 2024/3/31.
//
#include <stdio.h>
#include <iostream>
#define MaxSize 5
using namespace std;
typedef int ElemType;
定义结构体
typedef struct {
ElemType data[MaxSize];
int front;
int rear;
int tag;
}SqQueue;
// 初始化
void InitQueueC(SqQueue &Q){
Q.rear = MaxSize - 1;
Q.front = 0;
Q.tag = 0; // 初始化为0
}
// 判断队列是否为满
bool IsFullQueueC(SqQueue Q){
if ((Q.rear + 1) % MaxSize == Q.front && Q.tag == 1) return true;
else return false;
}
// 判断队列是否为空
bool IsEmptyQueueC(SqQueue Q){
if ((Q.rear + 1) % MaxSize == Q.front && Q.tag == 0) return true;
else return false;
}
// 入队
bool InQueueC(SqQueue &Q, ElemType x){
if (IsFullQueueC(Q)) {
printf("满啦,入队不了\n");
return false;
}
// 这里需要注意,我们rear所指向的位置
Q.rear = (Q.rear + 1) % MaxSize;
Q.data[Q.rear] = x;
Q.tag = 1;
return true;
}
// 出队
bool OutQueueC(SqQueue &Q, ElemType &x){
if(IsEmptyQueueC(Q)) {
printf("空啦,出队不了\n");
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.tag = 0;
return true;
}
// 计算队列长度
int LengthQueueC(SqQueue Q){
if (IsEmptyQueueC(Q)) return 0; // 空队列长度为0
if (IsFullQueueC(Q)) return MaxSize; // 满队列长度为MaxSize
int length = (Q.rear + MaxSize - Q.front ) % MaxSize + 1;
return length;
}
int main(int argc, const char * argv[]) {
SqQueue Q;
int x;
InitQueueC(Q);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
InQueueC(Q, 1);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
InQueueC(Q, 2);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
InQueueC(Q, 3);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
InQueueC(Q, 4);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
InQueueC(Q, 5);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
// InQueueC(Q, 6); // xxx
OutQueueC(Q, x);
OutQueueC(Q, x);
printf("队列的长度为 = %d\n", LengthQueueC(Q));
OutQueueC(Q, x);
OutQueueC(Q, x);
OutQueueC(Q, x);
// OutQueueC(Q, x);// xxx
printf("%d\n",Q.data[Q.front]);
printf("-----------------------------------");
printf("\n");
return 0;
}
欢迎指正,有问题评论或者私信就可以