使用队列结构打印天干地支表

#include<stdio.h>
#include<stdlib.h>

typedef struct queue {
	char** data;
	int head, tail, size, count;
}queue;
queue* makequeue(int n) {
	queue* q = (queue*)malloc(sizeof(queue));
	q->data = (char**)malloc(sizeof(char*) * n);
	q->head = q->tail = q->count = 0;
	q->size = n;
	return q;
}
int push(queue* q, char* val) {
	if (q->count == q->size) return 0;
	q->data[q->tail] = val;
	q->tail += 1;

	if (q->tail == q->size) {
		q->tail = 0;
	}
	q->count += 1;
	return 1;
}
char* pop(queue* q) {
	q->head += 1;
	if (q->head == q->size) q->head = 0;
	q->count -= 1;
	if (q->head == 0) return q->data[q->size - 1];
	return q->data[q->head - 1];
}


int main() {
	char sky[10][3] = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
	char land[12][3] = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
	queue* s = makequeue(10);
	queue* l = makequeue(12);
	for (int i = 0; i < 10; i++) {
		push(s, sky[i]);
	}
	for (int i = 0; i < 12; i++) {
		push(l, land[i]);
	}
	printf("                               天干地支表\n");
	for (int i = 0; i < 60; i++) {
		printf("|%02d%s%s", i + 1, pop(s), pop(l));
		int flag = i + 1;
		if (flag % 10 == 0) printf("|\n");
	}
	return 0;
}

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值