数据结构——用栈来判断回文字符串

#include<iostream>
#include<string>
typedef int Status;
using namespace std;
typedef struct StackNode//定义栈
{
    char data;
    struct StackNode *next;
  }StackNode,*LinkStack;
Status InitStack(LinkStack &S)//创建链栈
{
    S = NULL;
    return 1;
}
Status Push(LinkStack &S, char e)//入栈
{
    StackNode *p;
    p=new StackNode;
    p->data = e;
    p->next = S;
    S = p;
    return 1;
}
Status Pop(LinkStack &S, char &e)//出栈
{
    StackNode *p;
    p = new StackNode;
    if (S == NULL)
        return 0;
    e = S->data;
    p = S; S = S->next;
    delete p;
    return 1;
}
int main()
{
    string a;
    LinkStack S;//建立链栈
    cin >> a;//输入字符串
    for (char i : a) //遍历字符串
    {
        if (i != ' ')//如果字符不为空格,入栈
        {
            Push(S, i);
        }
    }
    while (1)
    {
        char j;
        for (char i : a)
    {
        Pop(S, j);//出栈
        if (i != ' ')//如果字符不为空格,进行比较
        {
            if (i != j)
            {
                cout << "不是回文字符串" << endl;
                return 0;
                }
            }
        }
        cout << "是回文字符串" << endl;
        return 0;
    }
}                                        

 

posted @ 2017-10-18 15:16  夏延  阅读(11132)  评论(0编辑  收藏  举报