//头文件.struct.h
#include
#include
#include
#define LEN sizeof(char)
#define Len1 5
#define Len2 5
typedef struct
{
char *ch;
int length;
}Hstring;
//1.初始化函数
#include "struct.h"
int Init_string(Hstring *p, int length, char *s)
{
int i;
/* 为p->ch分配内存空间*/
/* 在这里一不小心容易写成p = (char *) malloc (length* LEN);//写成这样会报段错误
*要注意的是c并不是结构体Hstring中的变量,Hstring中的变量只有ch和length,所以再分配时一定要小心。*/
p->ch = (char *) malloc (LEN * length);
p->length = length;
/*将自己输入的值压入链表*/
for(i=0;i
p->ch[i] = s[i];
}
return p->length;
}
//2.连接函数
#include "struct.h"
/*函数功能:
*连接p和s并将其放到c中
*/
int Contact(Hstring *c, Hstring *p, Hstring *s)
{
int i;
/*为c->ch分配内存*/
c->ch = (char *) malloc ((p->length + s->length) * LEN);
/*c->length并不需要分配内存空间,在Init_string函数中有p->length = length的赋值语句。若个人有洁癖,也可以分配->_->*/
c->length = p->length + s->length;
//将p中的值压入c的前半部分
for(i = 0;i < p->length;i++){
c->ch[i] = p->ch[i];
}
//将s中的值压入c的后半部分
for(i = 0;i < s->length;i++){
c->ch[i + p->length] = s->ch[i];
}
return c->length;
}
//3.插入函数insert
#include "struct.h"
/*函数功能:
在串S中选取一点pos,并在此点将串T插入*/
int Insert(Hstring *S, int pos, Hstring T)
{
int i;
if(pos < 0 || pos > S->length){
printf("pos error\n");
return 0;
}
else{
/*因为S->length + T.length > S->length * 所以为S重新分配内存 * 函数名: realloc 功 能: 重新分配主存 用 法: void *realloc(void *ptr, unsigned newsize); */
S->ch = (char *) realloc (S->ch,(S->length + T.length) * LEN);
S->length = S->length + T.length;
}
/*将pos点之后的元素逐个放到S->ch的末尾*/
for(i = pos;i <= S->length;i++){
S->ch[i + T.length] = S->ch[i];
}
for(i = 0;i < T.length;i++)
S->ch[pos + i] = T.ch[i];
return S->length;
}
//4.求子串函数
#include "struct.h"
//函数功能,截取p中pos点之后的元素到sub中 int Sub_string(Hstring *sub, Hstring *p, int pos) {
int i;
sub->length = p->length - pos;
printf("p->length = %d\n",p->length);
if(pos > p->length || pos < 1){
printf("error\n");
return -1;
}
else{
//为sub->ch分配内存空间 sub->ch = (char *) malloc (LEN * sub->length);
for(i = 0;i < sub->length;i++){
sub->ch[i] = p->ch[pos + i];
}
}
printf("sub->length = %d\n",sub->length);
return sub->length;
}
//5.求next值函数
#include "struct.h"
void Get_next(Hstring *p, int next[])
{
int Len_p = strlen(p);
int i = -1;
int j = 0;
int k;
char s[40];
for(k = 0;k < p->length;k++){
s[i] = p->ch[i];
}
while(j < Len_p - 1){
if(i == -1 || s[j] == s[i]){
i++;
j++;
if(s[j] != s[i])
next[j] = i;
else
next[j] = next[i];
}
else
i = next[i];
}
}
6.KMP算法求子串函数
#include "struct.h"
int kmp(Hstring s,Hstring p, int next[])
{
int i,j,k;
char buffer_s[40], buffer_p[40];
int Len_s,Len_p;
i = j = 0;
Len_s = s.length;
Len_p = p.length;
/*将链表中的元素压入数组中,以方便KMP算法的执行*/
printf("\nbuffer_s = ");
for(k = 0; k < Len_s;k++){
buffer_s[k] = s.ch[k];
printf("[%c]",buffer_s[k]);
}
printf("\n");
printf("\nbuffer_p = ");
for(k = 0; k < Len_p;k++){
buffer_p[k] = p.ch[k];
printf("[%c]",buffer_p[k]);
}
printf("\n");
/*while是此算法的核心,若不理解可以参照我日后的将写的KMP算法分析(现在还没写->_
while(i < Len_s && j < Len_p){
if(j == -1 || buffer_s[i] == buffer_p[j]){
i++;
j++;
}
else
j = next[j];
}
if(j == Len_p)
return i - j;
else
return -1;
}
//7.销毁函数
#include "struct.h"
void Clear_string(Hstring S)
{
free(S.ch);//销毁链表
S.length = 0;//将链表长度置0
}
//8.主函数
#include "struct.h"
int main()
{
Hstring string1, string2;
Hstring string3, string4;
int i, pos_insert, pos_sub;
int judge, position;
int next[30];
/*如果这里是 char *s, * 便以可以通过 * 但是会报 Segmentation fault *cause unknown */
char s[Len1];
char k[Len2];
next[0] = -1;
printf("please input string1:\n");
for(i=0;i
/*若这里使用scanf("%c",&s[i]) * 我们只能输入i/2个值
*cause unknown
*/
scanf("%s",&s[i]);
}
printf("please input string2:\n");
for(i=0;i
scanf("%s",&k[i]);
}
Init_string(&string1, Len1, s);
Init_string(&string2, Len2, k);
printf("请选择插入位置:\n");
scanf("%d",&pos_insert);
Insert(&string1,pos_insert,string2);
printf("插入操作完成后,string1 = :\n");
for(i=0;i
printf("[%c]",string1.ch[i]);
}
printf("\n");
Contact(&string3, &string1, &string2);
printf("连接操作完成后,string1 = :\n");
for(i=0;i < string3.length;i++){
printf("[%c]",string3.ch[i]);
}
printf("\n");
printf("请输入截取位置:\n");
scanf("%d",&pos_sub);
Sub_string(&string4, &string1, pos_sub);
printf("截取后,子串string4 = :\n");
for(i=0;i < string4.length;i++){
printf("[%c]",string4.ch[i]);
}
printf("\n");
Len_string(&string1);
Get_next(&string4, next);
position = kmp(string1, string4, next);
printf("子串在主串中的位置是:");
printf("%d\n",position);
printf("would you like destory the string? yes(1),no(other)\n");
scanf("%d",&judge);
if(judge == 1){
Clear_string(string1);
Clear_string(string2);
Clear_string(string3);
Clear_string(string4);
printf("clear successful\n");
}
return 0;
}