代码编译正常,输出不了正确结果,没找到问题在哪。。
#include
#include
#define MAXSIZE 100
typedef struct
{
char *ch ;
int length ;
}Str ;
int strassign(Str &str ,const char *ch);
void getnext(Str substr , int next[]);
int main()
{
int next[MAXSIZE] = {0};
Str substr ;
strassign(substr , "ABAABBAB");
printf("substr = %s \n",substr.ch) ;
getnext(substr , next );
for(int i = 1 ; i < 9 ;++i )
printf("%d" , next[ i ]);
return 1 ;
}
void getnext(Str substr , int next[])
{
int i = 1 ;
int j = 0 ;
next[i] = 0 ;
while( i < substr.length )
{
if( j == 0 || substr.ch[ i ] == substr.ch[ j ])
{
++i ;
++j ;
next[ i ] = j;
}
else
j = next[ j ];
}
}
int strassign(Str &str ,const char *ch)
{
int len = 0;
if(str.ch)
free(str.ch);
const char *c = ch ;
while( *c )
{
++len;
++c;
}
if(len == 0)
{
str.ch = NULL;
str.length = 0;
return 1 ;
}
else
{ c = ch ;
str.ch = (char*)malloc(sizeof(char)*(len+2)) ;
if(!str.ch)
return 0;
for(int i = 1 ; i <= len+1;++i,++c)
{
str.ch[ i ] = *c ;
}
str.ch[0] = '*' ;
str.length = len ;
return 1 ;
}
}
你要实现什麽功能啊
编译的时候,printf是不会输出的