首先当然是头文件啦
#include<stdio.h>
typedef int ElemType;
#define MAX_SIZE 9
typedef struct Queue
{
ElemType arr[MAX_SIZE];
int front;
int rear;
}Queue,*pqueue;
void init(pqueue ptr);
int full(pqueue ptr);
int enqueue(pqueue ptr,ElemType val);
int empty(pqueue ptr);
int front(pqueue ptr);
int rear(pqueue ptr);
函数的声明
#include"Queue.h"
void init(pqueue ptr) //初始化循环队列
{
if(ptr!=NULL)
{
ptr->front=ptr->rear=0;//队列的前指针和后指针相等并且都为0号下标即为初始化
}
}
int full(pqueue ptr)//判满循环队列
{
return (ptr->rear+1)%MAX_SIZEptr->front?1:0;
}
int enqueue(pqueue ptr,ElemType val)//插入一个队列并数据置为val
{
if(full(ptr))
{
return 0;
}
ptr->arr[ptr->rear]=val;
ptr->rear=(ptr->rear+1)%MAX_SIZE;
return 1;
}
int empty(pqueue ptr)//判空队列
{
return ptr->rearptr->front?1:0;
}
int dequeue(pqueue ptr)//出队列 也就是单纯的删除队列元素
{
if(empty(ptr))
{
return 0;
}
return ptr->front=(ptr->front+1)%MAX_SIZE;
}
int front(pqueue ptr)//获取front的元素
{
if(empty(ptr))
{
return 0;
}
return ptr->arr[ptr->front];
}
int rear(pqueue ptr)//获rear的元素
{
if(empty(ptr))
{
return 0;
}
return ptr->arr[(ptr->rear)%MAX_SIZE-1];
}
**测试来咯来咯
**#include<stdio.h>
#include"Queue.h"
int main()
{
Queue que;
init(&que);
for(int i=0;i<8;i++)
{
enqueue(&que,i+1);
}
int rtfront=front(&que);
int rtback=rear(&que);
printf("%d\n",rtfront);
printf("%d\n",rtback);
return 0;
}