学习笔记------数据结构(C语言版)串的堆分配存储表示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CYTCHAN/article/details/50659772

//HeapString.cpp

#include"predefined.h"
#include"HeapString.h"

void InitString(HString *S)
//初始化S为空串
{
	(*S).ch=NULL;
	(*S).length=0;
}

Status StrAssign(HString *T,char *chars)
//生成一个其值等于串常量chars的串T
{
	int i;
	if(!(*T).ch)
		free((*T).ch);
	i=strlen(chars);
	if(!i)
	{
		(*T).ch=NULL;
		(*T).length=0;
	}
	else
	{
		(*T).ch=(char *)malloc(i*sizeof(char));
		if(!(*T).ch)
			exit(OVERFLOW);
		(*T).length=i;
		for(i=0;i<(*T).length;i++)
		{
			(*T).ch[i]=chars[i];
		}
	}
	return OK;
}

Status StrCopy(HString *T,HString S)
//由串S复制得T
{
	int i;
	if((*T).ch)
		free((*T).ch);
	(*T).length=S.length;
	if(!((*T).ch=(char *)malloc((*T).length*sizeof(char))))
		exit(OVERFLOW);
	for(i=0;i<(*T).length;i++)
		(*T).ch[i]=S.ch[i];
	return OK;
}

Status StrEmpty(HString S)
//若S为空串,则返回TRUE,否则返回FALSE
{
	if(!S.length)
		return TRUE;
	else
		return FALSE;
}

int StrLength(HString S)
//返回S的元素个数,称为串的长度
{
	return S.length;
}

int StrCompare(HString S,HString T)
//若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0
{
	int i;
	for(i=0;i<S.length&&i<T.length;i++)
	{
		if(S.ch[i]!=T.ch[i])
			return S.ch[i]-T.ch[i];
	}
	return S.length-T.length;
}

Status ClearString(HString *S)
//将S清空
{
	if((*S).ch)
	{
		free((*S).ch);
		(*S).ch=NULL;
	}
	(*S).length=0;
	return OK;
}

Status Concat(HString *T,HString S1,HString S2)
//用T返回由S1和S2联接而成的新串
{
	int i;
	if(!(*T).ch)
		free((*T).ch);
	(*T).length=S1.length+S2.length;
	(*T).ch=(char*)malloc((*T).length*sizeof(char));
	if(!(*T).ch)
		exit(OVERFLOW);
	for(i=0;i<(*T).length;i++)
	{
		if(i<S1.length)
			(*T).ch[i]=S1.ch[i];
		else
			(*T).ch[i]=S2.ch[i-S1.length];
	}
	return OK;
}

Status SubString(HString *Sub,HString S,int pos,int len)
//用Sub返回串的第pos个字符起长度为len的子串
//其中,1<=pos<=StrLength(S)且0<=len<=StrLength(S)-pos+1
{
	int i;
	if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
		return ERROR;
	if((*Sub).ch)
	{
		free((*Sub).ch);
	}
	if(!len)
	{
		(*Sub).length=0;
		(*Sub).ch=NULL;
	}
	else
	{
		(*Sub).ch=(char *)malloc(len*sizeof(char));
		if(!(*Sub).ch)
			exit(OVERFLOW);
		(*Sub).length=len;
		for(i=0;i<(*Sub).length;i++,pos++)
			(*Sub).ch[i]=S.ch[pos-1];
	}
	return OK;
}

Status Index(HString S,HString T,int pos)
//算法4.1:若主串S中存在和串T值相同的子串。则返回它在主串S中第pos个字符之后的第一次出现的位置;否则函数返回0
{
	int m,n,i;
	HString Sub;
	InitString(&Sub);
	if(pos>0)
	{
		n=S.length;
		m=T.length;
		i=pos;
		while(i<=n-m+1)
		{
			SubString(&Sub,S,i,m);
			if(StrCompare(Sub,T)!=0)
				++i;
			else
				return i;
		}
	}
	return 0;
}

Status Replace(HString *S,HString T,HString V)
//用V替换主串S中出现的所有与T相等的不重叠的子串
{
	int i,j;
	if(!Index(*S,T,1)||StrEmpty(T))
		return ERROR;
	for(i=1;i<=(*S).length;i++)
	{
		if(j=Index(*S,T,i))
		{
			StrDelete(S,j,T.length);
			StrInsert(S,j,V);
			i=j+V.length-1;
		}
	}
	return OK;
}

Status StrInsert(HString *S,int pos,HString T)
//在串的第pos个字符之前插入串T
{
	int i;
	if(pos<1||pos>(*S).length)
		return ERROR;
	(*S).length+=T.length;
	(*S).ch=(char*)realloc((*S).ch,(*S).length*sizeof(char));
	if(!(*S).ch)
		exit(OVERFLOW);
	for(i=(*S).length-1;i>=pos+T.length-1;i--)
		(*S).ch[i]=(*S).ch[i-T.length];
	for(i=0;i<T.length;i++)
		(*S).ch[pos+i-1]=T.ch[i];
	return OK;
}

Status StrDelete(HString *S,int pos,int len)
//从串S中删除第pos个字符起长度为len的子串
{
	int i;
	if(pos<1||pos+len>(*S).length+1||len<0)
		return ERROR;
	(*S).length-=len;
	for(i=0;pos-1+i<(*S).length;i++)
		(*S).ch[pos-1+i]=(*S).ch[pos+len-1+i];
	return OK;
}

