(数据结构)(C++)顺序串的建立和部分基本操作

#define _CRT_SECURE_NO_WARNINGS 1
#include 
 
using namespace std;
 
#define  ElemType char

#define MAXSIZE 40
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0                                               
#define INFEASIBLE -1
#define OVERFLOW -1

typedef struct{
	ElemType data[MAXSIZE];
	int length;
}Sqstring,*SqString;

//生成串 
void StrAssign(SqString &s,char arr[]){
	int i;
    while(arr[i]!='\0'){
    	s->data[i]=arr[i];
    	i++;
	}
    s->length=i;
    
} 

//销毁串
void DestroyStr(SqString &s){
	/*本章的顺序串是直接采用顺序串本身来表示的,而不是顺序串指针
	 ,它的存储空间是由操作系统管理,即由操作系统分配其空间,
	 并在超出作用域的情况下释放空间 */ 
} 

//串的复制
void CopyStr(SqString &s,SqString t){
	int i;
	for(i=0;ilength;i++){
		s->data[i]=t->data[i];
		s->length=t->length;
	}
}

//判断串相等
bool EqualStr(SqString s,SqString t){
	bool same=true;
	int i;
	if(s->length!=t->length){
		return false;
	}else{
		for(i=0;ilength;i++){
			if(s->data[i]!=t->data[i]){
				same=false;
				break;
			}
		}
	}
	return same;
} 

//求串长
int StrLength(SqString s){
	return s->length;
} 

//串的连接
SqString ContactStr(SqString s,SqString t){
	SqString str;
	int i;
	str->length=s->length+t->length;
	for(i=0;ilength;i++){
		str->data[i]=s->data[i]; 
	} 
	for(i=0;ilength;i++){
		str->data[s->length+i]=t->data[i];
	}
	return str;
} 

//求子串(顺序串逻辑位置第i开始,连续j个字符组成的子串)
SqString SubStr(SqString s){
	cout<<"请输入子串起始位置和子串大小:"; 
    int i;
	int j;
	cin>>i;
	cin>>j; 
	SqString str;
	str=new Sqstring;
	str->length=0;
	if(i<=0||i>s->length||j<0||i+j-1>s->length){
		cout<<"输入有误"; 
		return str; 
	}
	int k;
	for(k=i-1;kdata[k-i+1]=s->data[k]; 
	}
	str->length=j;
	return str; 
} 

//子串的删除(顺序串逻辑位置第i开始,连续j个字符组成的子串)
SqString DestroyStr(SqString s){
	SqString str;
	int i;
	int j;
	cout<<"请输入删除子串起始位置和元素个数:";
	cin>>i;
	cin>>j;
	if(i<=0||i>s->length||i+j>s->length+1){
		return str;//返回空串 
	}
	int k;
	for(k=0;kdata[k]=s->data[k];
	}
	for(k=i+j-1;klength;k++){
		str->data[k-j]=s->data[k];
	}
	str->length=s->length-j;
	return str; 
} 

//子串的插入
SqString InsStr(SqString s1,int i,SqString s2){
	int j;
	i=i+1;
	SqString str;
	str=new Sqstring;
	str->length=0;
	if(i<=0||i>s1->length){
		return str;
	}
	for(j=0;jdata[j]=s1->data[j]; 
	}
	for(j=0;jlength;j++){
		str->data[i+j-1]=s2->data[j]; 
	}
	for(j=i-1;jlength;j++){
		str->data[s2->length+j]=s1->data[j]; 
	}
	str->length=s1->length+s2->length;
	return str;
} 

//子串的替换(顺序串逻辑位置第i开始,替换连续j个字符,插入t的子串)
SqString RepStr(SqString s,SqString t){
	int k;
	SqString str;
	str->length=0;
	cout<<"请输入起始位置和替换元素个数";
	int i;
	int j; 
	cin>>i;
	cin>>j;
	if(i<=0||i>s->length||i+j-1>s->length){
		return str;
	}
	for(k=0;kdata[k]=s->data[k];
	}
	for(k=0;klength;k++){
		str->data[i+k-1]=t->data[k]; 
	}
	for(k=i+j-1;klength;k++){
		str->data[t->length+k-j]=s->data[k]; 
	}   
	str->length=s->length-j+t->length; 
	return str;
}

//输出串
void PrintStr(SqString s){
	int i;
	cout<<"输出:";
	if(s->length>0){
		int i;
		for(i=0;ilength;i++){
			char c=s->data[i];
			printf("%c",c);
		} 
	}
} 

int main(){
	   
	    cout<<"数组:"<<"{abcde}"<length<length<

(数据结构)(C++)顺序串的建立和部分基本操作_第1张图片

 

你可能感兴趣的:(数据结构和算法(C/C++),数据结构,c++,算法)