【详解】串(顺序串,链串)的基本运算(插入,链接,替换等全有)有给源码----看这一篇就够了

介绍:

什么是串,字符串简称串,剩下大家都知道,我就不多说,直接上重点,基本运算。

void StrAssign(SqString* s, char* cstr) 字符串常量赋给串s

void Display(SqString* s) 输出串s

void Destroy(SqString* s) 摧毁串

void StrCopy(SqString* s, SqString* t) 串复制

bool StrEqual(SqString* s, SqString* t) 判串相等

int StrLength(SqString* s) 求串长

SqString* Concat(SqString* s, SqString* t) 串连接

SqString* SubStr(SqString* s, int i, int j) 求子串

SqString* InsStr(SqString*str1, int i, SqString*str2) 插入串

SqString* DelStr(SqString* str, int i, int j) 串删去

SqString* RepStr(SqString* s, int i, int j, SqString* t) 子串替换

顺序串的基本运算

结构体

SqString

cpp 复制代码
typedef struct str
{
	char data[1000];
	int length;
}SqString;

void StrAssign(SqString* s, char* cstr)

字符串常量赋给串s

cpp 复制代码
void StrAssign(SqString* s, char* cstr)
{
	strcpy(s->data, cstr);
	return;
}

void Display(SqString* s)

输出串s

cpp 复制代码
void Display(SqString* s)
{
	printf("%s", s->data);//*s.data
	return;
}

void Destroy(SqString* s)

摧毁串

cpp 复制代码
void Destroy(SqString* s)
{
	free(s);
}

void StrCopy(SqString* s, SqString* t)

串复制

cpp 复制代码
void StrCopy(SqString* s, SqString* t)
{
	strcpy(s->data, t->data);
	return;
}

bool StrEqual(SqString* s, SqString* t)

判串相等

cpp 复制代码
bool StrEqual(SqString* s, SqString* t)
{
	int x = strcmp(s->data, t->data);
	if (x == 0) return true;
	else return false;
}

int StrLength(SqString* s)

求串长

cpp 复制代码
int StrLength(SqString* s)
{
	int count = strlen(s->data);
	return count;
}

SqString* Concat(SqString* s, SqString* t)

串连接

cpp 复制代码
SqString* Concat(SqString* s, SqString* t)
{
	SqString* c = (SqString*)malloc(sizeof(SqString));
	strcpy(c->data, s->data);
	strcat(c->data, t->data);
	return c;
}

SqString* SubStr(SqString* s, int i, int j)

求子串

cpp 复制代码
SqString* SubStr(SqString* s, int i, int j)
{
	int len = StrLength(s);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i<1 || i>len || j<0 || i + j - 1>len) return last;
	int k = 0;
	for ( k = 0; k < j; k++)
	{
		last->data[k] = s->data[i + k-1];
	}
	last->data[k] = '\0';
	return last;
}

SqString* InsStr(SqString*str1, int i, SqString*str2)

插入串

cpp 复制代码
SqString* InsStr(SqString*str1, int i, SqString*str2)
{
	int len1 = StrLength(str1);
	int len2 = StrLength(str2);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i <= 0 || i > len1 + 1) return last;
	int k = 0;
	int j = 0;
	int h = 0;
	for (k = 0; k < i - 1; k++)
	{
		last->data[k] = str1->data[k];
	}
	for ( j = 0; j < len2; j++)
	{
		last->data[k + j] = str2->data[j];
	}
	for ( h = k; h < len1; h++)//小心
	{
		last->data[h + k + j-1] = str1->data[h];
	}
	last->data[h + k + j-1] = '\0';//字符串一定记得要给一个结束标志
	return last;
}

SqString* DelStr(SqString* str, int i, int j)

串删去

cpp 复制代码
SqString* DelStr(SqString* str, int i, int j)
{
	int len = StrLength(str);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i <= 0 || i > len || i + j - 1 > len) return last;//防止越界,力扣不这样做会ovreflow
	int x, y, z;
	for (x = 0; x < i - 1; x++)
	{
		last->data[x] = str->data[x];
	}//x=i-1
	for (y = i + j - 1; y < len; y++)
	{
		last->data[x++] = str->data[y];
	}
	last->data[x] = '\0';
	return last;
}

SqString* RepStr(SqString* s, int i, int j, SqString* t)

子串替换

