与队列有关的卡牌插入游戏

与队列有关的卡牌插入游戏

具体题目

#include"stdio.h"
#include"stdlib.h"

typedef struct QNode {
	int data;
	struct QNode* next;
}QNode, * QueuePtr;

typedef struct {
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

//初始化,建立队列
int InitStack(LinkQueue& S) {
	S.front = (QueuePtr)malloc(sizeof(QNode));
	//S.front=NULL;
	S.rear = S.front;
	if (!S.front)
		return 0;
	S.front->next = NULL;
	return 1;
}

//进队
int EnQueue(LinkQueue& S, int e) {
	QueuePtr p = NULL;
	p = (QueuePtr)malloc(sizeof(QNode));
	if (!p)exit(1);
	p->data = e;
	p->next = 0;
	S.rear->next = p;
	S.rear = p;
	return 1;
}

//置空队列
void ClearQueue(LinkQueue& S) {
	QueuePtr p = NULL;
	p = S.front;
	while (S.front != S.rear) {
		p = S.front->next;
		free(S.front);
		S.front = p;
	}
}

//判空队列
void QueueEmpty(LinkQueue& S) {
	if (S.front == S.rear)
		printf("判空:是\n");
	else
		printf("判空:否\n");

}

//返回队列长度
int   QueueLen(LinkQueue& S) {
	QueuePtr p = NULL;
	int len = 0;
	p = S.front;
	if (S.front == S.rear)
		return len;
	else {
		while (p != S.rear) {
			len++;
			p = p->next;
		}
		return len;
	}
}

//出队列,并把出数输入到另一个队列中
int pop(LinkQueue& S, LinkQueue& T) {
	int tem = 0;
	QueuePtr p = NULL;
	if (S.front->next == NULL)
		return 0;
	else {
		tem = S.front->next->data;
		EnQueue(T, tem);
		p = S.front->next;
		free(S.front);
		S.front = p;
		return tem;
	}
}

//出队列,并把出数输入到自己的队尾
int pop1(LinkQueue& S) {
	int tem = 0;
	QueuePtr p = NULL;
	if (S.front->next == NULL)
		return 0;
	else {
		tem = S.front->next->data;
		EnQueue(S, tem);
		p = S.front->next;
		free(S.front);
		S.front = p;
		return tem;
	}
}
//遍历输出队列
void QueueTraverse(LinkQueue& S) {
	QueuePtr p = S.front;
	while (p != S.rear) {
		printf("%d ", p->next->data);
		p = p->next;
	}

}
int main() {
	LinkQueue S;
	InitStack(S);
	LinkQueue T;
	InitStack(T);
	LinkQueue R;
	InitStack(R);
	int a, b;
	scanf_s("%d",&a);
	for (int i = 0; i < a; i++) {
		scanf_s("%d", &b);
		EnQueue(S, b);
	}
	while (QueueLen(S) > 2) {
		pop(S, T);
		pop1(S);
	}
	QueueTraverse(T);
	QueueTraverse(S);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值