#define MaxSize 100
#include <stdio.h>
#include<stdbool.h>
typedef struct {
int data[MaxSize];
int front;//队头元素
int rear;//队尾元素指向队尾元素下一个位置
}SqQueue;//顺序存储队列
void InitQueue(SqQueue Q) {
Q.front = Q.rear = 0;//初始化空队列
}
bool isEmpty(SqQueue Q) {
if (Q.front = Q.rear = 0)
return true;
else
return false;
}//判空
bool INQueue(SqQueue Q, int x) {
if ((Q.rear + 1) % MaxSize == Q.front){
return false;//队列已满
}
else {
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
}
return true;
}//入队
bool OUTQueue(SqQueue Q, int x) {
if (Q.rear = Q.front = 0) {
return false;//队列为空
}
else {
Q.data[Q.front] = x;
Q.front = (Q.front + 1) % MaxSize;
}
return true;
}//出队
/*为解决队列假溢出使用循环队列
队首指针进1 Q.front=(Q.front+1)%MaxSize
队尾指针进1 Q.rear=(Q.rear+1)%MaxSize
队列长度 (Q.rear+MaxSize-Q.front)%MaxSize
*/
typedef struct LNode {
int data;
struct LNode* next;
}LNode;//定义结点
typedef struct
C语言队列基本操作
于 2022-04-28 16:11:58 首次发布