输入一个字符串,判断该字符串是否为回文。回文就是字符串中心对称,从左向右读和从右向左读的内容是一样的。(不含空格)
#include<bits/stdc++.h> using namespace std; #define MAX 100 typedef struct { char *base; char *top; int stacksize; }SqStack; void InitStack(SqStack &s) { s.base = new char[MAX]; if( !s.base ) exit(0); s.top = s.base; s.stacksize = MAX; } void Push(SqStack &s , char e) { if(s.top - s.base == s.stacksize) return ; if(e == ' ') { printf("入栈不成功"); exit(0); } *s.top++ = e; } char Pop(SqStack &s , char &e) { if(s.top == s.base) return 0; return e = *--s.top; } int main() { char data[MAX]; int n; cin>>n; getchar(); for(int i = 0 ; i < n; i++) { data[i] = getchar(); } SqStack s; InitStack(s); for(int i = 0 ; i < n; i++) { Push(s , data[i]); } for(int i = 0 ; i < n; i++) { char e; if(data[i] != Pop(s , e) ) { printf("此字符串不是回文串"); return 0; } } printf("此字符串是回文串"); }
标签:SqStack,实现,top,char,int,base,回文 From: https://www.cnblogs.com/fan-wang/p/16823510.html