注意:全局变量front,rear,以及打印方法showDu对front,rear的改变。
include<stdio.h>
include<stdlib.h>
define Maxsize 3
typedef struct duilie {
float datas[Maxsize];
int front;
int rear;
}Du;
void Intial(Du *du) {
du->front = 0;
du->rear = 0;
}
//入队
void pushDui(Du *du,float m) {
if(du->rear==Maxsize){ printf("队列已经满了,不能再入队了!"); return -1;
}
du->datas[du->rear] = m;
du->rear = du->rear + 1;
}
//出队
float popDui(Du *du) {
if ( du->front == du->rear ) { printf("队列为空了,不能再出队了!"); return -1;
}
float dat = du->datas[du->front];
du->front = du->front + 1;
return dat;
}
void showDu(Du *du) {
int r = du->front;
int z = du->rear;
while (r != z) {
printf("%f->",du->datas[r]);
r = r + 1;
}
}
void main() {
Du du = (Du)malloc(sizeof(Du));
Intial(du);
float a;
pushDui(du, 3.5);
pushDui(du, 6.6);
pushDui(du, 8.6);
pushDui(du, 8.6);
showDu(du); printf("\n");
a=popDui(du); printf("%f\n",a);
a = popDui(du); printf("%f\n", a);
popDui(du);
popDui(du);
//showDu(du); printf("\n");
}