cpp 复制代码
SqString* RepStr(SqString* s, int i, int j, SqString* t)
{
	int len = StrLength(s);
	int len2 = StrLength(t);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
	int x, y, z;
	for (x = 0; x < i - 1; x++)
	{
		last->data[x] = s->data[x];
	}
	for (y = 0; y < len2; y++)
	{
		last->data[x + y] = t->data[y];
	}
	int h = 0;
	for (z = i + j - 1; z < len; z++)
	{
		last->data[x + y +h] = s->data[z];
		h++;
	}
	last->data[x + y + h] = '\0';
	return last;
}

总代码c语言

cpp 复制代码
#define  _CRT_SECURE_NO_WARNINGS 1
//串的基本运算
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct str
{
	char data[1000];
	int length;
}SqString;
void StrAssign(SqString* s, char* cstr)
{
	strcpy(s->data, cstr);
	return;
}
void Display(SqString* s)
{
	printf("%s", s->data);//*s.data
	return;
}
void Destroy(SqString* s)
{
	free(s);
}
void StrCopy(SqString* s, SqString* t)
{
	strcpy(s->data, t->data);
	return;
}
bool StrEqual(SqString* s, SqString* t)
{
	int x = strcmp(s->data, t->data);
	if (x == 0) return true;
	else return false;
}
int StrLength(SqString* s)
{
	int count = strlen(s->data);
	return count;
}
SqString* Concat(SqString* s, SqString* t)
{
	SqString* c = (SqString*)malloc(sizeof(SqString));
	strcpy(c->data, s->data);
	strcat(c->data, t->data);
	return c;
}
SqString* SubStr(SqString* s, int i, int j)
{
	int len = StrLength(s);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i<1 || i>len || j<0 || i + j - 1>len) return last;
	int k = 0;
	for ( k = 0; k < j; k++)
	{
		last->data[k] = s->data[i + k-1];
	}
	last->data[k] = '\0';
	return last;
}
SqString* InsStr(SqString*str1, int i, SqString*str2)
{
	int len1 = StrLength(str1);
	int len2 = StrLength(str2);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i <= 0 || i > len1 + 1) return last;
	int k = 0;
	int j = 0;
	int h = 0;
	for (k = 0; k < i - 1; k++)
	{
		last->data[k] = str1->data[k];
	}
	for ( j = 0; j < len2; j++)
	{
		last->data[k + j] = str2->data[j];
	}
	for ( h = k; h < len1; h++)//小心
	{
		last->data[h + k + j-1] = str1->data[h];
	}
	last->data[h + k + j-1] = '\0';//字符串一定记得要给一个结束标志
	return last;
}
SqString* DelStr(SqString* str, int i, int j)
{
	int len = StrLength(str);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i <= 0 || i > len || i + j - 1 > len) return last;//防止越界,力扣不这样做会ovreflow
	int x, y, z;
	for (x = 0; x < i - 1; x++)
	{
		last->data[x] = str->data[x];
	}//x=i-1
	for (y = i + j - 1; y < len; y++)
	{
		last->data[x++] = str->data[y];
	}
	last->data[x] = '\0';
	return last;
}
SqString* RepStr(SqString* s, int i, int j, SqString* t)
{
	int len = StrLength(s);
	int len2 = StrLength(t);
	SqString* last = (SqString*)malloc(sizeof(SqString));
	if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
	int x, y, z;
	for (x = 0; x < i - 1; x++)
	{
		last->data[x] = s->data[x];
	}
	for (y = 0; y < len2; y++)
	{
		last->data[x + y] = t->data[y];
	}
	int h = 0;
	for (z = i + j - 1; z < len; z++)
	{
		last->data[x + y +h] = s->data[z];
		h++;
	}
	last->data[x + y + h] = '\0';
	return last;
}
int main()
{
	SqString* str1 = (SqString*)malloc(sizeof(SqString));
	SqString* str2 = (SqString*)malloc(sizeof(SqString));
	char cstr[20] = "abcdef";
	char s[20] = "mm";
	//char s[20] = "de";
	StrAssign(str1, cstr);
	StrAssign(str2, s);
	SqString* str4 = RepStr(str1, 2, 3, str2);
	printf("%s", str4->data);
	
	return 0;
}

总代码c++

cpp 复制代码
//顺序串基本运算的算法
#include <stdio.h>
#define MaxSize 100
typedef struct
{	
	char data[MaxSize];
	int length;			//串长
} SqString;
void StrAssign(SqString &s,char cstr[])	//字符串常量赋给串s
{
	int i;
	for (i=0;cstr[i]!='\0';i++)
		s.data[i]=cstr[i];
	s.length=i;
}
void DestroyStr(SqString &s)
{  }

