c语言利用顺序栈判断是否是回文串,写函数模块代码
时间: 2024-05-13 12:20:26 浏览: 75
C语言判断回文字符串代码.rar
5星 · 资源好评率100%
好的,以下是判断回文串的函数模块代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct stack {
char data[MAXSIZE];
int top;
} Stack;
void InitStack(Stack *S) {
S->top = -1;
}
int PushStack(Stack *S, char c) {
if(S->top == MAXSIZE - 1)
return 0;
S->data[++S->top] = c;
return 1;
}
int PopStack(Stack *S, char *c) {
if(S->top == -1)
return 0;
*c = S->data[S->top--];
return 1;
}
int checkPalindrome(char *str) {
Stack S;
InitStack(&S);
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
PushStack(&S, str[i]);
}
int mid = length / 2;
if (length % 2 == 1)
mid++;
char c;
for (int i = mid; i < length; i++) {
PopStack(&S, &c);
if (c != str[i])
return 0;
}
return 1;
}
int main() {
char str[MAXSIZE];
scanf("%s", str);
if(checkPalindrome(str))
printf("Yes\n");
else
printf("No\n");
return 0;
}
```
阅读全文