数据结构 关于模式匹配

帅文浩 2012-11-08 10:40:22
请问各路大神,t怎么总是0,问题出在哪:

#include <stdio.h>
#include <malloc.h>
#define CHUNKSIZE 1
typedef struct Chunk
{
char ch[CHUNKSIZE];
struct Chunk *next;
}Chunk,*LinkList;
void CreateChunk_L(LinkList &L,int n)
{
LinkList p,q;
L=(LinkList)malloc(sizeof(Chunk));
q=L;
L->next=NULL;
for(int i=n;i>0;--i)
{
p=(LinkList)malloc(sizeof(Chunk));
scanf("%c",&p->ch);
q->next=p;
p->next=NULL;
q=p;
}
}
int Index(LinkList &L,LinkList &S,int pos)
{
LinkList p=L->next,q=S->next;
while(p&&q)
{
int i,j=0;
for(i=pos;i>0;--i)
p=p->next;
while(p&&q)
{
if(!(p->ch==q->ch))
{
if(p->next)
{
p=p->next;
++j;
}
else
{
return 0;
break;
}

}
else
{
if(q->next)
q=q->next;
else
{
return j;
break;
}
}
}

}
}
void main()
{
int t;
LinkList S,T;
CreateChunk_L(S,8);
CreateChunk_L(T,3);
t=Index(S,T,1);
printf("%d",t);
}
...全文
106 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2012-11-09
  • 打赏
  • 举报
回复
根据以下定义: char ch[CHUNKSIZE]; 以下语句: if(!(p->ch==q->ch)) 判断的是2个ch的地址,它们不会相等。如果要判断ch的内容,可改用strncmp(p->ch, q->ch, CHUNKSIZE)。

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