用堆栈判断是否为回文,为什么我这里结果不对,请大佬帮忙看看
本帖最后由 zyf117 于 2022-4-25 23:39 编辑因为不会调试,所以打印了一些中间变量,查看问题,请大佬们帮忙看看是哪里出了问题,以下是代码和编译结果
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT10
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack *s, ElemType e)
{
if( s->top - s->base >= s->stackSize )
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, ElemType *e)
{
if( s->top == s->base )
{
return;
}
*e = *--(s->top);
}
int StackLen(sqStack s)
{
return (s.top - s.base);
}
void main()
{
char a;
char b;
int i,j = 0;
int len;
sqStack Pal1,Pal2;
ElemType c;
InitStack(&Pal1);
InitStack(&Pal2);
printf("请输入一组以@结尾的回文:");
scanf("%c", &c);
while( c != '@' )
{
Push(&Pal1, c);
scanf("%c", &c);
}
getchar();
len = StackLen(Pal1);
printf("栈的当前容量是: %d\n", len);
printf("第一个栈为:");
for( i=0; i < len; i++ )
{
Pop(&Pal1, &c);
printf("%c",c);
}
for( i=0; i < len-1; i++ )
{
Pop(&Pal1, &c);
Push(&Pal2, c);
}
printf("\n");
printf("第二个栈为:");
for( i=0; i < len; i++ )
{
Pop(&Pal2, &c);
printf("%c",c);
}
printf("\n");
for( i=0; i < len-1; i++ )
{
Pop(&Pal1, &c);
a = c;
}
printf("第一个数组为");
for( i=0; i < len; i++ )
{
printf("%c ",a);
}
printf("\n");
for( i=0; i < len; i++ )
{
Pop(&Pal2, &c);
b = c;
}
printf("第二个数组为:");
for( i=0; i < len; i++ )
{
printf("%c ",b);
}
printf("\n");
printf("比较数组:");
for( i=0; i < len-1; i++ )
{
if(a == b)
{
j++;
printf("%c-%c %d%d\n ",a,b,j,i);
}
}
if(j == len)
{
printf("是回文");
}
else
{
printf("不是回文");
}
} #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT10
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
void InitStack(sqStack *s)
{
s->base = malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack *s, ElemType e)
{
if( s->top - s->base >= s->stackSize )
{
s->base = (ElemType *)realloc(s->base, (s->stackSize += STACKINCREMENT) * sizeof(ElemType));
//s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack *s, ElemType *e)
{
if( s->top == s->base )
{
return;
}
*e = *--(s->top);
}
int StackLen(sqStack s)
{
return (s.top - s.base);
}
void stack_deinit(sqStack *s) {
if(s) free(s->base);
}
//void main()
int main(void)
{
sqStack s;
InitStack(&s);
while(1) {
char ch = getchar();
if(ch == '@') break;
Push(&s, ch);
}
char buff;
size_t size = 0;
while(StackLen(s) != 0) {
char e; Pop(&s, &e);
buff = e;
}
for(size_t i = 0; i < size; ++i) {
Push(&s, buff);
}
bool flag = true;
for(size_t i = 0; i < size; ++i) {
char e; Pop(&s, &e);
if(e != buff) {
flag = false;
break;
}
}
printf("%s\n", flag ? "==" : "!=");
stack_deinit(&s);
return 0;
#if 0
char a;
char b;
int i,j = 0;
int len;
sqStack Pal1,Pal2;
ElemType c;
InitStack(&Pal1);
InitStack(&Pal2);
printf("请输入一组以@结尾的回文:");
scanf("%c", &c);
while( c != '@' )
{
Push(&Pal1, c);
scanf("%c", &c);
}
getchar();
len = StackLen(Pal1);
printf("栈的当前容量是: %d\n", len);
printf("第一个栈为:");
for( i=0; i < len; i++ )
{
Pop(&Pal1, &c);
printf("%c",c);
}
for( i=0; i < len-1; i++ )
{
Pop(&Pal1, &c);
Push(&Pal2, c);
}
printf("\n");
printf("第二个栈为:");
for( i=0; i < len; i++ )
{
Pop(&Pal2, &c);
printf("%c",c);
}
printf("\n");
for( i=0; i < len-1; i++ )
{
Pop(&Pal1, &c);
a = c;
}
printf("第一个数组为");
for( i=0; i < len; i++ )
{
printf("%c ",a);
}
printf("\n");
for( i=0; i < len; i++ )
{
Pop(&Pal2, &c);
b = c;
}
printf("第二个数组为:");
for( i=0; i < len; i++ )
{
printf("%c ",b);
}
printf("\n");
printf("比较数组:");
for( i=0; i < len-1; i++ )
{
if(a == b)
{
j++;
printf("%c-%c %d%d\n ",a,b,j,i);
}
}
if(j == len)
{
printf("是回文");
}
else
{
printf("不是回文");
}
return 0;
#endif
}
页:
[1]