void StrCopy(SqString &s,SqString t)	//串复制
{
	int i;
	for (i=0;i<t.length;i++)
		s.data[i]=t.data[i];
	s.length=t.length;
}
bool StrEqual(SqString s,SqString t)	//判串相等
{
	bool same=true;
	int i;
	if (s.length!=t.length)				//长度不相等时返回0
		same=false;
	else 
		for (i=0;i<s.length;i++)
			if (s.data[i]!=t.data[i])	//有一个对应字符不相同时返回0
			{	same=false;
				break;
			}
	return same;
}
int StrLength(SqString s)	//求串长
{
	return s.length;
}
SqString Concat(SqString s,SqString t)	//串连接
{
	SqString str;
	int i;
	str.length=s.length+t.length;
	for (i=0;i<s.length;i++)	//将s.data[0..s.length-1]复制到str
		str.data[i]=s.data[i];
	for (i=0;i<t.length;i++)	//将t.data[0..t.length-1]复制到str
		str.data[s.length+i]=t.data[i];
	return str;
}
SqString SubStr(SqString s,int i,int j)	//求子串
{
	SqString str;
	int k;
	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++)  		//将s.data[i..i+j]复制到str
		str.data[k-i+1]=s.data[k];
	str.length=j;
	return str;
} 
SqString InsStr(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++)      		//将s1.data[0..i-2]复制到str
		str.data[j]=s1.data[j];
	for (j=0;j<s2.length;j++)		//将s2.data[0..s2.length-1]复制到str
		str.data[i+j-1]=s2.data[j];
	for (j=i-1;j<s1.length;j++)		//将s1.data[i-1..s1.length-1]复制到str
		str.data[s2.length+j]=s1.data[j];
	str.length=s1.length+s2.length;
	return str;
}
SqString DelStr(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++)       		//将s.data[0..i-2]复制到str
		str.data[k]=s.data[k];
	for (k=i+j-1;k<s.length;k++)	//将s.data[i+j-1..s.length-1]复制到str
		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-1>s.length) //参数不正确时返回空串
		return str;
	for (k=0;k<i-1;k++)				//将s.data[0..i-2]复制到str
		str.data[k]=s.data[k];
	for (k=0;k<t.length;k++)   		//将t.data[0..t.length-1]复制到str
		str.data[i+k-1]=t.data[k];
	for (k=i+j-1;k<s.length;k++)	//将s.data[i+j-1..s.length-1]复制到str
		str.data[t.length+k-j]=s.data[k];
	str.length=s.length-j+t.length;
	return str;
}
void DispStr(SqString s)	//输出串s
{
	int i;
	if (s.length>0)
	{	for (i=0;i<s.length;i++)
			printf("%c",s.data[i]);
		printf("\n");
	}
}

链串的基本运算

为了防止文章过长链串的基本运算我直接全部给出有需要的自取

c语言总代码

