用c语言创建两个循环队列,一个循环用来存放十天干,一个循环列表存放十二地支,若天干队列为空,则让十天干入队列;若地支为空则让十地支依次入队列;若两个队都不为空,两个队列各取一个元素出列,并在屏幕上输出配对的天干地支;直到输出六十个甲子,程序结束
时间: 2023-05-28 18:06:53 浏览: 105
#include <stdio.h>
#define MAXSIZE 10
typedef struct {
char data[MAXSIZE];
int front; // 队头指针
int rear; // 队尾指针
} Queue;
void InitQueue(Queue *q) {
q->front = q->rear = 0;
}
int QueueEmpty(Queue q) {
return q.front == q.rear;
}
int QueueFull(Queue q) {
return (q.rear + 1) % MAXSIZE == q.front;
}
void EnQueue(Queue *q, char x) {
if (QueueFull(*q)) {
printf("Queue is full.\n");
return;
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAXSIZE;
}
char DeQueue(Queue *q) {
if (QueueEmpty(*q)) {
printf("Queue is empty.\n");
return '\0';
}
char x = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return x;
}
int main() {
Queue tianGan, diZhi;
InitQueue(&tianGan);
InitQueue(&diZhi);
int count = 0;
while (count < 60) {
if (QueueEmpty(tianGan)) {
EnQueue(&tianGan, '甲');
}
if (QueueEmpty(diZhi)) {
EnQueue(&diZhi, '子');
EnQueue(&diZhi, '丑');
EnQueue(&diZhi, '寅');
EnQueue(&diZhi, '卯');
EnQueue(&diZhi, '辰');
EnQueue(&diZhi, '巳');
EnQueue(&diZhi, '午');
EnQueue(&diZhi, '未');
EnQueue(&diZhi, '申');
EnQueue(&diZhi, '酉');
EnQueue(&diZhi, '戌');
EnQueue(&diZhi, '亥');
}
char t = DeQueue(&tianGan);
char d = DeQueue(&diZhi);
printf("%c%c ", t, d);
count++;
}
return 0;
}
阅读全文
相关推荐







