块串的匹配

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{char a;
struct node *next;
 }node;//块串块
 typedef struct node1
 {node *head,*last;
  } node1;//首位节点指针 
node1 fuzhi(char c[])
{node *p,*q;
node1 s;
int i=0,j;
j=strlen(c);
p=(node*)malloc(sizeof(node));
q=p;
s.head=p;
while(i<j)
{if(q!=p)
{
q->next=p;
q=p;}
p->a=c[i];
p=(node*)malloc(sizeof(node));
i++;
}
q->next=0;
free(p);
s.last=q;
return s;}//fuzhi 
node *pipei(node1 s,node1 p)
{node *p1,*p2,*p3;
p1=s.head;
p2=p.head;
p3=p1;

while(p1!=0&&p2!=0)
{if(p1->a==p2->a)
{p1=p1->next;
p2=p2->next;
}
else
{p3=p3->next;
p1=p3;
p2=p.head;
}
}
if(p2==0)
return p3;
else
{printf("匹配失败");
exit(1); 
}
}
int main()
{char c[40];
node1 s,s1;
node *p;
printf("主串是:");
scanf("%s",c);
s=fuzhi(c);
printf("子串是:");
scanf("%s",c);
s1=fuzhi(c);
p=pipei(s,s1);
while(p!=0)
{
printf("%c",p->a);
p=p->next;}
 } 
在这里插入代码片

猜你喜欢

转载自blog.csdn.net/feiqipengcheng/article/details/83214889