cpp 复制代码
#define  _CRT_SECURE_NO_WARNINGS 1
//链串
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
typedef struct snode
{
	char data;
	struct snode* next;
}LinkStrNode;
void StrAssign(LinkStrNode* s, char*cstr)
{
	int len = strlen(cstr);
	LinkStrNode* pre = s;
	for (int i = 0; i < len; i++)
	{
		LinkStrNode* p = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		p->data = cstr[i];
		pre->next = p;
		pre = p;

	}
	pre->next = NULL;
}
void Display(LinkStrNode* s)
{
	LinkStrNode* p = s->next;
	while (p != NULL)
	{
		printf("%c", p->data);
		p = p->next;
	}
	return;
}
void DestroyStr(LinkStrNode* s)
{
	LinkStrNode* p = s->next;
	LinkStrNode* pre = s;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = p->next;
	}
	free(pre);
}
void StrCopy(LinkStrNode* s, LinkStrNode* t)
{
	LinkStrNode* ps = s;
	LinkStrNode* pt = t;
	while (pt != NULL)
	{
		ps->data = pt->data;
		ps = ps->next; pt = pt->next;
	}
	return;
}
bool StrEqual(LinkStrNode* s, LinkStrNode* t)
{
	LinkStrNode* ps = s;
	LinkStrNode* pt = t;
	while (ps != NULL && pt != NULL)
	{
		if (ps->data != pt->data) return false;
		ps = ps->next; pt = pt->next;
	}
	if (ps != NULL || pt != NULL) return false;
	return true;
}
int StrLength(LinkStrNode* s)
{
	int count = 0;
	LinkStrNode* p = s->next;
	while (p != NULL)
	{
		count++;
		p = p->next;
	}
	return count;
}
LinkStrNode* Concat(LinkStrNode* s, LinkStrNode* t)
{
	LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	LinkStrNode* p = last;
	LinkStrNode* ps = s->next;
	LinkStrNode* pt = t->next;

	while (ps != NULL)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		p->next = q;
		ps = ps->next;
		p = q;
	}
	while (pt != NULL)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = pt->data;
		p->next = q;
		pt = pt->next;
		p = q;
	}
	p->next = NULL;
	return last;
}
LinkStrNode* SubStr(LinkStrNode* s, int i, int j)
{
	int len = StrLength(s);
	LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
	LinkStrNode* p = s;
	LinkStrNode* pres = last;
	for (int z = 0; z < i; z++) p = p->next;
	for (int x = 0; x < j; x++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = p->data;
		pres->next = q;
		pres = q;
		p = p->next;
	}
	pres->next = NULL;
	return last;
}
LinkStrNode* InsStr(LinkStrNode* s, int i, LinkStrNode* t)
{
	int len = StrLength(s);
	LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	if (i <= 0 || i > len + 1) return last;
	LinkStrNode* ps = s->next;
	LinkStrNode* pl = last;
	int j, k, h;
	for (j = 0;ps!=NULL&& j < i - 1; j++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		pl->next = q;
		pl = q;
		ps = ps->next;
	}
	int len2 = StrLength(t);
	LinkStrNode* pt = t->next;
	for ( k = 0;pt!=NULL&& k < len2; k++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = pt->data;
		pl->next = q;
		pl = q;
		pt = pt->next;
	}
	for (h = j; ps!=NULL&&h < len; h++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		pl->next = q;
		pl = q;
		ps = ps->next;
	}
	pl->next = NULL;
	return last;
}
LinkStrNode* DelStr(LinkStrNode* s, int i, int j)
{
	int len = StrLength(s);
	
	LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	if (len < 1) return last;
	if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
	LinkStrNode* ps = s->next;
	LinkStrNode* pa = last;
	int x, y, h;
	for ( x = 0; x < i-1; x++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		pa->next = q;
		pa = q;
		ps = ps->next;	
	}	
	for ( y = 0; y < j; y++)
	{
		ps = ps->next;
	}
	for (h = x + y; h < len; h++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		pa->next = q;
		pa = q;
		ps = ps->next;
	}
	pa->next = NULL;
	return last;
}
LinkStrNode* RepStr(LinkStrNode* s, int i, int j, LinkStrNode* t)
{
	int len = StrLength(s);
	LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	if (len < 1) return last;
	if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
	int x, y, z;
	LinkStrNode* ps = s->next;
	LinkStrNode* pt = last;
	for (x = 0; x < i - 1; x++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		pt->next = q;
		pt = q;
		ps = ps->next;
	}
	int len2 = StrLength(t);
	LinkStrNode* pt2 = t->next;
	for (y = 0; pt2!=NULL&&y < len2; y++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = pt2->data;
		pt->next = q;
		pt = q;
		pt2 = pt2->next;
	}
	int k;
	for ( k = 0; k < j; k++) ps = ps->next;
	for (z = x + k; z < len; z++)
	{
		LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
		q->data = ps->data;
		pt->next = q;
		pt = q;
		ps = ps->next;

	}
	pt->next = NULL;
	return last;
}
int main()
{
	LinkStrNode* s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	LinkStrNode* t = (LinkStrNode*)malloc(sizeof(LinkStrNode));
	char str[] = "abcd";
	char str2[] = "mmm";
	StrAssign(t, str2);
 	StrAssign(s, str);
	//LinkStrNode* y = SubStr(s, 2, 3);
	//LinkStrNode*y = Concat(s, t);
	//int h = StrLength(s);
	//int k = StrEqual(t, s);
	//printf("%d", h);
	LinkStrNode* y = RepStr(s, 2,2,t);
	Display(y);
	return 0;
}

c++总代码

