(数据结构)(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 snode {
	char data;
	struct snode* next;
}LinkStrNode, * LinkStr;

//生成链串 
void StrAssign(LinkStr& s, char arr[]) {
	int i;
	LinkStr r, p;
	s = new LinkStrNode;
	r = s;        //r始终指向尾节点
	for (i = 0; arr[i] != 0; i++) {
		p = new LinkStrNode;
		p->data = arr[i];
		r->next = p;
		r = p;
	}
	r->next = NULL;
}

//销毁串
void DestroyStr(LinkStr& s) {
	LinkStr pre = s;
	LinkStr p = s->next;
	while (p != NULL) {
		delete pre;
		pre = p;
		p = pre->next;
	}
	delete pre;
}

//串的复制
void StrCopy(LinkStr& s, LinkStr t) {
	LinkStr p = t->next;
	LinkStr q, r;
	s = new LinkStrNode;
	r = s;
	while (p != NULL) {
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	r->next = NULL;

}

//输出串
void PrintStr(LinkStr s) {
	cout << "打印:";
	LinkStr p = s->next;
	while (p != NULL) {
		cout << p->data;
		p = p->next;
	}
	cout << endl;
}


//求串长
int StrLength(LinkStr s) {
	int i = 0;
	LinkStr p = s->next;
	while (p != NULL)
	{
		i++;
		p = p->next;
	}
	return i;
}

//串的连接
LinkStr StrContact(LinkStr s, LinkStr t) {
	LinkStr str;
	LinkStr p = s->next;
	LinkStr q, r;
	str = new LinkStrNode;
	r = str;
	while (p != NULL) {
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	p = t->next;
	while (p != NULL) {
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	r->next = NULL;
	return str;
}



//判断串相等
bool StrEqual(LinkStr s, LinkStr t) {
	LinkStr p = s->next;
	LinkStr q = t->next;
	while (p != NULL && q != NULL && q->data == p->data)
	{
		p = p->next;
		q = q->next;

	}
	if (p == NULL && q == NULL) {
		return true;
	}
	else {
		return false;
	}
}

//子串的插入
LinkStr StrIns(LinkStr s, int i, LinkStr t) {
	int k;
	LinkStr str;
	LinkStr p = s->next;
	LinkStr pl = t->next;
	LinkStr q, r;
	str = new LinkStrNode;
	str->next = NULL;
	r = str;
	if (i <= 0 || i > StrLength(s) + 1) {
		cout << "插入位置有误!";
		return str;//返回空串

	}
	for (k = 0; k < i; k++)
	{
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	while (pl != NULL) {
		q = new LinkStrNode;
		q->data = pl->data;
		r->next = q;
		r = q;
		pl = pl->next;
	}
	while (p != NULL) {
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	r->next = NULL;
	return str;

}

//子串的删除
LinkStr StrDelete(LinkStr s, int i, int j) {
	//删除第i个位置长度为j的字符串
	int k;
	LinkStr str;
	LinkStr p = s->next;
	LinkStr q, r;
	str = new LinkStrNode;
	str->next = NULL;
	r = str;
	if (i <= 0 || i > StrLength(s) || j<0 ||  i+j - 1>StrLength(s))
	{
		cout << "输入不合法" << endl;
		return str;
	}
	for (k = 1; k < i; k++)//删除子串从第i个位置开始
	{
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	for (k = 0; k < j; k++) {//从头开始,移动到j个位置
		p = p->next;
	}
	while (p != NULL) {
		q = new LinkStrNode;
		q->data = p->data;
		r->next = q;
		r = q;
		p = p->next;
	}
	r->next = NULL;

	return str;
}

int main() {

	cout << "建立串s,并赋值内容为{abcde}" << endl;
	char arr[] = "abcde";
	LinkStr s;
	StrAssign(s, arr);
	PrintStr(s);

	cout << "新建串s2复制于旧串并打印。" << endl;
	LinkStr s2;
	s2 = new LinkStrNode;
	StrCopy(s2, s);
	PrintStr(s2);

	cout << "判断s和s2是否相等:";
	if (StrEqual(s, s2)) {
		cout << "true" << endl;
	}
	else {
		cout << "false" << endl;
	}

	int i = StrLength(s);
	cout << "串s的长度为:" << i << endl;

	LinkStr s3;
	s3 = new LinkStrNode;
	s3 = StrContact(s, s2);
	cout << "建立新串s3,s3由s和s2合并:";
	PrintStr(s3);
	cout << endl;


	char arr2[] = "12345";
	LinkStr s4;
	s4 = new LinkStrNode;
	StrAssign(s4, arr2);
	cout << "建立新串s4,并且给s4赋值为{12345}" << endl;
	cout << "将s4作为子串插入到s1的第三个元素后面,建立串s5:";
	LinkStr s5;
	s5 = new LinkStrNode;
	s5 = StrIns(s, 3, s4);
	PrintStr(s5);

	cout << "建立新串s6,由s5删除第三个位置到第八个位置形成的子串:" << endl;
	LinkStr s6;
	s6 = new LinkStrNode;
	s6 = StrDelete(s5, 4, 5);
	PrintStr(s6);

}

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

 

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