八,实现顺序串的基本运算的算法
#include <stdio.h>
#include <stdlib.h>
#define max 100
typedef struct
{
char data[max];
int length;
}sqstring;
sqstring s, s1;
void strassign(sqstring &s, char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
s.data[i] = str[i];
}
s.length = i;
}
bool equal(sqstring s, sqstring t)
{
int i;
if (s.length != t.length)
return false;
else
{
for (i = 0; i<s.length; i++)
{
s.data[i] != t.data[i];
return false;
break;
}
}
}
int length(sqstring &s)
{
return s.length;
}
sqstring connect(sqstring s, sqstring t)
{
sqstring str;
int i;
str.length = s.length + t.length;
for (i = 0; i<s.length; i++)
{
str.data[i] = s.data[i];
}
for (i = 0; i<t.length; i++)
{
str.data[s.length + i] = t.data[i];
}
return str;
}
sqstring zichuan(sqstring s, int i, int j)
{
int k;
sqstring str;
str.length = 0;
if (i <= 0 || i>s.length || j<0 || i + j - 1>s.length)
return str;
for (k = i - 1; k<i + j - 1; k++)
{
str.data[k - i + 1] = s.data[k];
}
str.length = j;
return str;
}
sqstring insert(sqstring s1, int i, sqstring s2)
{
int j;
sqstring str;
str.length = 0;
if (i <= 0 || i>s1.length + 1)
return str;
for (j = 0; j<i - 1; j++)
str.data[j] = s1.data[j];
for (j = 0; j<s2.length; j++)
str.data[i + j - 1] = s2.data[j];
for (j = i - 1; j<s1.length; j++)
str.data[s2.length + j] = s1.data[j];
str.length = s1.length + s2.length;
return str;
printf("%s", str.data);
}
sqstring deletestr(sqstring s, int i, int j)
{
int k;
sqstring str;
str.length = 0;
if (i <= 0 || i>s.length || i + j>s.length + 1)
return str;
for (k = 0; k<i - 1; k++)
{
str.data[k] = s.data[k];
}
for (k = i + j - 1; k<s.length; k++)
str.data[k - j] = s.data[k];
str.length = s.length - j;
return str;
}
sqstring repstr(sqstring s, int i, int j, sqstring t)
{
int k;
sqstring str;
str.length = 0;
if (i <= 0 || i>s.length || i + j>s.length + 1)
return str;
for (k = 0; k<i - 1; k++)
str.data[k] = s.data[k];
for (k = 0; k<t.length; k++)
str.data[i + k - 1] = t.data[k];
for (k = i + j - 1; k<s.length; k++)
str.data[t.length + k - j] = s.data[k];
str.length = s.length - j + t.length;
return str;
}
void dispstr(sqstring s)
{
int i;
if (s.length>0)
{
for (i = 0; i<s.length; i++)
{
printf("%c", s.data[i]);
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
int num,n;
sqstring s2;
char *ch1 = "abcdefghijklmn";
char *ch2 = "xyz";
strassign(s, ch1);
strassign(s1, ch2);
printf("请输入你要进行的操作:\n1.输出串s\n2.输出串s的长度\n3.在串s的第九个字符位置插入串s1\n4.删除串s的第二个字符开始的五个字符\n5.将串s的第二个字符开始的五个字符替换成串s1\n6.提出串s第二个字符开始的十个字符\n7.将两个串连起来\n\n");
while (scanf("%d", &num))
{
switch (num)
{
case 1:
dispstr(s);
break;
case 2:
printf("%d\n",length(s));
break;
case 3:
s2=insert(s, 9, s1);
dispstr(s2);
break;
case 4:
s2 = deletestr(s, 2, 5);
dispstr(s2);
break;
case 5:
s2 = repstr(s, 2, 5, s1);
dispstr(s2);
break;
case 6:
s2 = zichuan(s, 2, 10);
dispstr(s2);
break;
case 7:
s2 = connect(s, s1);
dispstr(s2);
break;
default:printf("重新输入\n");
break;
}
}
system("pause");
return 0;
}
九,实现链串的基本运算的算法
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char data;
struct node *next;
}Linkstrnode;
Linkstrnode *s,*s1,*s2;
void assign(Linkstrnode *&s, char str[])
{
int i;
Linkstrnode *r, *p;
s = (Linkstrnode *)malloc(sizeof(Linkstrnode));
r = s;
for (i = 0; str[i] != '\0'; i++)
{
p = (Linkstrnode *)malloc(sizeof(Linkstrnode));
p->data = str[i];
r->next = p;
r = p;
}
r->next=NULL;
}
void destroy(Linkstrnode *&s)
{
Linkstrnode *p;
while (s != NULL)
{
p = s;
s = s->next;
free(p);
}
}
void copy(Linkstrnode *&s, Linkstrnode *t)
{
Linkstrnode *p = t->next, *q, *r;
s = (Linkstrnode *)malloc(sizeof(Linkstrnode));
r = s;
while (p != NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
}
bool equal(Linkstrnode *s, Linkstrnode *t)
{
Linkstrnode *p = s->next, *q = t->next;
while (p != NULL&&q != NULL&&p->data == q->data)
{
p = p->next;
q = q->next;
}
if (q == NULL&&p==NULL)
return true;
else
return false;
}
int length(Linkstrnode *s)
{
int i = 0;
Linkstrnode *p = s->next;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
Linkstrnode *concat(Linkstrnode *s, Linkstrnode *t)
{
Linkstrnode *str, *p = s->next, *q, *r;
str = (Linkstrnode *)malloc(sizeof(Linkstrnode));
r = str;
while (p != NULL)
{
q= (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
p = t->next;
while (p != NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
return str;
}
Linkstrnode *zichuan(Linkstrnode *s, int i, int j)
{
int k;
Linkstrnode *str, *p = s->next, *q, *r;
str = (Linkstrnode *)malloc(sizeof(Linkstrnode));
str->next = NULL;
r = str;
if (i <= 0 || i > length(s) || j<0 || i + j - 1>length(s))
return str;
for (k = 1; k < i; k++)
{
p = p->next;
}
for (k = 1; k <= j; k++)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
return str;
}
Linkstrnode *insert(Linkstrnode *s, int i, Linkstrnode *t)
{
int k;
Linkstrnode *str, *p = s->next,*p1=t->next, *q, *r;
str = (Linkstrnode *)malloc(sizeof(Linkstrnode));
str->next = NULL;
r = str;
if (i <= 0 || i > length(s) + 1)
return str;
for (k = 1; k <= i; k++)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
while (p != NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
while (p != NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
return str;
}
Linkstrnode *deletestr(Linkstrnode *s, int i, int j)
{
int k;
Linkstrnode *str, *p = s->next, *q, *r;
str = (Linkstrnode *)malloc(sizeof(Linkstrnode));
str->next = NULL;
r = str;
if (i <= 0 || i > length(s) || j<0 || i + j - 1>length(s))
return str;
for (k = 1; k <= i; k++)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
for (k = 0; k < j; k++)
{
p = p->next;
}
while (p != NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
return str;
}
Linkstrnode *repstr(Linkstrnode *s, int i, int j, Linkstrnode *t)
{
int k;
Linkstrnode *str, *p = s->next, *p1 = t->next, *q, *r;
str = (Linkstrnode *)malloc(sizeof(Linkstrnode));
str->next = NULL;
r = str;
if (i <= 0 || i > length(s) || j<0 || i + j - 1>length(s))
return str;
for (k = 0; k < i-1; k++)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data; q->next = NULL;
r->next = q;
r = q;
p = p->next;
}
for (k = 0; k < j; k++)
{
p = p->next;
}
while (p1!= NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p1->data;
q->next = NULL;
r->next = q;
r = q;
p1 = p1->next;
}
while (p != NULL)
{
q = (Linkstrnode *)malloc(sizeof(Linkstrnode));
q->data = p->data; q->next = NULL;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
return str;
}
void dispstr(Linkstrnode *s)
{
Linkstrnode *p = s->next;
while (p != NULL)
{
printf("%c", p->data);
p=p->next;
}
printf("\n");
}
int main()
{
int num,n;
assign(s, "abcdefghijklmn");
assign(s1, "xyz");
printf("请输入你要进行的操作:\n1.输出串s\n2.输出串s的长度\n3.在串s的第九个字符位置插入串s1\n4.删除串s的第二个字符开始的五个字符\n5.将串s的第二个字符开始的五个字符替换成串s1\n6.提出串s第二个字符开始的十个字符\n7.将两个串连起来\n\n");
while (scanf("%d", &num))
{
switch (num)
{
case 1:
dispstr(s);
break;
case 2:
printf("%d\n",length(s));
break;
case 3:
s2=insert(s, 9, s1);
dispstr(s2);
break;
case 4:
s2 = deletestr(s, 2, 5);
dispstr(s2);
break;
case 5:
s2 = repstr(s, 2, 5, s1);
dispstr(s2);
break;
case 6:
s2 = zichuan(s, 2, 10);
dispstr(s2);
break;
case 7:
s2 = concat(s, s1);
dispstr(s2);
break;
default:printf("重新输入\n");
break;
}
}
system("pause");
return 0;
}
十,实现顺序串的各种模式匹配算法
#include<stdio.h>
#include<stdlib.h>
#define max 100
typedef struct
{
char data[max];
int length;
}sqstring;
void strassign(sqstring &s,char cstr[])
{
int i;
for(i=0;cstr[i]!='\0';i++)
{
s.data[i]=cstr[i];
}
s.length=i;
}
void dispstr(sqstring s)
{
int i;
if(s.length>0)
{
for(i=0;i<s.length;i++)
printf("%c",s.data[i]);
printf("\n");
}
}
int BF(sqstring s,sqstring t)
{
int i=0,j=0;
while(i<s.length&&j<t.length)
{
if(s.data[i]==t.data[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(j>=t.length)
return(i-t.length);
else
return -1;
}
void getnext(sqstring t,int next[])
{
int j,k;
j=0;k=-1;
next[0]=-1;
while(j<t.length-1)
{
if(k==-1||t.data[j]==t.data[k])
{
j++;k++;
next[j]=k;
}
else
k=next[k];
}
}
void getnextval(sqstring t)
{
int nextval[100]={0};
int j=0,k=-1;
nextval[0]=-1;
while(j<t.length)
{
if(k==-1||t.data[j]==t.data[k])
{
j++;k++;
if(t.data[j]!=t.data[k])
nextval[j]=k;
else
nextval[j]=nextval[k];
}
else
k=nextval[k];
}
for(j=0;j<t.length;j++)
{
printf("%5d",nextval[j]);
}
}
int KMP(sqstring s,sqstring t)
{
int next[100];
int i=0,j=0;
getnext(t,next);
while(i<s.length && j<t.length)
{
if(j==-1||s.data[i]==t.data[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j>=t.length)
return (i-t.length);
else
return -1;
}
int main()
{
int a,b,j;
sqstring S1,S2;
char *s,*t;
int next[100]={0};
s="abcabcdabcdeabcdefabcdefg";
t="abcdeabcdefab";
strassign(S1,s);
strassign(S2,t);
dispstr(S1);
dispstr(S2);
a=BF(S1,S2);
printf("t在s的中的位置为% d\n",a);
printf("t的next值为:\n");
getnext(S2,next);
for(j=0;j<S2.length;j++)
{
printf("%5d",next[j]);
}
printf("\n");
printf("t的nextval值为:\n");
getnextval(S2);
printf("\n");
b=KMP(S1,S2);
printf("t在s的中的位置为% d\n",b);
system("pause");
return 0;
}