mirror of
https://github.com/ViolentAyang/DataStructureC.git
synced 2026-02-02 18:10:59 +08:00
79 lines
1.6 KiB
C
79 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#define MaxSize 10
|
|
|
|
//栈是只允许在一段进行插入或删除操作的线性表
|
|
//队列只允许在一端进行插入,在另一端进行删除的线性表
|
|
//这里的顺序队列以循环队列为例
|
|
|
|
typedef struct{
|
|
int data[MaxSize];
|
|
int front,rear;
|
|
}SqQueue;
|
|
//初始化队列
|
|
void InitQUeue(SqQueue *S){
|
|
S->front = S->rear = 0;
|
|
}
|
|
//判断队列是否满
|
|
bool QueueFull(SqQueue S){
|
|
if((S.rear+1)%MaxSize==S.front){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
//判断队列是否为空
|
|
bool QueueEmpty(SqQueue S){
|
|
if(S.front==S.rear){
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
//入队操作
|
|
bool EnQueue(SqQueue *S,int x){
|
|
if(QueueFull(*S)){
|
|
return false;
|
|
}
|
|
S->data[S->rear] = x;
|
|
S->rear = (S->rear+1)%MaxSize;
|
|
return true;
|
|
}
|
|
//出队操作
|
|
bool DeQueue(SqQueue *S,int* x){
|
|
if(QueueEmpty(*S)){
|
|
return false;
|
|
}
|
|
*x = S->data[S->front];
|
|
S->front = (S->front+1)%MaxSize;
|
|
return true;
|
|
}
|
|
void PrintQueue(SqQueue S){
|
|
for(int i=S.front;i<S.rear;i++){
|
|
printf("%d\n",S.data[i]);
|
|
}
|
|
}
|
|
int main(){
|
|
SqQueue S;
|
|
InitQUeue(&S);
|
|
int x=0;
|
|
printf("是否为空:%d\n",QueueEmpty(S));
|
|
for(int i=1;i<=9;i++){
|
|
EnQueue(&S,i);
|
|
}
|
|
PrintQueue(S);
|
|
printf("是否为满:%d\n",QueueFull(S));
|
|
printf("是否为空:%d\n",QueueEmpty(S));
|
|
printf("当前队尾元素:%d\n",S.data[S.rear-1]);
|
|
DeQueue(&S,&x);
|
|
printf("当前出队的元素为:%d\n",x);
|
|
DeQueue(&S,&x);
|
|
printf("当前出队的元素为:%d\n",x);
|
|
DeQueue(&S,&x);
|
|
printf("当前出队的元素为:%d\n",x);
|
|
printf("是否为满:%d\n",QueueFull(S));
|
|
printf("是否为空:%d\n",QueueEmpty(S));
|
|
PrintQueue(S);
|
|
return 0;
|
|
}
|