线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

#include<bits/stdc++.h>
using namespace std;
#define Status int
//--------------串的定长顺序存储表示-------------------
#define MAXSTRLEN 255
typedef unsigned char SString[MAXSTRLEN + 1];			//0号单元存放串的长度
 
Status StrAssign(SString &T, const char *chars) {
   
	int len, uncut;
	int i;
	len = strlen(chars);
	if (len > MAXSTRLEN) {
   
		T[0] = MAXSTRLEN;
		uncut = 0;
	}
	else {
   
		T[0] = len;
		uncut = 1;
	}
	for(i = 1; i <= T[0]; i++) {
   
		T[i] = chars[i - 1];
	}
	return uncut;
}
 
Status StrCopy(SString &T, SString S) {
   
	if (S == NULL)
		return 0;
	for(int i = 0; i <= S[0]; i++)
		T[i] = S[i];				//最后一位'\0'
	return 1;
}
 
Status StrEmpty(SString S) {
   
	if (S == NULL)
		return 0;
	if (S[0] == 0)
		return 1;
	else
		return 0;
}
 
Status StrCompare(SString S, SString T) {
   
	if (S && T == NULL)
		return 0;
	for(int i = 1; i <= S[0] && i <= T[0]; i++) {
   
		if (S[i] != T[i])
			return S[i] - T[i];
	}
	return S[0] - T[0];
}
 
Status StrLength(SString S) {
   
	//串S存在,返回S的元素个数
	if (S == NULL)
		return 0;
	return S[0];
}
 
Status ClearString(SString &S) {
   
	//串S存在,将S清为空串
	if (S == NULL) 
		return 0;
	S[0] = 0;
	return 1;
}
//算法4.2
Status Concat(SString &T, SString S1, SString S2) {
   
	int i, j;
	int uncut;
	if (S1 && S2 == NULL)
		return 0;
	if (S1[0] + S2[0] <= MAXSTRLEN) {
   
		for (i = 1; i <= S1[0]; i++)
			T[i] = S1[i];
		for (i = S1[0] + 1, j = 1; i <= S1[0] + S2[0], j <= S2[0]; i++, j++)
			T[i] = S2[j];
		T[0] = S1[0] + S2[0];
		uncut = 1;
	}
	else if (S1[0] < MAXSTRLEN) {
   
		for(i = 1; i <= S1[0]; i++)
			T[i] = S1[i];
		for(i = S1[0] + 1, j = 1; i <= MAXSTRLEN, j <= MAXSTRLEN - S1[0]; i++, j++) 
			T[i] = S2[j];
		T[0] = MAXSTRLEN;
		uncut = 0;
	}
	else {
   
		for(i = 0; i <= MAXSTRLEN; i++)
			T[i] = S1[i];
		uncut = 0;
	}
	return uncut;
}
//算法4.3
Status SubString(SString &Sub, SString S, int pos, int len) {
   
	if (pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1)
		return 0;
	int i, j;
	for(i = 1, j = pos; i <= len, j <= pos + len -1; i++, j++)
		Sub[i] = S[j];
	Sub[0] = len;
	return 1;
}
//算法4.1
Status Index(SString S, SString T, int pos) {
   
	int n, m, i;
	if (pos > 0) {
   
		n = StrLength(S);
		m = StrLength(T);
		i = pos;
		SString sub;
		while(i <= n - m + 1) {
   
			SubString(sub, S, i, m);
			if (StrCompare(sub, T) != 0)
				i++;
			else
				return i;	//返回子串在主串中的位置
		}
	}
	return 0;				//S中不存在与T相等的子串
}
 
Status Replace(SString &S, SString T, SString V) {
   
	int i = 1;
	if (StrEmpty(T))
		return 0;
	while(i) {
   
		i = Index(S, T, i);
		if (i) {
   
			StrDelete(S, i, T[0]);
			StrInsert(S, i, V);
			i += V[0];
		}
	}
	return 1;
}

Status StrInsert(SString &S, int pos, SString T) {
   
	if ((S && T == NULL) || (pos < 1) || (pos > S[0] + 1))
		return 0;
	int uncut;
	int i;
	if (S[0] + T[0] <= MAXSTRLEN) {
   
		for(i = S[0]; i >= pos; i--)
			S[i + T[0]] = S[i];
		for(i = pos; i < pos + T[0]; i++)
			S[i] = T[i - pos + 1];
		S[0] += T[0];
		uncut = 1;
	}
	else {
   
		for(i = MAXSTRLEN; i >= pos; i--)
			S[i] = S[i - T[0]];
		for(i = pos; i < pos + T[0]; i++)
			S[i] = T[i - pos + 1];
		S[0] = MAXSTRLEN;
		uncut = 0;
	}
}
 
Status StrDelete(SString &S, int pos, int len) {
   
	if ((S == NULL) || (pos < 1) || (pos > S[0] - len + 1) || (len < 0))
		return 0;
	int i;
	for(i = pos; i <= pos + len; i++)
		S[i] = S[i + len];
	S[0] -= len;
	return 1;
}
 
Status DestroyString (SString &S) {
   
	return 0;
}