回文指的是一个字符串从前面读和从后面读都一
样,编写一个算法判断一个字符串是否为回文。
要求:
1)采用链栈实现算法;
2)从键盘输入一个字符串,输出判断结果。
#include"stdio.h"
#include"stdlib.h"
typedef char ElemType;
typedef struct stnode
{
ElemType data;
struct stnode *next;
}StNode, *LinkStack;
int huiwen(char str[])
{
int i = 0;
char ch;
StNode *sl = NULL, *p;
while ((ch = str[i++]) != '\0')
{
p = (StNode *)malloc(sizeof(StNode));
p->data = ch;
p->next = sl;
sl = p;
}
i = 0;
while (sl != NULL)
{
p = sl;
ch = p->data;
sl = sl->next;
free(p);
if (ch != str[i++])
return 0;
}
return 1;
}
void main()
{
char string[20];
int hw;
printf("input a string:");
gets_s(string);
hw = huiwen(string);
if (hw) printf("The string is HUIWEN.");
else printf("The string is not HUIWEN.");
}
欢迎访问我的博客https://www.ndmiao.cn/