回文串判定(栈)

来源:互联网 发布:手机淘宝如何删除好评 编辑:程序博客网 时间:2024/05/17 07:12

Think:
思路就是 将整个栈存入, 然后 分别从 栈顶和栈底遍历

Problem Description

输入一串字符(长度小于100),判断该串字符是否是回文串(正序读与逆序读内容相同)。
Input

输入一串字符(长度小于100)。
Output

若该串字符是回文串输出“yes”,否则输出”no“。
Example Input

asdfgfdsa
Example Output

yes

#include<bits/stdc++.h>using namespace std;typedef char Elemtype;#define INF 0x3f3f3ftypedef struct{    Elemtype *top;    Elemtype *base;    int stacksize;}sqstack;void Mal(sqstack &L);void Push(sqstack &L, Elemtype key);//int Gettop(sqstack &L);void Pop(sqstack &L);bool Empty(sqstack &L);int cnt;int main(){  char str[105];  int i, d;  while(cin >> str)  {      sqstack L;      Mal(L);      d = strlen(str);      bool flag = true;      for (i = 0;i <= d - 1;i ++)      {          Push(L, str[i]);      }      char *temp;      temp = L.base;      while(temp < L.top)      {          if (*temp != *(L.top - 1))          {              flag = false;              break;          }          temp ++;          L.top --;      }      if (flag)        cout << "yes" << endl;      else        cout << "no" << endl;  }}void Mal(sqstack &L){    L.base = (Elemtype *)malloc(INF * sizeof(Elemtype));    if (!L.base)        exit(-1);    else        L.top = L.base;        L.stacksize = INF;}void Push(sqstack &L, Elemtype key){    if(L.top - L.base >= L.stacksize)    {        L.base = (Elemtype *)realloc(L.base, (L.stacksize + 50)*sizeof(Elemtype));        if(!L.base)        {            exit(-1);        }        L.top = L.base + L.stacksize;        L.stacksize += 50;    }    *L.top ++ = key;    cnt ++;}//int Gettop(sqstack &L)//{//    cout << *(L.top - 1) << endl;//    return 0;//}void Pop(sqstack &L){    if (L.top == L.base)        exit(-1);    else        -- L.top;}bool Empty(sqstack &L){    if (L.top == L.base)        return true;    else        return false;}
原创粉丝点击