为什么第二次输出结果不对呢??
#include "stdio.h"#include "stdlib.h"
#define LEN sizeof(linkstring)
typedef struct strnode
{
char data;
struct strnode *next;
}linkstring;
linkstring *l_create()//建立带头结点的单链表
{
linkstring *head,*p,*rear;
char ch;
head = (linkstring *)malloc(LEN);
head->data = '#';
rear = head;
printf("输入字符以'@'结束:");
ch = getchar();
while(ch!='@')
{
p = (linkstring *)malloc(LEN);
p->data = ch;
rear->next = p;
rear = p;
ch = getchar();
}
rear->next = NULL;
return (head);
}
linkstring *l_bfindex(linkstring *s,linkstring *t)//链表BF匹配算法
{
linkstring *ps,*pt,*first;
ps = s->next;
first = s->next;
pt = t->next;
while(ps)
{
while(pt)
{
if(ps->data==pt->data)
{
ps = ps->next;
pt = pt->next;
}
else
{
first = first->next;
ps = first;
pt = t->next;
}
}
if(! pt)
{
return (first);
}
else
{
return (NULL);
}
}
}
void print(linkstring *head)
{
linkstring *p = head->next;
while(p)
{
printf("%c",p->data);
p = p->next;
}
}
int main(int argc,char *argv[])
{
linkstring *hs,*ht,*result;
printf("输入链表形式的原串:");
hs = l_create();
print(hs);
putchar('\n');
printf("输入链表形式的模式串:");
ht = l_create();
print(ht);//为什么此处输出结果与预想的不一致呢???
/*
result = l_bfindex(hs,ht);
if(result)
{
printf("子串在原串的位置:%c",result->data);
}
else
{
printf("子串在原串中没有匹配点");
}
*/
putchar('\n');
return 0;
}
第一次创建链表并输出没有问题,但是在第二次创建链表并输出时却会多输出许多空格...这是为什么呢,各种办法都想过了,就是解决不了??请教...
[ 本帖最后由 hnliji1107 于 2010-6-16 17:36 编辑 ]