栈和队列

文章介绍了如何使用编程中的栈和队列数据结构来模拟川剧中的扯脸技巧以及天干地支纪年的队列操作。通过栈实现变脸过程中的脸谱顺序管理,队列用于天干地支的循环输出。
摘要由CSDN通过智能技术生成

1.变脸是运用在川剧艺术中塑造人物的一种特技。“扯脸”是比较复杂的一种变脸方法。它是事前将脸谱画在一张一张的绸子上,剪好,每张脸谱上都系一把丝线,再一张一张地贴在脸上。丝线则系在衣服的某一个顺手而又不引人注目的地方(如腰带上之类)。随著剧情的进展,在舞蹈动作的掩护下,一张一张地将它扯下来。

假设所贴脸谱的颜色顺序是白色、黄色、黑色、红色、青色、金色、银色,请用所学知识输出扯脸的序列。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 20
char color[8][10] = {"1", "白色","黄色", "黑色", "红色", "青色", "金色", "银色" };

typedef struct lianpu{
char color[10];
int num;
}ElemType;


//创建栈 
 struct Node{
	ElemType *p;
	int Top;
	int size;
}stack[20];

// 入栈
void push( ElemType x){
	int Top = stack[0].Top;
	Top++;
	stack[Top].p = (ElemType*)malloc(sizeof(ElemType));
	strcpy(stack[Top].p->color , x.color);
	stack[Top].p->num = x.num;
	stack[0].Top = Top;

}

// 出栈
void pop(){
		int Top = stack[0].Top;
	printf("%d %s\n",stack[Top].p->num, stack[Top].p->color);
	stack[0].Top = --Top;
}



int main(){

	int i;
	 stack[0].Top = 0;
	ElemType x;
	for(i = 1; i < 8; i++){
		x.num = i;
		strcpy(x.color, color[i]);
		push( x);
	}
	
	printf("扯脸顺序:\n");
	
	for(i = 1; i < 8; i++){
		pop();
	}
	
}

2.队列:天干地支纪年法,源于中国。中国自古便有十天干与十二地支,简称“干支”,取意于树木的干和枝。十天干即:甲、乙、丙、丁、戊(wù)、己、庚、辛、壬(rén)、癸(guǐ);十二地支即:子、丑、、卯(mǎo)、辰、(sì)、(wèi)、申、(yǒu)、(xu一声)、。十二地支又与十二生肖对应:子鼠、丑牛、寅虎、卯兔、辰龙、巳蛇、午马、未羊、申猴、酉鸡、戌狗、亥猪。十天干和十二地支依次相配,组成六十个基本单位,即六十甲子

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char TianGan[][4] = {{"1"},{"甲"},{"乙"},{"丙"},{"丁"},{"戊"},{"己"},{"庚"},{"辛"},{"壬"},{"癸"}};
char DiZhi[][4] = {{"1"},{"子"},{"丑"},{"寅"},{"卯"},{"辰"},{"巳"},{"午"},{"未"},{"申"},{"酉"},{"戌"},{"亥"}};

//创建天干队列
 struct a{
	char name[10];
	int MaxSize;
	int front, rear;
}Queue1[11];

//创建地支队列
 struct b{
	char name[10];
	int MaxSize;
	int front, rear;
}Queue2[13];



//天干入队
void InQueue1(){
	int front = 0, rear = 0,  i = 1;
	while(1){
		strcpy(Queue1[i].name,TianGan[i]);
		rear++;
		i++;
		if((rear + 1) % Queue1[0].MaxSize == front)break;
	}
	Queue1[0].rear = rear;
} 

//地支入队
void InQueue2(){
	int front = 0, rear = 0, i = 1;
	while(1){
		strcpy(Queue2[i].name, DiZhi[i]);
		rear++;
		i++;
		if((rear + 1) % Queue2[0].MaxSize == front)break;
	}
	Queue2[0].rear = rear;
	
} 

//天干出队 
void OutQueue1(){
	int front = 0;
	front = Queue1[0].front;
	front++;
	Queue1[0].front = front % 11; 
		printf("%s", Queue1[front].name);

} 

//地支出队 
void OutQueue2(){
	int front = 0;
	front = Queue2[0].front;
	front++;
		Queue2[0].front = front % 13; 
		printf("%s ", Queue2[front].name);
} 

//判断队列是否为空
bool IsEmpty1(){
	
	if(Queue1[0].front == Queue1[0].rear) return true;
	else return false;
}

//判断队列是否为空
bool IsEmpty2(){
	
	if(Queue2[0].front == Queue2[0].rear) return true;
	else return false;
}

int main(){
	Queue1[0].front = 0;
 Queue1[0].rear = 0;
Queue2[0].front = 0;
Queue2[0].rear = 0;
Queue1[0].MaxSize = 11;
Queue2[0].MaxSize = 13;

	int i;
	for(i = 1; i <= 60; i++){
//如果队列为空调用入队函数并初始化front	
	if(IsEmpty1()) {
	InQueue1(); 
	Queue1[0].front = 0;
	}
	
    if(IsEmpty2()){
    	InQueue2(); 
    	Queue2[0].front = 0;
	}
	//出队并输出 
	 OutQueue1();
	 OutQueue2();
	
	if(i % 10 == 0)printf("\n");
	} 
	
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值