赞
踩
题目:
回文判断
试写一个算法,判断依次读入的一个以@为结束符的字母序列,是否为形如‘序列1&序列2’模式的字符序列。其中序列1和序列2中都不含字符‘&’,且序列2是序列1的逆序列。例如,‘a+b&b+a’是属该模式的字符序列,而‘1+3&3-1’则不是。
主要思路:
“回文”一字符串正着读和反着读是相同的字符序列,如“abcba”,“abba"为"回文”,“abab”则不是“回文”。
通过栈先进后出的特点。即可判断是否为回文。
这里的题目是耿国华的数据结构教材题目。
【只提供子函数,需放入链栈的基本操作里运行】
void huiwen()//回文 { linkstack top; type temp, ch; top = (linkstack)malloc(sizeof(stack)); if (top == NULL) return; InitStack(top); printf("\n请输入字符:"); while ((ch = getchar()) != '&') { if (push(top, ch)) printf("%c入栈成功\n", ch); else printf("%c入栈失败\n", ch); } do { ch = getchar(); pop(top, &temp); // printf("ch=%c,temp=%c\n", ch,temp); if (ch != temp) { printf("no"); break; } } while (ch != '@' && !IsEmpty(top)); if ((ch = getchar()) == '@' && IsEmpty(top)) printf("yes"); else printf("no"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。