【数据结构】字符串顺序存储结构

前端之家收集整理的这篇文章主要介绍了【数据结构】字符串顺序存储结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include "string.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "io.h"
  5. #include "math.h"
  6. #include "time.h"
  7.  
  8. #define OK 1
  9. #define ERROR 0
  10. #define TRUE 1
  11. #define FALSE 0
  12.  
  13. #define MAXSIZE 40 /* 存储空间初始分配量 */
  14.  
  15. typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
  16. typedef int ElemType; /* ElemType类型根据实际情况而定,这里假设为int */
  17.  
  18. typedef char String[MAXSIZE+1]; /* 0号单元存放串的长度 */
  19.  
  20. /* 生成一个其值等于chars的串T */
  21. Status StrAssign(String T,char *chars)
  22. {
  23. int i;
  24. if(strlen(chars)>MAXSIZE)
  25. return ERROR;
  26. else
  27. {
  28. T[0]=strlen(chars);
  29. for(i=1;i<=T[0];i++)
  30. T[i]=*(chars+i-1);
  31. return OK;
  32. }
  33. }
  34.  
  35. /* 由串S复制得串T */
  36. Status StrCopy(String T,String S)
  37. {
  38. int i;
  39. for(i=0;i<=S[0];i++)
  40. T[i]=S[i];
  41. return OK;
  42. }
  43.  
  44. /* 若S为空串,则返回TRUE,否则返回FALSE */
  45. Status StrEmpty(String S)
  46. {
  47. if(S[0]==0)
  48. return TRUE;
  49. else
  50. return FALSE;
  51. }
  52.  
  53. /* 初始条件: 串S和T存在 */
  54. /* 操作结果: 若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0 */
  55. int StrCompare(String S,String T)
  56. {
  57. int i;
  58. for(i=1;i<=S[0]&&i<=T[0];++i)
  59. if(S[i]!=T[i])
  60. return S[i]-T[i];
  61. return S[0]-T[0];
  62. }
  63.  
  64. /* 返回串的元素个数 */
  65. int StrLength(String S)
  66. {
  67. return S[0];
  68. }
  69.  
  70. /* 初始条件:串S存在。操作结果:将S清为空串 */
  71. Status ClearString(String S)
  72. {
  73. S[0]=0;/* 令串长为零 */
  74. return OK;
  75. }
  76.  
  77. /* 用T返回S1和S2联接而成的新串。若未截断,则返回TRUE,否则FALSE */
  78. Status Concat(String T,String S1,String S2)
  79. {
  80. int i;
  81. if(S1[0]+S2[0]<=MAXSIZE)
  82. { /* 未截断 */
  83. for(i=1;i<=S1[0];i++)
  84. T[i]=S1[i];
  85. for(i=1;i<=S2[0];i++)
  86. T[S1[0]+i]=S2[i];
  87. T[0]=S1[0]+S2[0];
  88. return TRUE;
  89. }
  90. else
  91. { /* 截断S2 */
  92. for(i=1;i<=S1[0];i++)
  93. T[i]=S1[i];
  94. for(i=1;i<=MAXSIZE-S1[0];i++)
  95. T[S1[0]+i]=S2[i];
  96. T[0]=MAXSIZE;
  97. return FALSE;
  98. }
  99. }
  100.  
  101. /* 用Sub返回串S的第pos个字符起长度为len的子串。 */
  102. Status SubString(String Sub,String S,int pos,int len)
  103. {
  104. int i;
  105. if(pos<1||pos>S[0]||len<0||len>S[0]-pos+1)
  106. return ERROR;
  107. for(i=1;i<=len;i++)
  108. Sub[i]=S[pos+i-1];
  109. Sub[0]=len;
  110. return OK;
  111. }
  112.  
  113. /* 返回子串T在主串S中第pos个字符之后的位置。若不存在,则函数返回值为0。 */
  114. /* 其中,T非空,1≤pos≤StrLength(S)。 */
  115. int Index(String S,String T,int pos)
  116. {
  117. int i = pos; /* i用于主串S中当前位置下标值,若pos不为1,则从pos位置开始匹配 */
  118. int j = 1; /* j用于子串T中当前位置下标值 */
  119. while (i <= S[0] && j <= T[0]) /* 若i小于S的长度并且j小于T的长度时,循环继续 */
  120. {
  121. if (S[i] == T[j]) /* 两字母相等则继续 */
  122. {
  123. ++i;
  124. ++j;
  125. }
  126. else /* 指针后退重新开始匹配 */
  127. {
  128. i = i-j+2; /* i退回到上次匹配首位的下一位 */
  129. j = 1; /* j退回到子串T的首位 */
  130. }
  131. }
  132. if (j > T[0])
  133. return i-T[0];
  134. else
  135. return 0;
  136. }
  137.  
  138.  
  139. /* T为非空串。若主串S中第pos个字符之后存在与T相等的子串, */
  140. /* 则返回第一个这样的子串在S中的位置,否则返回0 */
  141. int Index2(String S,int pos)
  142. {
  143. int n,m,i;
  144. String sub;
  145. if (pos > 0)
  146. {
  147. n = StrLength(S); /* 得到主串S的长度 */
  148. m = StrLength(T); /* 得到子串T的长度 */
  149. i = pos;
  150. while (i <= n-m+1)
  151. {
  152. SubString (sub,S,i,m); /* 取主串中第i个位置长度与T相等的子串给sub */
  153. if (StrCompare(sub,T) != 0) /* 如果两串不相等 */
  154. ++i;
  155. else /* 如果两串相等 */
  156. return i; /* 则返回i值 */
  157. }
  158. }
  159. return 0; /* 若无子串与T相等,返回0 */
  160. }
  161.  
  162.  
  163. /* 初始条件: 串S和T存在,1≤pos≤StrLength(S)+1 */
  164. /* 操作结果: 在串S的第pos个字符之前插入串T。完全插入返回TRUE,部分插入返回FALSE */
  165. Status StrInsert(String S,String T)
  166. {
  167. int i;
  168. if(pos<1||pos>S[0]+1)
  169. return ERROR;
  170. if(S[0]+T[0]<=MAXSIZE)
  171. { /* 完全插入 */
  172. for(i=S[0];i>=pos;i--)
  173. S[i+T[0]]=S[i];
  174. for(i=pos;i<pos+T[0];i++)
  175. S[i]=T[i-pos+1];
  176. S[0]=S[0]+T[0];
  177. return TRUE;
  178. }
  179. else
  180. { /* 部分插入 */
  181. for(i=MAXSIZE;i<=pos;i--)
  182. S[i]=S[i-T[0]];
  183. for(i=pos;i<pos+T[0];i++)
  184. S[i]=T[i-pos+1];
  185. S[0]=MAXSIZE;
  186. return FALSE;
  187. }
  188. }
  189.  
  190. /* 初始条件: 串S存在,1≤pos≤StrLength(S)-len+1 */
  191. /* 操作结果: 从串S中删除第pos个字符起长度为len的子串 */
  192. Status StrDelete(String S,int len)
  193. {
  194. int i;
  195. if(pos<1||pos>S[0]-len+1||len<0)
  196. return ERROR;
  197. for(i=pos+len;i<=S[0];i++)
  198. S[i-len]=S[i];
  199. S[0]-=len;
  200. return OK;
  201. }
  202.  
  203. /* 初始条件: 串S,T和V存在,T是非空串(此函数与串的存储结构无关) */
  204. /* 操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 */
  205. Status Replace(String S,String V)
  206. {
  207. int i=1; /* 从串S的第一个字符起查找串T */
  208. if(StrEmpty(T)) /* T是空串 */
  209. return ERROR;
  210. do
  211. {
  212. i=Index(S,T,i); /* 结果i为从上一个i之后找到的子串T的位置 */
  213. if(i) /* 串S中存在串T */
  214. {
  215. StrDelete(S,StrLength(T)); /* 删除该串T */
  216. StrInsert(S,V); /* 在原串T的位置插入串V */
  217. i+=StrLength(V); /* 在插入的串V后面继续查找串T */
  218. }
  219. }while(i);
  220. return OK;
  221. }
  222.  
  223. /* 输出字符串T */
  224. void StrPrint(String T)
  225. {
  226. int i;
  227. for(i=1;i<=T[0];i++)
  228. printf("%c",T[i]);
  229. printf("\n");
  230. }
  231.  
  232.  
  233. int main()
  234. {
  235. int i,j;
  236. Status k;
  237. char s;
  238. String t,s1,s2;
  239. printf("请输入串s1: ");
  240. k=StrAssign(s1,"abcd");
  241. if(!k)
  242. {
  243. printf("串长超过MAXSIZE(=%d)\n",MAXSIZE);
  244. exit(0);
  245. }
  246. printf("串长为%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1));
  247. StrCopy(s2,s1);
  248. printf("拷贝s1生成的串为: ");
  249. StrPrint(s2);
  250. printf("请输入串s2: ");
  251. k=StrAssign(s2,"efghijk");
  252. if(!k)
  253. {
  254. printf("串长超过MAXSIZE(%d)\n",MAXSIZE);
  255. exit(0);
  256. }
  257. i=StrCompare(s1,s2);
  258. if(i<0)
  259. s='<';
  260. else if(i==0)
  261. s='=';
  262. else
  263. s='>';
  264. printf("串s1%c串s2\n",s);
  265. k=Concat(t,s2);
  266. printf("串s1联接串s2得到的串t为: ");
  267. StrPrint(t);
  268. if(k==FALSE)
  269. printf("串t有截断\n");
  270. ClearString(s1);
  271. printf("清为空串后,串s1为: ");
  272. StrPrint(s1);
  273. printf("串长为%d 串空否?%d(1:是 0:否)\n",StrEmpty(s1));
  274. printf("求串t的子串,请输入子串的起始位置,子串长度: ");
  275.  
  276. i=2;
  277. j=3;
  278. printf("%d,%d \n",j);
  279.  
  280. k=SubString(s2,t,j);
  281. if(k)
  282. {
  283. printf("子串s2为: ");
  284. StrPrint(s2);
  285. }
  286. printf("从串t的第pos个字符起,删除len个字符,请输入pos,len: ");
  287. i=4;
  288. j=2;
  289. printf("%d,j);
  290.  
  291.  
  292. StrDelete(t,j);
  293. printf("删除后的串t为: ");
  294. StrPrint(t);
  295. i=StrLength(s2)/2;
  296. StrInsert(s2,t);
  297. printf("在串s2的第%d个字符之前插入串t后,串s2为:\n",i);
  298. StrPrint(s2);
  299. i=Index(s2,1);
  300. printf("s2的第%d个字母起和t第一次匹配\n",i);
  301. SubString(t,s2,1,1);
  302. printf("t为:");
  303. StrPrint(t);
  304. Concat(s1,t);
  305. printf("s1为:");
  306. StrPrint(s1);
  307. Replace(s2,s1);
  308. printf("用串s1取代串s2中和串t相同的不重叠的串后,串s2为: ");
  309. StrPrint(s2);
  310.  
  311.  
  312. return 0;
  313. }

猜你在找的数据结构相关文章