题目一:回文判断(*)
[问题描述]
对于一个从键盘输入的字符串,判断其是否为回文。回文即正反序相同。如“abba”是回文,而“abab”不是回文。
[基本要求]
(1)数据从键盘读入;
(2)输出要判断的字符串;
(3)利用栈和队列对给定的字符串判断其是否是回文,若是则输出“Yes”,否则输出“No”。
[测试数据]
代码部分:
#include<bits/stdc++.h>
using namespace std;
struct Node{
char a;
Node* next;
};
Node* CreateStack(){
Node* top=(Node*)malloc(sizeof(Node));
top->next=NULL;
return top;
}
void push(Node* head,char k){
Node* p=(Node*)malloc(sizeof(Node));
p->a=k;
p->next=head->next;
head->next=p;
}
char pop(Node* head){
char temp=head->next->a;
Node* p=head->next;
head->next=head->next->next;
free(p);
return temp;
}
void judge(Node* head){
char str[3000];
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;i++)
push(head,str[i]);
for(int i=0;i<len;i++){
char temp=pop(head);
if(temp!=str[i]){
puts("NO");
return;
}
}
puts("YES");
}
int main()
{
Node* Stack=CreateStack();
while(1) judge(Stack);
return 0;
}
思路
手写一个栈把字符全部push入栈,之后top出栈与原表进行对比,若全都一样就是回文串,输出YES否则输出NO.
该博客探讨如何利用栈来判断一个输入的字符串是否为回文。通过将字符串的字符依次压入栈中,然后逐个与原字符串比较,如果所有字符都相同,则输出"Yes",表示是回文,否则输出"No"。
825

被折叠的 条评论
为什么被折叠?



