#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
struct strch{
char ch;
struct strch *next;
};
struct strch * creat()
{
struct strch *head,*p1,*p2;
int strl=0;
int n=0;
head=NULL;
p1=p2=(struct strch*)malloc(sizeof(struct strch));
printf("please enter the strch:");
scanf("%c",&p1->ch);
while((*p1).ch!='#')
{
n=n+1;
if(head==NULL)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct strch*)malloc(sizeof(struct strch));
scanf("%c",&p1->ch);
}
p2->next=NULL;
return head;
}
void print(struct strch * head)
{
struct strch *p1;
p1=head;
if(p1==NULL)
printf("this is a null");
else
while(p1->next!=NULL)
{
printf("%c",(*p1).ch);
p1=p1->next;
}
}
void fun(struct strch *&head,struct strch *t1,struct strch *t2)//用t2替换链表(以head为头结点)中的t1
//[bo]
[size=5这个函数好像没起作用???[/size][/bo]
{
struct strch *p1,*p;
struct strch *headt1,*headt2;
headt1=t1;headt2=t2;
p1=head;
int i=0;
if(head==NULL) {printf("none exsit");exit(0);}
else while(p1->next!=NULL)
{
if(p1->ch==t1->ch)
{
p=p1;
for(;p1->ch==t1->ch;p1=p1->next,t1=t1->next);//循环判断head中是否有t1
if(t1->next==NULL)//如果经上循环t1已经指向未,则说明head中有t1,并已用p标记了t1在head 中出现的起始
{
p->next=headt2;
while(t2->next!=NULL)t2=t2->next;
t2->next=p1;
t2=headt2;//当链表中有多个t1时,要保证让t1,t2指向表头
t1=headt1;
}
}
else p1=p1->next;
}
}
void main()
{
struct strch *head,*t1,*t2;
printf("产生长串");
head=creat();
printf("产生长串中的一个子串");
t1=creat();
printf("产生替代子串的另一个串");
t2=creat();
fun(head,t1,t2);
print(head);
}