栈的应用之两个栈模拟一个队列
发布日期:2021-06-29 15:42:39 浏览次数:2 分类:技术文章

本文共 717 字,大约阅读时间需要 2 分钟。

利用两个栈s1,s2模拟一个队列,已知栈的三个运算定义如下:

push(ST,x):元素x入st栈

pop(ST,&x):st栈顶元素出栈,赋给变量x

isEmpty(ST):判断ST栈是否为空

//入队列 int enQueue(SqStack &s1,SqStack &s2,int x){	int y;	if(s1.top==maxsize-1)	{		if(!isEmpty(s2))			return 0;		else if(isEmpty(s2))		{			while(!isEmpty(s1))			{				pop(s1,y);				push(s2,y);			}			push(s1,x);			return 1;		}	}	else	{		push(s1,x);		return 1;	}} //出队列int deQueue(SqStack &s2,Sqtack &s1,int &x){	int y;	if(!isEmpty(s2))	{		pop(s2,x);		return 1;	}	else if(isEmpty(s2))	{		if(isEmpty(s1))			return 0;		else		{			while(!isEmpty(s1))			{				pop(s1,y);				push(s2,y);			}			pop(s2,x);			return 1;		}	}} //判断队列是否为空int isQueueEmpty(SqStack s1,SqStack s2){	if(isEmpty(s1)&&isEmpty(s2))		return 1;	else 		return 0;}

 

转载地址:https://codingchaozhang.blog.csdn.net/article/details/85256620 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:树与二叉树之基础知识篇
下一篇:栈的应用之设计共享栈

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年05月01日 16时03分06秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章