直接上代码了,实在没啥好说的;
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct {
int *base;
int front; //队头
int rear; //队尾
}SqQueue;
void InitQ(SqQueue &Q,int N);
void AddQ(SqQueue &Q, int x );
Status DeleteQ(SqQueue &Q,int &e);
void printQ(SqQueue Q); //输出队列元素及其下标位置
int N;
int main()
{
int x,e,i;
SqQueue Q;
scanf("%d",&N); //输入循环队列空间大小
InitQ(Q,N);
for(i=0;i<5;i++){
scanf("%d",&x);
AddQ(Q,x);
}
printQ(Q); //输出队列中的各元素
for(i=0;i<5;i++){
if(DeleteQ(Q,e)==OK) printf("%d is out.\n",e);
}
for(i=0;i<5;i++){
scanf("%d",&x);
AddQ(Q,x);
}
printQ(Q);
return 0;
}
void printQ(SqQueue Q){
int i;
i=Q.front;
while(i!=Q.rear){
printf("%d(%d) ",Q.base[i],i);
i=(i+1)%N;
}
printf("\n");
}
/* 请在这里填写答案 */
void InitQ(SqQueue &Q,int N)
{
//Q = (SqQueue*)malloc(sizeof(SqQueue));
Q.base = (int *)malloc(N*sizeof(int));
Q.front = 0;
Q.rear = 0;
}
void AddQ(SqQueue &Q, int x )
{
if((Q.rear+1) % N == Q.front)
{
return;
}
Q.base[Q.rear % N] = x;
Q.rear = (Q.rear +1) % N;
}
Status DeleteQ(SqQueue &Q,int &e)
{
if(Q.front == Q.rear)
{
return 0;
}
e = Q.base[Q.front % N];
Q.front++;
return 1;
}
测试结果:
6
1 2 3 4 5 6 7 8
1(0) 2(1) 3(2) 4(3) 5(4)
1 is out.
2 is out.
3 is out.
4 is out.
5 is out.
9 10
6(5) 7(0) 8(1) 9(2) 10(3)