队列题目

2023-06-21 0 536

结构体

//队列结构体定义
//静态队列
typedef struct {
	int data[MAXSIZE];
	int rear,front;
	int tag; 
}SqQueue;

//栈的定义
typedef struct{
	int data[MAXSIZE];
	int top;
}SqStack;

队列题目:

//1.tag队列进队出队 
//tag队列进队
bool EnQueue(SqQueue &q,int x){
	if(q.rear == q.front && tag == 1)return false;
	else{
		q.tag = 1;
		q.data[q.rear] = x;
		q.rear = (q.rear+1)%MAXSIZE;
		return true;
	}
} 

//tag队列出队
bool DeQueue(SqQueue &q,int &e){
	if(q.rear == q.rear && q.tag == 0)return false;
	else{
		q.tag = 0;
		e = q.data[q.front];
		q.front = (q.front+1)%MAXSIZE;
		return true;
	}
}
//2.使用一个栈,将队列里的元素逆转
bool reverseByStack(SqQueue &q,SqStack &s){
	if(q.front == q.rear)return false;//队空
	//队列放入栈内 
	while(!EmptyQueue(q)){
		DeQueue(q,m);
		Push(s,m);
	} 
	//栈内元素放入队列 
	while(!EmptyStack(s)){
		Pop(s,m);
		EnQueue(q,m);
	}
}
//3.利用两个栈来模拟一个队列
//a为s1,b为s2 
//进队
bool EnQueueByStack(SqStack &a,SqStack &b,int x){
	if(!StackOverflow(a)){
		Push(a,x);
		return true;
	}
	if(StackOverflow(a) && StackEmpty(b)){
		int m;
		while(!StackEmpty(a)){
			Pop(a,m);
			Push(b,m);
		}
		Push(a,x);
		return true;
	}
	if(StackOverflow(a) && StackOverflow(b)){
		return false; //两个栈都满了 
	}
} 
//出队
bool DeQueueByStack(SqStack &a,SqStack &b,int &e){
	if(!StackEmpty(b)){
		Pop(b,e);
		return true;
	}else if(StackEmpty(a)){
		return false; //两个栈都没有元素 
	}else{
		int m;
		while(!StackEmpty(a)){
			Pop(a,m);
			Push(b,m);
		}
		Pop(b,e);
		return true;
	}
	
} 

//判队空
bool EmptyQueue(SqStack a,SqStack b){
	if(StackEmpty(a) && StackEmpty(b))return true;
	else return false;
} 
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

本站部分资源收集自互联网,我站仅分享内容,仅供用户个人研究,不提供商业使用,如有侵权下架处理,觉得资源内容不错请您在官方渠道购买正版,您在阅读的同时下载的内容本站不承担任何法律责任!Some resources of this website are collected from the Internet. We only share the content, which is only for personal research of users, and not for commercial use. If there is infringement and off shelf processing, please purchase the authentic version of the resource content from the official channel if you think it is good. We will not bear any legal responsibility for the content you download while reading!

橙凰素材-精品源码下载|正版主题更新 学习交流 队列题目 https://b.bqzmz.com/2023/06/21/%e9%98%9f%e5%88%97%e9%a2%98%e7%9b%ae/

为用户提供极致的体验

常见问题
  • 正版主题是指站长在指定渠道购买的官方主题,提供作者联系,资源支持,长期更新等等内容
查看详情
  • 非常抱歉,请联系网站页脚的客服,我们将在核实后下架处理并返还给您此资源的收益(会扣去网站运营的费用),谢谢理解!
查看详情
  • 网络资源是站长从互联网分享收集而来的内容,本站不会做测试,直接分享,不包源码可用性,运营性,如有需求点击右边的联系正版,谢谢!
查看详情
  • 如果资源不是最新版本,请在资源下方评论区进行催更或提交工单处理,我们会尽快更新并通过邮箱通知您。
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务