C语言实现颠倒栈的方法
本文实例讲述了C语言实现颠倒栈的方法,很实用的技巧。分享给大家供大家参考之用。
具体实现方法如下:
#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
#include<stack>
usingnamespacestd;
voidinitializeStack(stack<int>&st)
{
for(inti=1;i<=5;i++)
st.push(i);
}
voidaddToStack(stack<int>&st,inti)
{
if(st.empty())
st.push(i);
else{
inttop=st.top();
st.pop();
addToStack(st,i);
st.push(top);
}
}
voidreverseStack(stack<int>&st)
{
if(st.empty())
return;
inttop=st.top();
st.pop();
reverseStack(st);
addToStack(st,top);
}
voidprint(stack<int>st)
{
if(st.empty())
return;
else{
inttop=st.top();
st.pop();
print(st);
cout<<top<<"";
}
}
voidmain()
{
stack<int>st;
initializeStack(st);
print(st);
cout<<endl;
reverseStack(st);
print(st);
}
希望本文所述对大家C程序算法设计的学习有所帮助。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短