读书人

判断回文的数据结构算法求相助!为什

发布时间: 2013-01-09 09:38:16 作者: rapoo

判断回文的数据结构算法,求帮助!!!为什么无论输入什么字符,给的永远都是“不是回文”
#include<stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2
typedef int SElemType;
typedef struct SqStack
{
SElemType *base;
int *top;
int stacksize;
}SqStack;


int InitStack(SqStack *S)
{S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base) return 0;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}


int Push(SqStack *S,SElemType e)
{if(S->top-S->base>=S->stacksize)
{S->base=(SElemType*)realloc(S->base,(S->stacksize+STACK_INCREMENT)*sizeof(SElemType));
if(!S->base)
return 0;
S->top=S->base+S->stacksize;
S->stacksize+=STACK_INCREMENT;
}
*S->top++=e;
return 1;
}

int Pop(SqStack *S)
{if(S->top==S->base)
return 0;
S->top--;
return 1;
}
int StackEmpty(SqStack S)
{if (S.top==S.base)
return 1;
else return 0;
}

int strlen(SqStack S)
{return S.top-S.base;
}


int IsHuiwen( char *S)
{SqStack T;
int i,l;
char t;
InitStack(&T);
l=sizeof(S);
for(i=0;i<l/2;i++)
Push(&T,S[i]);
while(StackEmpty(T)==0)
{t=Pop (&T);
if( t!=S[l-i-1]) { return 0 ;}
i--;}
return -1 ;
}

void main( )
{char Str[100]="";
printf("输入一个字符串:\n");
scanf("%s",Str);
if (IsHuiwen(Str)==-1) printf(" \n这个字符串是回文。");
else printf("\n这个字符串不是回文。");}
[解决办法]
楼上别乱打击人.
楼主的栈数据结构是可以的.一个base指向栈底的指针,一个top指向栈顶的指针.加一个栈的最大大小,完全OK的啊.我记得严蔚敏老师就是这么写的.
只是楼主的编码风格不太好.
代码中有四个小问题:
1.Pop函数应该返回栈顶元素.
2.计算字符串的长度错误,你传进来的是个指针.
int l = sizeof(S);
这句求的是指针本身的长度,在32位系统里永远都是4.并不是求整个字符串的字节数
应该改用
int l = strlen(S);
3.你压栈的时候压的字符串的前半截.那么应该分奇数和偶数.

int nStackLen = 0;
nStackLen = l / 2;
if (l %2)//长度为奇数应该+1
{
nStackLen ++;
}
for(i = 0;i < nStackLen;i ++)
Push(&T,S[i]);

4.由于你压入栈的时候前半段,且数组的下标是从0开始的.所以后半段的其实位置
应该是l - i;
应该做如下更改
if( t!=S[l-i ]) 
{
return 0 ;
}

OK,稍微整理下楼主的代码就OK了.
下面是修改后的代码
#include<stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include<string.h>
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2
typedef int SElemType;
typedef struct SqStack
{
SElemType *base;
int *top;
int stacksize;
}SqStack;


int InitStack(SqStack *S)
{
S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base) return 0;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}


int Push(SqStack *S,SElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACK_INCREMENT)*sizeof(SElemType));
if(!S->base)
return 0;
S->top=S->base+S->stacksize;
S->stacksize+=STACK_INCREMENT;


}
*S->top++=e;
return 1;
}

int Pop(SqStack *S)
{
if(S->top==S->base)
return 0;
//由于你入栈的时候是后自增,返回栈顶元素时,应该先减1
S->top --;
int nValue = *(S->top);
return nValue;
//return 1;
}
int StackEmpty(SqStack S)
{
if (S.top==S.base)
return 1;
else return 0;
}

int strlen(SqStack S)
{
return S.top-S.base;
}


int IsHuiwen( char *S)
{
SqStack T;
int i,l;
char t;
InitStack(&T);
l = strlen(S); //计算字符串长度
int nStackLen = 0;
nStackLen = l / 2;
if (l %2)//长度为奇数应该+1
{
nStackLen ++;
}
for(i = 0;i < nStackLen;i ++)
Push(&T,S[i]);
while(StackEmpty(T) == 0)
{
t=Pop (&T);
if( t!=S[l-i ])
{
return 0 ;
}
i--;
}
return -1 ;
}

void main( )
{
char Str[100]="";
printf("输入一个字符串:\n");
scanf("%s",Str);
if (IsHuiwen(Str)==-1) printf(" \n这个字符串是回文。\n");
else printf("\n这个字符串不是回文。\n");
system("pause");
}


读书人网 >C语言

热点推荐