试写一个算法判别读入的一个以‘@’ 为结束符的字符序列是否是“回文”。( 栈的实现)
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
typedef struct{
int top;
char data[MaxSize];
}Stack;
void InitStack(Stack &S)
{
S.top = -1;
}
bool IsEmptyStack(Stack S)
{
if(S.top == -1)
return true;
else
return false;
}
bool Push(Stack &S, char x)
{
if(S.top == MaxSize - 1)
return false;
S.data[++S.top] = x;
return true;
}
char Pop(Stack &S)
{
char x;
if(S.top == -1)
return false;
x = S.data[S.top--];
printf("----------出栈元素为: %c\n", x);
return x;
}
void display(Stack S)
{
while(S.top != -1)
printf("***栈内元素为: %c\n", S.data[S.top--]);
}
int main(void)
{
Stack S;
InitStack(S);
char c ;
int flag = 1;
printf("请输入进栈元素: ");
while(flag){
c = getchar();
if(c != '@'){
Push(S, c);
}else{
flag = 0;
}
}
display(S);
char temp;
int i = 0;
while(S.top != -1){
temp = Pop(S);
if(temp == S.data[i]){
i++;
}else{
printf("-----------------------------------Oh-my-God!!!-----------------------------------------不是回文\n");
return 0;
}
}
printf("-------------------------------------Congradulation!!!-----------------------------------------是回文\n");
return 0;
}