3.7 按升序对一个字符串栈进行排序

程序代码:

#include"sqstack.cpp"
void StackSort(SqStack *&st)
{
	SqStack *tmpst;
	InitStack(tmpst);
	ElemType e,e1;
	while(!StackEmpty(st))
	{
		Pop(st,e);
		printf("   st:出栈%c =>",e);
		while(!StackEmpty(tmpst))
		{
			GetTop(tmpst,e1);
			printf(" tmpst:取栈顶元素%c ",e1);
			if(e1>e)
			{
				printf("因%c>%c",e1,e);
				printf("tmpst:退栈%c",e1);
				Pop(tmpst,e1);
				printf("s:进栈%c",e1);
				Push(st,e1);
			}
			else
			{
				printf("因%c<%c ,退出循环",e1,e);
				break;
			}
		}
		Push(tmpst,e);
		printf(" tmpst:进栈%c\n",e);
	}
	while(!StackEmpty(tmpst))
	{
		Pop(tmpst,e);
		Push(st,e);
	}
	
	DestroyStack(tmpst);
 } 


int main()
{
	ElemType e;
	SqStack *s;
	InitStack(s);
	printf(" (1)依次进栈元素 1,3,4,2\n");
	Push(s,'1');
	Push(s,'3');
	Push(s,'4');
	Push(s,'2');
	printf(" (2)栈s排序过程:\n");
	StackSort(s); 
	printf(" (3)栈s排序完毕\n");
	printf(" (4)s的出栈序列:");
	while(!StackEmpty(s))
	{
		Pop(s,e);
		printf("%c ",e);
		
	}
	printf("\n");
	DestroyStack(s);
	return 1;
}
运行截图:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值