文章目录
1.题目描述
2.算法实现
1.题目描述
回文是指正读反读均相同的字符序列;如“abba”和“abdba”均是回文,但“good”不是回文。试写一个算法判定给定的字符序列是否是回文。(提示:将一半字符入栈)
2.算法实现
#include<bits/stdc++.h>
using namespace std;#define MAXSIZE 100
typedef struct
{int *base;int *top;int stacksize;
} SqStack;void InitStack(SqStack &S)
{S.base=new int [MAXSIZE];if(!S.base) return;S.top=S.base;S.stacksize=MAXSIZE;
}void Push(SqStack &S,char e)
{if(S.top-S.base==S.stacksize) return ;*S.top++=e;
}void Pop(SqStack &S,char &e)
{if(S.base==S.top) return ;e=*--S.top;
}int main()
{SqStack S;char t[20];int len,i;char c;InitStack(S);cout<<"请输入字符t:";cin>>t;len =strlen(t);for(i=0; i<len/2; i++){Push(S,t[i]);while(S.base!=S.top){Pop(S,c);if(c!=t[i]){cout<<"不是回文!\n";return 0;}else i++;}}cout<<"是回文!";return 0;
}