mirror of
https://github.com/ViolentAyang/DataStructureC.git
synced 2026-02-03 02:13:14 +08:00
Create 线性队列入队操作.c
This commit is contained in:
42
队列/线性队列入队操作.c
Normal file
42
队列/线性队列入队操作.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#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 QueueEmpty(SqQueue S){
|
||||||
|
if(S.front==S.rear){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//入队操作
|
||||||
|
bool EnQueue(SqQueue *S,int x){
|
||||||
|
if(!QueueEmpty(*S)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
S->data[S->rear] = x;
|
||||||
|
S->rear += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
SqQueue S;
|
||||||
|
InitQUeue(&S);
|
||||||
|
printf("%d\n",QueueEmpty(S));
|
||||||
|
EnQueue(&S,5);
|
||||||
|
printf("%d\n",QueueEmpty(S));
|
||||||
|
printf("%d\n",S.data[S.rear-1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user