void StrPrint(HString S)
{
	if(StrEmpty(S))
		printf("▲空串!!!\n");
	else
	{
		int i;
		for(i=0;i<S.length;i++)
		{
			printf("%c ",S.ch[i]);
		}
	}
}

//HeapString.h

typedef struct
{
	char *ch;
	int length;
}HString;

void InitString(HString *S);
Status StrAssign(HString *T,char *chars);
Status StrCopy(HString *T,HString S);
Status StrEmpty(HString S);
int StrLength(HString S);
int StrCompare(HString S,HString T);
Status ClearString(HString *S);
Status Concat(HString *T,HString S1,HString S2);
Status SubString(HString *Sub,HString S,int pos,int len);
Status Index(HString S,HString T,int pos);
Status Replace(HString *S,HString T,HString V);
Status StrInsert(HString *S,int pos,HString T);
Status StrDelete(HString *S,int pos,int len);
void StrPrint(HString S);

//main.cpp

#include"predefined.h"
#include"HeapString.h"
int main()
{
	char *chars="abcdefg";
	char *t="***";
	char *v="^^^^";
	HString S,Tmp,T,V,Sub;
	int i;
	printf("Function 1\n★函数void InitString(HString *S)测试...\n");
	printf("▲初始化空串Tmp...\n\n");
	InitString(&Tmp);
	InitString(&T);
	InitString(&S);
	InitString(&V);
	InitString(&Sub);
	printf("Function 2\n★函数Status StrAssign(HString *T,char *chars)测试...\n");
	printf("▲为Tmp赋值%s...\n\n",chars);
	StrAssign(&Tmp,chars);
	printf("Function 3\n★函数Status StrEmpty(HString S)测试...\n");
	StrEmpty(Tmp)?printf("▲Tmp空串!!!\n\n",chars):printf("▲Tmp非空!!!\n\n");
	printf("Function 4\n★函数int StrLength(HString S)测试...\n");
	i=StrLength(Tmp);
	printf("▲Tmp的长度为:%d\n\n",i);
	printf("Function 5\n★函数void StrPrint(HString S)测试...\n");
	printf("▲Tmp=\"");
	StrPrint(Tmp);
	printf("\"\n\n");
	printf("Function 6\n★函数Status StrCopy(HString *T,HString S)测试...\n");
	printf("▲复制Tmp到S...\n");
	StrCopy(&S,Tmp);
	printf("▲S=\"");
	StrPrint(S);
	printf("\"\n\n");
	printf("Function 7\n★函数int StrCompare(HString S,HString T)测试...\n");
	printf("▲比较字符串Tmp、S: ");
	i=StrCompare(Tmp,S);
	i==0?printf("Tmp=S\n\n"):(i<0?printf("Tmp<S\n\n"):printf("Tmp>S\n\n"));
	printf("Function 8\n★函数Status StrInsert(HString *S,int pos,HString T)测试...\n");
	printf("▲将\"***\"赋给T...\n");
	StrAssign(&T,t);
	printf("▲在S的第5个字符前插入T...\n");
	StrInsert(&S,5,T);
	printf("▲S=\"");
	StrPrint(S);
	printf("\"\n\n");
	printf("Function 9\n★函数Status Index(HString S,HString T,int pos)测试...\n");
	printf("▲获取\"***\"在S中从第1个字符算起的第一次出现的位置...\n");
	i=Index(S,T,1);
	printf("▲\"***\"在S中从第1个字符算起的第一次出现的位置为:%d\n\n",i);
	printf("Function 10\n★函数Status SubString(HString *Sub,HString S,int pos,int len)测试...\n");
	printf("▲用Sub返回S中第5个字符起的3个字符...\n");
	SubString(&Sub,S,5,3);
	printf("▲Sub=\"");
	StrPrint(Sub);
	printf("\"\n\n");
	printf("Function 11\n★函数Status Replace(HString *S,HString T,HString V)测试...\n");
	printf("▲将\"^^^^\"赋给V...\n");
	StrAssign(&V,v);
	printf("▲用\"^^^^\"替换S中的\"***\"...\n");
	Replace(&S,T,V);
	printf("▲S=\"");
	StrPrint(S);
	printf("\"\n\n");
	printf("Function 12\n★函数Status StrDelete(HString *S,int pos,int len)测试...\n");
	printf("▲删除S第5个字符起的4个字符...\n");
	StrDelete(&S,5,4);
	printf("▲S=\"");
	StrPrint(S);
	printf("\"\n\n");
	printf("Function 13\n★函数Status ClearString(HString *S)测试...\n");
	printf("▲清空S前:");
	StrEmpty(S)?printf("▲S空串!!!\n"):printf("▲S非空!!!\n");
	ClearString(&S);
	printf("▲清空S后:");
	StrEmpty(S)?printf("▲S空串!!!\n\n"):printf("▲S非空!!!\n\n");
	printf("Function 14\n★函数Status Concat(HString *T,HString S1,HString S2)测试...\n");
	printf("▲链接T和V形成Tmp...\n");
	Concat(&Tmp,T,V);
	printf("▲Tmp=\"");
	StrPrint(Tmp);
	printf("\"\n\n");
}

//predefined.h

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define   TRUE           1
#define   FALSE          0
#define   OK             1
#define   ERROR          0
#define   INFEASIBLE    -1
#define   OVERFLOW      -2
typedef   int Status;
//函数暂停一会
#define Wait(x) \
{\
	double _Loop_Num_;\
	for(_Loop_Num_=0.01;_Loop_Num_<=100000.0*x;_Loop_Num_+=0.01)\
	;\
}


展开阅读全文

没有更多推荐了,返回首页