mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
✨ Add LinkQueue
This commit is contained in:
@@ -5,97 +5,99 @@
|
||||
//循环顺序队列的第二种实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MaxSize 10
|
||||
|
||||
typedef struct {
|
||||
int data[MaxSize];//
|
||||
int front,rear;//对头指针和队尾指针
|
||||
int front, rear;//对头指针和队尾指针
|
||||
int size;//利用size变量记录队列长度,并用作判满的条件!有了size就不会浪费一个存储空间
|
||||
}SqQueue;
|
||||
} SqQueue;
|
||||
|
||||
//初始化
|
||||
void InitQueue(SqQueue &Q){
|
||||
Q.rear=Q.front=0;//初始化时,队头队尾都指向0
|
||||
Q.size=0;//初试长度
|
||||
void InitQueue(SqQueue &Q) {
|
||||
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
|
||||
Q.size = 0;//初试长度
|
||||
}
|
||||
|
||||
//判空
|
||||
bool QueueEmpty(SqQueue Q){
|
||||
if(Q.size==0)//有了size,条件不一样了
|
||||
bool QueueEmpty(SqQueue Q) {
|
||||
if (Q.size == 0)//有了size,条件不一样了
|
||||
return true;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
//入队操作
|
||||
bool EnQueue(SqQueue &Q,int t){
|
||||
if(Q.size==MaxSize)return false;//队满,注意这里的判满条件
|
||||
Q.data[Q.rear]=t;
|
||||
Q.rear=(Q.rear+1)%MaxSize;//通过取余操作让整个队列循环起来
|
||||
bool EnQueue(SqQueue &Q, int t) {
|
||||
if (Q.size == MaxSize)return false;//队满,注意这里的判满条件
|
||||
Q.data[Q.rear] = t;
|
||||
Q.rear = (Q.rear + 1) % MaxSize;//通过取余操作让整个队列循环起来
|
||||
Q.size++;
|
||||
return true;
|
||||
}
|
||||
|
||||
//出队操作
|
||||
bool DeQueue(SqQueue &Q,int &x){
|
||||
if(Q.size==0)return false;//队空
|
||||
x=Q.data[Q.front];
|
||||
Q.front=(Q.front+1)%MaxSize;
|
||||
bool DeQueue(SqQueue &Q, int &x) {
|
||||
if (Q.size == 0)return false;//队空
|
||||
x = Q.data[Q.front];
|
||||
Q.front = (Q.front + 1) % MaxSize;
|
||||
Q.size--;
|
||||
return true;
|
||||
}
|
||||
|
||||
//获取队头元素,用x返回
|
||||
bool GetHead(SqQueue Q,int &x){
|
||||
if (Q.size==0)return false;
|
||||
x=Q.data[Q.front];
|
||||
bool GetHead(SqQueue Q, int &x) {
|
||||
if (Q.size == 0)return false;
|
||||
x = Q.data[Q.front];
|
||||
return true;
|
||||
}
|
||||
|
||||
//打印整个队列
|
||||
void PrintQueue(SqQueue Q){
|
||||
void PrintQueue(SqQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
while(Q.front!=Q.rear){
|
||||
printf("Q[%d]=%d\n",Q.front,Q.data[Q.front]);
|
||||
Q.front=(Q.front+1)%MaxSize;
|
||||
while (Q.front != Q.rear) {
|
||||
printf("Q[%d]=%d\n", Q.front, Q.data[Q.front]);
|
||||
Q.front = (Q.front + 1) % MaxSize;
|
||||
}
|
||||
printf("打印完毕!\n");
|
||||
}
|
||||
|
||||
//测试
|
||||
void TestQueue(){
|
||||
void TestQueue() {
|
||||
printf("开始测试!\n");
|
||||
SqQueue Q;
|
||||
InitQueue(Q);
|
||||
if (EnQueue(Q,1)){
|
||||
if (EnQueue(Q, 1)) {
|
||||
printf("入队成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队失败了\n");
|
||||
}
|
||||
if (EnQueue(Q,2)){
|
||||
if (EnQueue(Q, 2)) {
|
||||
printf("入队又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队又失败了\n");
|
||||
}
|
||||
PrintQueue(Q);
|
||||
int x;
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(GetHead(Q,x)){
|
||||
printf("获取队头成功!,队头元素为:%d\n",x);
|
||||
} else{
|
||||
if (GetHead(Q, x)) {
|
||||
printf("获取队头成功!,队头元素为:%d\n", x);
|
||||
} else {
|
||||
printf("获取队头元素失败!\n");
|
||||
}
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(QueueEmpty(Q)){
|
||||
if (QueueEmpty(Q)) {
|
||||
printf("队空啦\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("队非空\n");
|
||||
}
|
||||
|
||||
@@ -103,7 +105,7 @@ void TestQueue(){
|
||||
printf("结束测试!\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
int main() {
|
||||
TestQueue();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user