数据结构:串

1、串的概念
       字符串简称串,是一种特殊的线性表,它的数据元素仅由一个字符组成。

2、串的定义
       串(String)是由零个或多个字符组成的有限序列,又称字符串。

       其中s是串名,用双引号括起来的字符序列为串值,但引号本身并不属于串的内容。ai(1<=i<=n)是一个任意字符,它称为串的元素,是构成串的基本单位,i是它在整个串中的序号;n为串的长度,表示串中所包含的字符个数。

3、术语描述
(1)长度–串中字符的个数,称为串的长度。
(2)空串–长度为零的字符串称为空串。
(3)空格串–由一个或多个连续空格组成的串称为空格串。
(4)串相等–两个串相等,是指两个串的长度相等且对应的字符都相等。
(5)子串–串中任意连续的字符组成的子序列称为该串的子串。
(6)主串–包含子串的串为该子串的主串。
(7)模式匹配–子串的定位运算又称为模式匹配,是一个求子串的队医给字符在主串中序号的运算。被匹配的主串称为目标串,子串称为模式。

字符串场长度为n,子串个数为:n(n+1)/2+1      注:1为空串

 

串实现

1.初始化

2.拷贝

3.链接

4.插入

5.删除

6.判断是否相等

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct String
{
	char* pstr;
	int curlen;
	int totallen;//备用空间  
}String,*PString;


void StrAssign(PString pstring, char* sub)
{
	if (pstring == NULL || sub == NULL)
	{
		return;
	}
	int len = strlen(sub);
	pstring->pstr = (char*)malloc(len + 1);
	if (pstring->pstr == NULL)exit(0);
	for (int i = 0; i < len; ++i)
	{
		pstring->pstr[i] = sub[i];
	}
	pstring->pstr[len] = '\0';

	pstring->curlen = len;
	pstring->totallen = len + 1;
}

void StrCopy(PString ps1, PString ps2)
{
	if (ps1 == NULL || ps2 == NULL)
	{
		return;
	}
	int len1 = ps1->curlen;
	int len2 = ps2->curlen;
	
	if (len2 >= ps1->totallen)
	{
		char* pnewstr = (char*)malloc(len2 + 1);
		free(ps1->pstr);
		ps1->pstr = pnewstr;
		ps1->totallen = len2 + 1;
	}
	int i = 0;
	for (i; i < len2 + 1; ++i)
	{
		ps1->pstr[i] = ps2->pstr[i];
	}
	ps1->curlen = len2;
}

void StrCat(PString ps1, PString ps2)
{
	if (ps1 == NULL || ps2 == NULL)
	{
		return;
	}
	int len1 = ps1->curlen;
	int len2 = ps2->curlen;

	if (len1 + len2 >= ps1->totallen)
	{
		char* pnewstr = (char*)malloc(len1 + len2 + 1);
		for (int i = 0; i < len1; ++i)
		{
			pnewstr[i] = ps1->pstr[i];
		}
		free(ps1->pstr);
		ps1->pstr = pnewstr;
		ps1->totallen = len1 + len2 + 1;
	}

	for (int j = 0; j < len2 + 1; ++j)
	{
		ps1->pstr[j + len1] = ps2->pstr[j];
	}
	ps1->curlen = len1 + len2;
}

bool StrInsert(PString ps1, int pos, PString ps2)
{
	if (ps1 == NULL || ps2 == NULL || pos < 0 || pos > ps1->curlen)
	{
		return false;
	}
	int len1 = ps1->curlen;
	int len2 = ps2->curlen;

	if (len1 + len2 >= ps1->totallen)
	{
		char* pnewstr = (char*)malloc(len1 + len2 + 1);
		for (int i = 0; i < len1; ++i)
		{
			pnewstr[i] = ps1->pstr[i];
		}
		free(ps1->pstr);
		ps1->pstr = pnewstr;
		ps1->totallen = len1 + len2 + 1;
	}
	for (int index = len1; index >= pos; index--)
	{
		ps1->pstr[index + len2] = ps1->pstr[index];
	}
	for (int i = 0; i < len2; ++i)
	{
		ps1->pstr[i + pos] = ps2->pstr[i];
	}
	ps1->curlen = len1 + len2;
	return true;
}

bool StrDelete(PString ps, int pos, int len)
{
	if (ps == NULL || pos < 0 || pos + len > ps->curlen)
	{
		return false;
	}
	int index = pos + len;
	for (index; index <= ps->curlen; index++)
	{
		ps->pstr[index - len] = ps->pstr[index];
	}
	ps->curlen = ps->curlen - len;
	return true;
}

bool StrEqual(PString ps1, PString ps2)
{
	if (ps1 == NULL || ps2 == NULL)
	{
		return false;
	}
	if (ps1->curlen != ps2->curlen)
	{
		return false;
	}
	for (int i = 0; i < ps1->curlen; i++)
	{
		if (ps1->pstr[i] != ps2->pstr[i])
		{
			return false;
		}
	}
	return true;
}

void Print(PString ps)
{
	if (ps == NULL)
	{
		return;
	}
	int i = 0;
	for (i; i < ps->curlen; ++i)
	{
		printf("%c", ps->pstr[i]);
	}
	printf("\n");
}

void Destroyed(PString ps1)
{
	if (ps1 == NULL)
	{
		return;
	}
	free(ps1->pstr);
	ps1->pstr = NULL;
	ps1->curlen = ps1->totallen = 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值