您的位置:首页 > 其它

利用递归实现栈的逆转(转何海涛)

2011-05-24 00:04 211 查看
程序有错误,未调试出来

// test44.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
void add_to_bottom(char temp,stack<char> &stack){
if(stack.empty())
stack.push(temp);
else{
char temp1=stack.top();
stack.pop();
add_to_bottom(temp,stack);
stack.push(temp1);
}
}
void reverse(stack<char>& stack){
if(!stack.empty()){
char temp=stack.top();
stack.pop();
reverse(stack);
add_to_bottom(temp,stack);
}
}
void main(int argc, char* argv[])
{
stack <char> s1;
s1.push('a');
s1.push('b');
s1.push('c');
s1.push('d');
reverse(s1);
while(!s1.empty()){
printf("%c ",s1.top());
s1.pop();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: