当前位置:首页 > 通信资讯 > 正文

c语言颠倒字符串(c语言字符串颠倒函数编写)

本文实例讲述了C语言实现颠倒栈的方法,很实用的技巧。分享给大家供大家参考之用。

具体实现方法如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include <iostream> #include <iterator> #include <algorithm> #include <vector> #include <stack> using namespace std; void initializeStack(stack<int> &st) { for(int i = 1; i <= 5; i++) st.push(i); } void addToStack(stack<int>& st, int i) { if(st.empty()) st.push(i); else { int top = st.top(); st.pop(); addToStack(st, i); st.push(top); } } void reverseStack(stack<int> &st) { if(st.empty()) return; int top = st.top(); st.pop(); reverseStack(st); addToStack(st, top); } void print(stack<int> st) { if(st.empty()) return; else { int top = st.top(); st.pop(); print(st); cout << top << " "; } } void main() { stack<int> st; initializeStack(st); print(st); cout << endl; reverseStack(st); print(st); }

希望本文所述对大家C程序算法设计的学习有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。