c数据机构顺序栈回文判断代码实现
时间: 2023-12-12 08:53:40 浏览: 89
回文数判断的栈实现
好的,下面是C语言顺序栈实现的回文判断代码:
```c
#include <stdio.h>
#include <string.h>
#define MAXSIZE 100
typedef struct {
char data[MAXSIZE];
int top;
} SqStack;
void InitStack(SqStack *S)
{
S->top = -1;
}
int IsEmpty(SqStack S)
{
if (S.top == -1)
return 1;
else
return 0;
}
int IsFull(SqStack S)
{
if (S.top == MAXSIZE - 1)
return 1;
else
return 0;
}
int Push(SqStack *S, char e)
{
if (IsFull(*S))
return 0;
S->top++;
S->data[S->top] = e;
return 1;
}
int Pop(SqStack *S, char *e)
{
if (IsEmpty(*S))
return 0;
*e = S->data[S->top];
S->top--;
return 1;
}
int Palindrome(char str[])
{
SqStack S;
int i, len;
char e;
len = strlen(str);
InitStack(&S);
for (i = 0; i < len / 2; i++)
Push(&S, str[i]);
if (len % 2 != 0)
i++;
while (i < len) {
Pop(&S, &e);
if (str[i] != e)
return 0;
i++;
}
return 1;
}
int main()
{
char str[MAXSIZE];
printf("请输入字符串:");
gets(str);
if (Palindrome(str))
printf("是
阅读全文