cpp 复制代码
//链串基本运算的算法
#include <stdio.h>
#include <malloc.h>
typedef struct snode 
{	
	char data;
	struct snode *next;
} LinkStrNode;
void StrAssign(LinkStrNode *&s,char cstr[])	//字符串常量cstr赋给串s
{
	int i;
	LinkStrNode *r,*p;
	s=(LinkStrNode *)malloc(sizeof(LinkStrNode));
	r=s;						//r始终指向尾结点
	for (i=0;cstr[i]!='\0';i++) 
	{	p=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		p->data=cstr[i];
		r->next=p;r=p;
	}
	r->next=NULL;
}
void DestroyStr(LinkStrNode *&s)
{	LinkStrNode *pre=s,*p=s->next;	//pre指向结点p的前驱结点
	while (p!=NULL)					//扫描链串s
	{	free(pre);					//释放pre结点
		pre=p;						//pre、p同步后移一个结点
		p=pre->next;
	}
	free(pre);						//循环结束时,p为NULL,pre指向尾结点,释放它
}
void StrCopy(LinkStrNode *&s,LinkStrNode *t)	//串t复制给串s
{
	LinkStrNode *p=t->next,*q,*r;
	s=(LinkStrNode *)malloc(sizeof(LinkStrNode));
	r=s;						//r始终指向尾结点
	while (p!=NULL)				//将t的所有结点复制到s
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	r->next=NULL;
}
bool StrEqual(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 (p==NULL && q==NULL)
		return true;
	else
		return false;
}
int StrLength(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)				//将s的所有结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	p=t->next;
	while (p!=NULL)				//将t的所有结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	r->next=NULL;
	return str;
}
LinkStrNode *SubStr(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;						//r指向新建链表的尾结点
	if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
		return str;				//参数不正确时返回空串
	for (k=0;k<i-1;k++)
		p=p->next;
	for (k=1;k<=j;k++) 			//将s的第i个结点开始的j个结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	r->next=NULL;
	return str;
}
LinkStrNode *InsStr(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;								//r指向新建链表的尾结点
	if (i<=0 || i>StrLength(s)+1)		//参数不正确时返回空串
		return str;
	for (k=1;k<i;k++)					//将s的前i个结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	while (p1!=NULL)					//将t的所有结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p1->data;
		r->next=q;r=q;
		p1=p1->next;
	}
	while (p!=NULL)						//将结点p及其后的结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	r->next=NULL;
	return str;
}
LinkStrNode *DelStr(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;						//r指向新建链表的尾结点
	if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
		return str;				//参数不正确时返回空串
	for (k=0;k<i-1;k++)			//将s的前i-1个结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p->data;
		r->next=q;r=q;
		p=p->next;
	}
	for (k=0;k<j;k++)				//让p沿next跳j个结点
		p=p->next;
	while (p!=NULL)					//将结点p及其后的结点复制到str
	{	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;							//r指向新建链表的尾结点
	if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
		return str;		 			//参数不正确时返回空串
	for (k=0;k<i-1;k++)  			//将s的前i-1个结点复制到str
	{	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沿next跳j个结点
		p=p->next;
	while (p1!=NULL)				//将t的所有结点复制到str
	{	q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
		q->data=p1->data;q->next=NULL;
		r->next=q;r=q;
		p1=p1->next;
	}
	while (p!=NULL)					//将结点p及其后的结点复制到str
	{	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");
}

小结:

对文章有任何问题的还请指出,接受大家的批评,让我改进,如果大家有所收获的话还请不要吝啬你们的点赞和收藏,这可以激励我写出更加优秀的文章,本文代码均经过调试运行,如果对你有帮助的话还请给我点个赞。

相关推荐
qq_433554544 分钟前
C++ 面向对象编程:+号运算符重载,左移运算符重载
开发语言·c++
努力学习编程的伍大侠8 分钟前
基础排序算法
数据结构·c++·算法
XiaoLeisj36 分钟前
【递归,搜索与回溯算法 & 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(二)
数据结构·算法·leetcode·决策树·深度优先·剪枝
yuyanjingtao1 小时前
CCF-GESP 等级考试 2023年9月认证C++四级真题解析
c++·青少年编程·gesp·csp-j/s·编程等级考试
Jasmine_llq1 小时前
《 火星人 》
算法·青少年编程·c#
闻缺陷则喜何志丹1 小时前
【C++动态规划 图论】3243. 新增道路查询后的最短距离 I|1567
c++·算法·动态规划·力扣·图论·最短路·路径
charlie1145141911 小时前
C++ STL CookBook
开发语言·c++·stl·c++20
Lenyiin1 小时前
01.02、判定是否互为字符重排
算法·leetcode
小林熬夜学编程1 小时前
【Linux网络编程】第十四弹---构建功能丰富的HTTP服务器:从状态码处理到服务函数扩展
linux·运维·服务器·c语言·网络·c++·http