1_5用一个栈实现另一个栈的排序

1. 题目: 一个栈中元素的类型为整形,现在想将该栈从顶到底按从大到小的顺序,只需申请一个栈。

2,.思路:我能说最开始我没有读懂题目吗,囧囧囧,语文不好的人不想再解释啦!申请一个辅助栈,存储排序的元素。

3. Answer

#include 
#include 
using namespace std;

void sortStack(stack &input)
{
	stack help;
	while (!input.empty())
	{
		int cur = input.top();
		input.pop();
		while (!help.empty() && help.top() input;
	for (int i = 8; i >4;i--)
	{
		input.push(i);
	}

	for (int i = 5; i >0 ; i--)
	{
		input.push(i);
	}

	cout<


你可能感兴趣的:(《程序员代码面试指南》学习笔记)