#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
#define Max 100
//定义栈
typedef struct {
char *base;
char *top;
int maxsize;
}stack;
//初始化
void Initstack(stack &s) {
s.base = new char[Max];
s.top = s.base ;
s.maxsize = Max;
}
//入栈
void push(stack &s, char e) { //默认栈不会满
*(s.top) = e;
s.top++;
}
//出栈
void pop(stack &s) {
//判空
if (s.top != s.base) {
s.top--;
}
}
//top
char top(stack &s) {
if (s.top != s.base) {
return *(s.top - 1);
}
}
int main() {
string ss;
while (cin >> ss && ss[0] != '0') {
stack s;
Initstack(s);
for (int i = 0; ss[i] != '\0'; i++) { //入栈
push(s, ss[i]);
}
//逆序输出
while (s.top != s.base) {
cout << top(s);
pop(s);
}
cout << endl;
}
}
BJFU 递归实现字符串的逆序存储(自定义栈)
最新推荐文章于 2024-08-29 16:39:06 发布