Status Init_SeqQueue(&Q); 初始化操作
Status Length_SeqQueue(&Q); 求表长
ElemType Get_SeqQueue(&Q); 取队首元素
Status Add_SeqQueue(&Q,e); 入队操作
ElemType De_SeqQueue(&Q); 出队操作
Status Empty_SeqQueue(&Q); 判空
SeqQueue.h
/*
*项目名: List
*文件名: SeqQueue.h
*创建者: CaoPengCheng
*创建时间:2021-4-14
*描述: 循环队列(静态)
*函数:
Status Init_SeqQueue(&Q); 初始化操作
Status Length_SeqQueue(&Q); 求表长
ElemType Get_SeqQueue(&Q); 取队首元素
Status Add_SeqQueue(&Q,e); 入队操作
ElemType De_SeqQueue(&Q); 出队操作
Status Empty_SeqQueue(&Q); 判空
*/#include"stdio.h"#include"stdlib.h"#define ElemType int#define Status int #define MAXSiZE 100 #define OK 1#define ERROE 0typedefstruct{
ElemType data[MAXSiZE];int front,rear;}SeqQueue;//初始化
Status Init_SeqQueue(SeqQueue *Q){
Q->front=0;
Q->rear=0;return OK;}//判空
Status Empty_SeqQueue(SeqQueue *Q){if(Q->front ==Q->rear )return OK;elsereturn ERROE ;}//入队
Status Add_SeqQueue(SeqQueue *Q,ElemType e){if((Q->rear+1)%MAXSiZE==Q->front){printf("队满!\n");return ERROE;}
Q->rear =(Q->rear +1)%MAXSiZE ;
Q->data[Q->rear]=e;return OK;}//取队头
ElemType Get_SeqQueue(SeqQueue *Q){
ElemType e;if(Empty_SeqQueue(Q)){printf("队空!\n");return ERROE;}
e=Q->data [(Q->front +1)%MAXSiZE ];return e;}//出队
ElemType De_SeqQueue(SeqQueue *Q){
ElemType e;if(Empty_SeqQueue(Q)){printf("队空!\n");return ERROE;}
Q->front =(Q->front +1)%MAXSiZE;//出队
e=Q->data[Q->front];return e;}//求队长度intLength_SeqQueue(SeqQueue *Q){return(Q->rear-Q->front+MAXSiZE)%MAXSiZE;}
测试
Status SeqQueueTest(){/*
Status Init_SeqQueue(&Q); 初始化操作
Status Length_SeqQueue(&Q); 求表长
ElemType Get_SeqQueue(&Q); 取队首元素
Status Add_SeqQueue(&Q,e); 入队操作
ElemType De_SeqQueue(&Q); 出队操作
Status Empty_SeqQueue(&Q); 判空
*/
SeqQueue *Q,q;int i;
Q=&q;printf("---循环队列测试---\n");Init_SeqQueue(Q);printf("入队");for(i=20;i>=0;i--)Add_SeqQueue(Q,i);printf("Success!!\n");printf("队头:%d \n",Get_SeqQueue(Q));//队尾会占用一个节点,所有长度一个比实际长度加一printf("队长:%d \n",Length_SeqQueue(Q));printf("出队:");for(i=1;i<=20;i++)printf("%d ",De_SeqQueue(Q));printf("\nover!\n");}