ADD SqQueue

This commit is contained in:
Kim Yang
2020-08-01 00:20:39 +08:00
parent eeaa29aab3
commit 5b15b37696
3 changed files with 95 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
//
// Created by kim on 2020/6/28.
// Created by kim on 2020/7/28.
//
//共享顺序栈的实现

View File

@@ -1,5 +1,5 @@
//
// Created by kim on 2020/6/28.
// Created by kim on 2020/7/28.
//
//链栈的实现

View File

@@ -0,0 +1,93 @@
//
// Created by kim yang on 2020/7/31.
//
//循环顺序队列的第一种实现方式
#include <stdio.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];//
int front,rear;//对头指针和队尾指针
}SqQueue;
//初始化
void InitQueue(SqQueue &Q){
Q.rear=Q.front=0;//初始化时队头队尾都指向0
}
//判空
bool QueueEmpty(SqQueue Q){
if(Q.front==Q.rear)
return true;
else
return true;
}
//入队操作
bool EnQueue(SqQueue &Q,int t){
if((Q.rear+1)%MaxSize==Q.front)return false;//队满,注意这里的判满条件
//这里的判满条件会造成浪费一个存储空间的问题
Q.data[Q.rear]=t;
Q.rear=(Q.rear+1)%MaxSize;//通过取余操作让整个队列循环起来
return true;
}
//出队操作
bool DeQueue(SqQueue &Q,int &x){
if(Q.rear==Q.front)return false;//队空
x=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
return true;
}
//获取队头元素,用x返回
bool GetHead(SqQueue Q,int &x){
if (Q.front==Q.rear)return false;
x=Q.data[Q.front];
return true;
}
//打印整个队列
void PrintQueue(SqQueue Q){
printf("开始打印队列\n");
while(Q.front==Q.rear){
printf("Q[%d]=%d",Q.front,Q.data[Q.front]);
Q.front=(Q.front+1)%MaxSize;
}
}
//测试
void TestQueue(){
printf("开始测试!\n");
SqQueue Q;
InitQueue(Q);
if (EnQueue(Q,1)){
printf("入队成功啦!\n");
} else{
printf("入队失败了\n");
}
if (EnQueue(Q,2)){
printf("入队又成功啦!\n");
} else{
printf("入队又失败了\n");
}
PrintQueue(Q);
int x;
if (DeQueue(Q, x)){
printf("出队成功,弹出的元素为:%d\n",x);
} else{
printf("出队失败了,再检出一下吧!\n");
}
printf("结束测试!\n");
}
int main(){
TestQueue();
return 0;
}