用棧判斷迴文串
阿新 • • 發佈:2018-12-21
#include <iostream> #include <cstdlib> #include <string> using namespace std; const int MaxSize = 50; struct SqStack { char data[MaxSize]; int top; }; void InitStack(SqStack *&s); bool Push(SqStack *s,char e); char Pop(SqStack *s); void InitStack(SqStack *&s) { s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1; } bool Push(SqStack *s,char e) { if(s->top==MaxSize-1) return false; else { s->top++; s->data[s->top]=e; } return true; } char Pop(SqStack *s) { return (s->data[s->top--]); } int main() { SqStack *s; bool flag = true; char str[MaxSize]; InitStack(s); cin>>str; for(int i = 0; str[i] != '\0'; i++) Push(s,str[i]); for(int j = 0; str[j] != '\0'; j++) { if(str[j]!=Pop(s)) { flag = false; break; } } if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }