数据结构 ------链式串的基本操作:
这里采用串的链式存储结构,在明白了单链表的基本原理基础上,再理解串的链式存储以及相关操作就通俗易懂了。
这里要注意的是,p->next:指向第一个字符,刚开始创建的p为头结点,然后需要注意的地方就是这里的 r->next =q; r =q; 这里的意思是将q指针放在r->next的后面,接着r指针移动q指针的位置. 还有一种就是(q->next =r->next,r->next =q;)主要注意 q->next = r->next是指q指针指向的位置与r指针指向的位置一样.
代码如下:
#include <stdio.h>
#include <malloc.h>
typedef struct node{
char data;
struct node *next;
}linknode;
//初始化
void initstr(linknode *&b, char str[])
{
linknode *p, *r;
//头结点
b = (linknode *)malloc(sizeof(linknode));
r = b;
for(int i=0; i< str[i]!= '\0'; i++)
{
p = (linknode *)malloc(sizeof(linknode));
p->data = str[i];
p->next = r->next;
r->next =p;
r=p;
}
r->next =NULL; //最后记得在最后面指向空
}
//销毁串
void destroystr(linknode *&b)
{
//注意这里是由前往后删除
linknode *pre = b, *p =b->next;
while(p!=NULL)
{
free(pre); //先删除前驱结点
pre = p;
p = p->next;
}
free(pre);
}
//尾插法
void strcpy(linknode *&b, linknode *s)
{
linknode *p = s->next, *r, *q;
b = (linknode *)malloc(sizeof(linknode));
r = b;
while(p!=NULL)
{
q= (linknode *)malloc(sizeof(linknode));
q->data = p->data;
p->next = r->next;
r->next = q;
r = q;
p = p->next;
}
r->next =NULL;
}
//判断两字符串是否相等
bool strequal(linknode *b, linknode *s)
{
linknode *p = b->next;
linknode *r = s->next;
while(p!=NULL && r!=NULL && p->data == r->data)
{
p = p ->next;
r = r ->next;
}
if(p == NULL && r == NULL) return true;
else return false;
}
//求字符串的长度
int strlength(linknode *s)
{
int i =0;
linknode *p = s->next;
while(p!=NULL)
{
i++;
p = p->next;
}
return i;
}
//这里是将两个字符串连接起来
linknode * strcon(linknode *s, linknode *t)
{
linknode *str, *p =s->next, *r,*q;
str = (linknode *)malloc(sizeof(linknode));
//需要两个指针。
r = str;
while(p!=NULL)
{
q = (linknode *)malloc(sizeof(linknode));
q->data = s->data;
q->next = r->next;
r->next = q;
r = q;
p = p->next;
}
p = t->next;
while(p!=NULL)
{
q = (linknode *)malloc(sizeof(linknode));
q ->data = t->data;
q->next = r->next;
r ->next = q;
r =q;
p = p->next;
}
r = r->NULL;
return str;
}
//求子串
linknode * sonstr(linknode *s, int i, int j) //第i个位置开始的j个字符
{
linknode *strs=(linknode *)malloc(sizeof(linknode));
strs->next =NULL;
linknode *r,*p = s->next,*q;
r =strs;
for(int k = 0; k<i-1; k++)
p = p->next;
for(int k=0; k<j; k++)
{
q = (linknode *)malloc(sizeof(linknode));
q ->data= p->data;
q->next = r->next;
r->next = q;
r =q;
p = p->next;
}
r->next =NULL;
return strs;
}
//字符串内部插入
linknode * insertstr(linknode *a,int j ,linknode *b) //a中第j个位置开始插入
{
linknode *str,*q,*r;
linknode *p =a->next, *t =b->next;
str = (linknode *)malloc(sizeof(linknode));
str->next = NULL;
r =str;
if(j<0 && j > strlength(a)+1)//不加1就是插尾部
return NULL;
for(int i=1; i<j; i++)
{
q = (linknode *)malloc(sizeof(linknode));
q ->data = p->data;
q->next = r->next;
r->next = q;
r = q;
p = p->next;
}
while(t!=NULL)
{
q =(linknode *)malloc(sizeof(linknode));
q->data = t->data;
q->next = r->next;
r->next =q;
r= q;
t = t->next;
}
while(p!=NULL)
{
q = (linknode *)malloc(sizeof(linknode));
q ->data = p->data;
q->next = r->next;
r->next = q;
r= q;
p = p->next;
}
r->next =NULL;
return str;
}
//删除
linknode * delestr(linknode *s,int i ,int j) //从第i个位置开始的j个字符
{
linknode *str;
linknode *r, *p= s->next,*q;
str = (linknode *)malloc(sizeof(linknode));
str->next =NULL;
r =str;
for(int k = 0; k<i-1; k++)
{
q = (linknode *)malloc(sizeof(linknode));
q ->data = p->data;
q->next = r->next;
r->next = q;
r =q;
p = p->next;
}
for(int k =0; k< j; k++)
p = p->next;
while(p!=NULL)
{
q = (linknode *)malloc(sizeof(linknode));
q->data = p->data;
q->next = r->next;
r ->next =q;
r = q;
p = p->next;
}
r ->next =NULL;
return str;
}
//替换
linknode * restr(linknode *s, int i, int j, linknode *t)//s串中第i个位置开始的连续j个字符用t字符串替换。
{
linknode *str =(linknode *)malloc(sizeof(linknode));
str->next = NULL;
linknode *p =s->next;
linknode *w =t->next;
linknode *r,*q;
r =str;
for(int k =0; k<i; k++)
{
q = (linknode *)malloc(sizeof(linknode));
q ->data =p->data;
q->next = r->next;
r->next = q;
r =q;
p = p->next;
}
for(int k=0; k<j-1; k++)
{
p = p->next;
}
while(w!=NULL)
{
q =(linknode *)malloc(sizeof(linknode));
q -> data = w ->data;
q->next = r->next;
r->next = q;
r = q;
w = w->next;
}
while(p!=NULL)
{
q =(linknode *)malloc(sizeof(linknode));
q->data = p->data;
q->next = r->next;
r->next =q;
r =q ;
p= p->next;
}
r->next =NULL;
return str;
}
//输出
void show(linknode *s)
{
linknode *p = s->next;
while(p!=NULL)
{
printf("%c",p->data);
p = p->next;
}
printf("\n");
}
int main()
{
linknode *s,*b;
char str1[] = "allllllal111111";
char str2[] = "abcdefg";
initstr(s,str1);
show(s);
initstr(b,str2);
show(b);
int s1 = strlength(s);
int s2 = strlength(b);
linknode *str3 = insertstr(s,7,b);
printf("\n将str2插入str1的第7位置开始:");
show(str3);
linknode *str4 = delestr(str3,7,s2);
printf("\n在str3字符串中从第7个位置连续删除 %d 个字符: ",s2);
show(str4);
linknode *str5 = restr(str4,5,s2,b);
printf("\n在str4字符串从第5个位置开始用str2字符串连续替换 %d: ",s2);
show(str5);
linknode *str6 = sonstr(str5,3,5);
printf("\n在str5字符串中获取从第3个位置开始的连续5个字符: ");
show(str6);
return 0;
}
程序运行如下: