当前位置:   article > 正文

【详解】串(顺序串,链串)的基本运算(插入,链接,替换等全有)有给源码----看这一篇就够了_顺序串插入

顺序串插入

介绍:

什么是串,字符串简称串,剩下大家都知道,我就不多说,直接上重点,基本运算。

void StrAssign(SqString* s, char* cstr) 字符串常量赋给串s

void Display(SqString* s) 输出串s

void Destroy(SqString* s) 摧毁串

void StrCopy(SqString* s, SqString* t) 串复制

bool StrEqual(SqString* s, SqString* t) 判串相等

int StrLength(SqString* s) 求串长

SqString* Concat(SqString* s, SqString* t) 串连接

SqString* SubStr(SqString* s, int i, int j) 求子串

SqString* InsStr(SqString*str1, int i, SqString*str2) 插入串

SqString* DelStr(SqString* str, int i, int j) 串删去

SqString* RepStr(SqString* s, int i, int j, SqString* t) 子串替换

顺序串的基本运算

结构体

SqString

  1. typedef struct str
  2. {
  3. char data[1000];
  4. int length;
  5. }SqString;

void StrAssign(SqString* s, char* cstr)

字符串常量赋给串s

  1. void StrAssign(SqString* s, char* cstr)
  2. {
  3. strcpy(s->data, cstr);
  4. return;
  5. }

void Display(SqString* s)

 输出串s

  1. void Display(SqString* s)
  2. {
  3. printf("%s", s->data);//*s.data
  4. return;
  5. }

void Destroy(SqString* s)

摧毁串

  1. void Destroy(SqString* s)
  2. {
  3. free(s);
  4. }

void StrCopy(SqString* s, SqString* t) 

串复制

  1. void StrCopy(SqString* s, SqString* t)
  2. {
  3. strcpy(s->data, t->data);
  4. return;
  5. }

bool StrEqual(SqString* s, SqString* t)

 判串相等

  1. bool StrEqual(SqString* s, SqString* t)
  2. {
  3. int x = strcmp(s->data, t->data);
  4. if (x == 0) return true;
  5. else return false;
  6. }

int StrLength(SqString* s) 

求串长

  1. int StrLength(SqString* s)
  2. {
  3. int count = strlen(s->data);
  4. return count;
  5. }

SqString* Concat(SqString* s, SqString* t) 

串连接

  1. SqString* Concat(SqString* s, SqString* t)
  2. {
  3. SqString* c = (SqString*)malloc(sizeof(SqString));
  4. strcpy(c->data, s->data);
  5. strcat(c->data, t->data);
  6. return c;
  7. }

SqString* SubStr(SqString* s, int i, int j)

 求子串

  1. SqString* SubStr(SqString* s, int i, int j)
  2. {
  3. int len = StrLength(s);
  4. SqString* last = (SqString*)malloc(sizeof(SqString));
  5. if (i<1 || i>len || j<0 || i + j - 1>len) return last;
  6. int k = 0;
  7. for ( k = 0; k < j; k++)
  8. {
  9. last->data[k] = s->data[i + k-1];
  10. }
  11. last->data[k] = '\0';
  12. return last;
  13. }

SqString* InsStr(SqString*str1, int i, SqString*str2)

 插入串

  1. SqString* InsStr(SqString*str1, int i, SqString*str2)
  2. {
  3. int len1 = StrLength(str1);
  4. int len2 = StrLength(str2);
  5. SqString* last = (SqString*)malloc(sizeof(SqString));
  6. if (i <= 0 || i > len1 + 1) return last;
  7. int k = 0;
  8. int j = 0;
  9. int h = 0;
  10. for (k = 0; k < i - 1; k++)
  11. {
  12. last->data[k] = str1->data[k];
  13. }
  14. for ( j = 0; j < len2; j++)
  15. {
  16. last->data[k + j] = str2->data[j];
  17. }
  18. for ( h = k; h < len1; h++)//小心
  19. {
  20. last->data[h + k + j-1] = str1->data[h];
  21. }
  22. last->data[h + k + j-1] = '\0';//字符串一定记得要给一个结束标志
  23. return last;
  24. }

SqString* DelStr(SqString* str, int i, int j)

 串删去

  1. SqString* DelStr(SqString* str, int i, int j)
  2. {
  3. int len = StrLength(str);
  4. SqString* last = (SqString*)malloc(sizeof(SqString));
  5. if (i <= 0 || i > len || i + j - 1 > len) return last;//防止越界,力扣不这样做会ovreflow
  6. int x, y, z;
  7. for (x = 0; x < i - 1; x++)
  8. {
  9. last->data[x] = str->data[x];
  10. }//x=i-1
  11. for (y = i + j - 1; y < len; y++)
  12. {
  13. last->data[x++] = str->data[y];
  14. }
  15. last->data[x] = '\0';
  16. return last;
  17. }

SqString* RepStr(SqString* s, int i, int j, SqString* t)

 子串替换

  1. SqString* RepStr(SqString* s, int i, int j, SqString* t)
  2. {
  3. int len = StrLength(s);
  4. int len2 = StrLength(t);
  5. SqString* last = (SqString*)malloc(sizeof(SqString));
  6. if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
  7. int x, y, z;
  8. for (x = 0; x < i - 1; x++)
  9. {
  10. last->data[x] = s->data[x];
  11. }
  12. for (y = 0; y < len2; y++)
  13. {
  14. last->data[x + y] = t->data[y];
  15. }
  16. int h = 0;
  17. for (z = i + j - 1; z < len; z++)
  18. {
  19. last->data[x + y +h] = s->data[z];
  20. h++;
  21. }
  22. last->data[x + y + h] = '\0';
  23. return last;
  24. }

总代码c语言

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //串的基本运算
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. typedef struct str
  8. {
  9. char data[1000];
  10. int length;
  11. }SqString;
  12. void StrAssign(SqString* s, char* cstr)
  13. {
  14. strcpy(s->data, cstr);
  15. return;
  16. }
  17. void Display(SqString* s)
  18. {
  19. printf("%s", s->data);//*s.data
  20. return;
  21. }
  22. void Destroy(SqString* s)
  23. {
  24. free(s);
  25. }
  26. void StrCopy(SqString* s, SqString* t)
  27. {
  28. strcpy(s->data, t->data);
  29. return;
  30. }
  31. bool StrEqual(SqString* s, SqString* t)
  32. {
  33. int x = strcmp(s->data, t->data);
  34. if (x == 0) return true;
  35. else return false;
  36. }
  37. int StrLength(SqString* s)
  38. {
  39. int count = strlen(s->data);
  40. return count;
  41. }
  42. SqString* Concat(SqString* s, SqString* t)
  43. {
  44. SqString* c = (SqString*)malloc(sizeof(SqString));
  45. strcpy(c->data, s->data);
  46. strcat(c->data, t->data);
  47. return c;
  48. }
  49. SqString* SubStr(SqString* s, int i, int j)
  50. {
  51. int len = StrLength(s);
  52. SqString* last = (SqString*)malloc(sizeof(SqString));
  53. if (i<1 || i>len || j<0 || i + j - 1>len) return last;
  54. int k = 0;
  55. for ( k = 0; k < j; k++)
  56. {
  57. last->data[k] = s->data[i + k-1];
  58. }
  59. last->data[k] = '\0';
  60. return last;
  61. }
  62. SqString* InsStr(SqString*str1, int i, SqString*str2)
  63. {
  64. int len1 = StrLength(str1);
  65. int len2 = StrLength(str2);
  66. SqString* last = (SqString*)malloc(sizeof(SqString));
  67. if (i <= 0 || i > len1 + 1) return last;
  68. int k = 0;
  69. int j = 0;
  70. int h = 0;
  71. for (k = 0; k < i - 1; k++)
  72. {
  73. last->data[k] = str1->data[k];
  74. }
  75. for ( j = 0; j < len2; j++)
  76. {
  77. last->data[k + j] = str2->data[j];
  78. }
  79. for ( h = k; h < len1; h++)//小心
  80. {
  81. last->data[h + k + j-1] = str1->data[h];
  82. }
  83. last->data[h + k + j-1] = '\0';//字符串一定记得要给一个结束标志
  84. return last;
  85. }
  86. SqString* DelStr(SqString* str, int i, int j)
  87. {
  88. int len = StrLength(str);
  89. SqString* last = (SqString*)malloc(sizeof(SqString));
  90. if (i <= 0 || i > len || i + j - 1 > len) return last;//防止越界,力扣不这样做会ovreflow
  91. int x, y, z;
  92. for (x = 0; x < i - 1; x++)
  93. {
  94. last->data[x] = str->data[x];
  95. }//x=i-1
  96. for (y = i + j - 1; y < len; y++)
  97. {
  98. last->data[x++] = str->data[y];
  99. }
  100. last->data[x] = '\0';
  101. return last;
  102. }
  103. SqString* RepStr(SqString* s, int i, int j, SqString* t)
  104. {
  105. int len = StrLength(s);
  106. int len2 = StrLength(t);
  107. SqString* last = (SqString*)malloc(sizeof(SqString));
  108. if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
  109. int x, y, z;
  110. for (x = 0; x < i - 1; x++)
  111. {
  112. last->data[x] = s->data[x];
  113. }
  114. for (y = 0; y < len2; y++)
  115. {
  116. last->data[x + y] = t->data[y];
  117. }
  118. int h = 0;
  119. for (z = i + j - 1; z < len; z++)
  120. {
  121. last->data[x + y +h] = s->data[z];
  122. h++;
  123. }
  124. last->data[x + y + h] = '\0';
  125. return last;
  126. }
  127. int main()
  128. {
  129. SqString* str1 = (SqString*)malloc(sizeof(SqString));
  130. SqString* str2 = (SqString*)malloc(sizeof(SqString));
  131. char cstr[20] = "abcdef";
  132. char s[20] = "mm";
  133. //char s[20] = "de";
  134. StrAssign(str1, cstr);
  135. StrAssign(str2, s);
  136. SqString* str4 = RepStr(str1, 2, 3, str2);
  137. printf("%s", str4->data);
  138. return 0;
  139. }

总代码c++

  1. //顺序串基本运算的算法
  2. #include <stdio.h>
  3. #define MaxSize 100
  4. typedef struct
  5. {
  6. char data[MaxSize];
  7. int length; //串长
  8. } SqString;
  9. void StrAssign(SqString &s,char cstr[]) //字符串常量赋给串s
  10. {
  11. int i;
  12. for (i=0;cstr[i]!='\0';i++)
  13. s.data[i]=cstr[i];
  14. s.length=i;
  15. }
  16. void DestroyStr(SqString &s)
  17. { }
  18. void StrCopy(SqString &s,SqString t) //串复制
  19. {
  20. int i;
  21. for (i=0;i<t.length;i++)
  22. s.data[i]=t.data[i];
  23. s.length=t.length;
  24. }
  25. bool StrEqual(SqString s,SqString t) //判串相等
  26. {
  27. bool same=true;
  28. int i;
  29. if (s.length!=t.length) //长度不相等时返回0
  30. same=false;
  31. else
  32. for (i=0;i<s.length;i++)
  33. if (s.data[i]!=t.data[i]) //有一个对应字符不相同时返回0
  34. { same=false;
  35. break;
  36. }
  37. return same;
  38. }
  39. int StrLength(SqString s) //求串长
  40. {
  41. return s.length;
  42. }
  43. SqString Concat(SqString s,SqString t) //串连接
  44. {
  45. SqString str;
  46. int i;
  47. str.length=s.length+t.length;
  48. for (i=0;i<s.length;i++) //将s.data[0..s.length-1]复制到str
  49. str.data[i]=s.data[i];
  50. for (i=0;i<t.length;i++) //将t.data[0..t.length-1]复制到str
  51. str.data[s.length+i]=t.data[i];
  52. return str;
  53. }
  54. SqString SubStr(SqString s,int i,int j) //求子串
  55. {
  56. SqString str;
  57. int k;
  58. str.length=0;
  59. if (i<=0 || i>s.length || j<0 || i+j-1>s.length)
  60. return str; //参数不正确时返回空串
  61. for (k=i-1;k<i+j-1;k++) //将s.data[i..i+j]复制到str
  62. str.data[k-i+1]=s.data[k];
  63. str.length=j;
  64. return str;
  65. }
  66. SqString InsStr(SqString s1,int i,SqString s2) //插入串
  67. {
  68. int j;
  69. SqString str;
  70. str.length=0;
  71. if (i<=0 || i>s1.length+1) //参数不正确时返回空串
  72. return str;
  73. for (j=0;j<i-1;j++) //将s1.data[0..i-2]复制到str
  74. str.data[j]=s1.data[j];
  75. for (j=0;j<s2.length;j++) //将s2.data[0..s2.length-1]复制到str
  76. str.data[i+j-1]=s2.data[j];
  77. for (j=i-1;j<s1.length;j++) //将s1.data[i-1..s1.length-1]复制到str
  78. str.data[s2.length+j]=s1.data[j];
  79. str.length=s1.length+s2.length;
  80. return str;
  81. }
  82. SqString DelStr(SqString s,int i,int j) //串删去
  83. {
  84. int k;
  85. SqString str;
  86. str.length=0;
  87. if (i<=0 || i>s.length || i+j>s.length+1) //参数不正确时返回空串
  88. return str;
  89. for (k=0;k<i-1;k++) //将s.data[0..i-2]复制到str
  90. str.data[k]=s.data[k];
  91. for (k=i+j-1;k<s.length;k++) //将s.data[i+j-1..s.length-1]复制到str
  92. str.data[k-j]=s.data[k];
  93. str.length=s.length-j;
  94. return str;
  95. }
  96. SqString RepStr(SqString s,int i,int j,SqString t) //子串替换
  97. {
  98. int k;
  99. SqString str;
  100. str.length=0;
  101. if (i<=0 || i>s.length || i+j-1>s.length) //参数不正确时返回空串
  102. return str;
  103. for (k=0;k<i-1;k++) //将s.data[0..i-2]复制到str
  104. str.data[k]=s.data[k];
  105. for (k=0;k<t.length;k++) //将t.data[0..t.length-1]复制到str
  106. str.data[i+k-1]=t.data[k];
  107. for (k=i+j-1;k<s.length;k++) //将s.data[i+j-1..s.length-1]复制到str
  108. str.data[t.length+k-j]=s.data[k];
  109. str.length=s.length-j+t.length;
  110. return str;
  111. }
  112. void DispStr(SqString s) //输出串s
  113. {
  114. int i;
  115. if (s.length>0)
  116. { for (i=0;i<s.length;i++)
  117. printf("%c",s.data[i]);
  118. printf("\n");
  119. }
  120. }

链串的基本运算

为了防止文章过长链串的基本运算我直接全部给出有需要的自取

c语言总代码

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //链串
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7. typedef struct snode
  8. {
  9. char data;
  10. struct snode* next;
  11. }LinkStrNode;
  12. void StrAssign(LinkStrNode* s, char*cstr)
  13. {
  14. int len = strlen(cstr);
  15. LinkStrNode* pre = s;
  16. for (int i = 0; i < len; i++)
  17. {
  18. LinkStrNode* p = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  19. p->data = cstr[i];
  20. pre->next = p;
  21. pre = p;
  22. }
  23. pre->next = NULL;
  24. }
  25. void Display(LinkStrNode* s)
  26. {
  27. LinkStrNode* p = s->next;
  28. while (p != NULL)
  29. {
  30. printf("%c", p->data);
  31. p = p->next;
  32. }
  33. return;
  34. }
  35. void DestroyStr(LinkStrNode* s)
  36. {
  37. LinkStrNode* p = s->next;
  38. LinkStrNode* pre = s;
  39. while (p != NULL)
  40. {
  41. free(pre);
  42. pre = p;
  43. p = p->next;
  44. }
  45. free(pre);
  46. }
  47. void StrCopy(LinkStrNode* s, LinkStrNode* t)
  48. {
  49. LinkStrNode* ps = s;
  50. LinkStrNode* pt = t;
  51. while (pt != NULL)
  52. {
  53. ps->data = pt->data;
  54. ps = ps->next; pt = pt->next;
  55. }
  56. return;
  57. }
  58. bool StrEqual(LinkStrNode* s, LinkStrNode* t)
  59. {
  60. LinkStrNode* ps = s;
  61. LinkStrNode* pt = t;
  62. while (ps != NULL && pt != NULL)
  63. {
  64. if (ps->data != pt->data) return false;
  65. ps = ps->next; pt = pt->next;
  66. }
  67. if (ps != NULL || pt != NULL) return false;
  68. return true;
  69. }
  70. int StrLength(LinkStrNode* s)
  71. {
  72. int count = 0;
  73. LinkStrNode* p = s->next;
  74. while (p != NULL)
  75. {
  76. count++;
  77. p = p->next;
  78. }
  79. return count;
  80. }
  81. LinkStrNode* Concat(LinkStrNode* s, LinkStrNode* t)
  82. {
  83. LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  84. LinkStrNode* p = last;
  85. LinkStrNode* ps = s->next;
  86. LinkStrNode* pt = t->next;
  87. while (ps != NULL)
  88. {
  89. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  90. q->data = ps->data;
  91. p->next = q;
  92. ps = ps->next;
  93. p = q;
  94. }
  95. while (pt != NULL)
  96. {
  97. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  98. q->data = pt->data;
  99. p->next = q;
  100. pt = pt->next;
  101. p = q;
  102. }
  103. p->next = NULL;
  104. return last;
  105. }
  106. LinkStrNode* SubStr(LinkStrNode* s, int i, int j)
  107. {
  108. int len = StrLength(s);
  109. LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  110. if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
  111. LinkStrNode* p = s;
  112. LinkStrNode* pres = last;
  113. for (int z = 0; z < i; z++) p = p->next;
  114. for (int x = 0; x < j; x++)
  115. {
  116. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  117. q->data = p->data;
  118. pres->next = q;
  119. pres = q;
  120. p = p->next;
  121. }
  122. pres->next = NULL;
  123. return last;
  124. }
  125. LinkStrNode* InsStr(LinkStrNode* s, int i, LinkStrNode* t)
  126. {
  127. int len = StrLength(s);
  128. LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  129. if (i <= 0 || i > len + 1) return last;
  130. LinkStrNode* ps = s->next;
  131. LinkStrNode* pl = last;
  132. int j, k, h;
  133. for (j = 0;ps!=NULL&& j < i - 1; j++)
  134. {
  135. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  136. q->data = ps->data;
  137. pl->next = q;
  138. pl = q;
  139. ps = ps->next;
  140. }
  141. int len2 = StrLength(t);
  142. LinkStrNode* pt = t->next;
  143. for ( k = 0;pt!=NULL&& k < len2; k++)
  144. {
  145. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  146. q->data = pt->data;
  147. pl->next = q;
  148. pl = q;
  149. pt = pt->next;
  150. }
  151. for (h = j; ps!=NULL&&h < len; h++)
  152. {
  153. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  154. q->data = ps->data;
  155. pl->next = q;
  156. pl = q;
  157. ps = ps->next;
  158. }
  159. pl->next = NULL;
  160. return last;
  161. }
  162. LinkStrNode* DelStr(LinkStrNode* s, int i, int j)
  163. {
  164. int len = StrLength(s);
  165. LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  166. if (len < 1) return last;
  167. if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
  168. LinkStrNode* ps = s->next;
  169. LinkStrNode* pa = last;
  170. int x, y, h;
  171. for ( x = 0; x < i-1; x++)
  172. {
  173. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  174. q->data = ps->data;
  175. pa->next = q;
  176. pa = q;
  177. ps = ps->next;
  178. }
  179. for ( y = 0; y < j; y++)
  180. {
  181. ps = ps->next;
  182. }
  183. for (h = x + y; h < len; h++)
  184. {
  185. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  186. q->data = ps->data;
  187. pa->next = q;
  188. pa = q;
  189. ps = ps->next;
  190. }
  191. pa->next = NULL;
  192. return last;
  193. }
  194. LinkStrNode* RepStr(LinkStrNode* s, int i, int j, LinkStrNode* t)
  195. {
  196. int len = StrLength(s);
  197. LinkStrNode* last = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  198. if (len < 1) return last;
  199. if (i <= 0 || i > len || j<0 || i + j - 1>len) return last;
  200. int x, y, z;
  201. LinkStrNode* ps = s->next;
  202. LinkStrNode* pt = last;
  203. for (x = 0; x < i - 1; x++)
  204. {
  205. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  206. q->data = ps->data;
  207. pt->next = q;
  208. pt = q;
  209. ps = ps->next;
  210. }
  211. int len2 = StrLength(t);
  212. LinkStrNode* pt2 = t->next;
  213. for (y = 0; pt2!=NULL&&y < len2; y++)
  214. {
  215. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  216. q->data = pt2->data;
  217. pt->next = q;
  218. pt = q;
  219. pt2 = pt2->next;
  220. }
  221. int k;
  222. for ( k = 0; k < j; k++) ps = ps->next;
  223. for (z = x + k; z < len; z++)
  224. {
  225. LinkStrNode* q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  226. q->data = ps->data;
  227. pt->next = q;
  228. pt = q;
  229. ps = ps->next;
  230. }
  231. pt->next = NULL;
  232. return last;
  233. }
  234. int main()
  235. {
  236. LinkStrNode* s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  237. LinkStrNode* t = (LinkStrNode*)malloc(sizeof(LinkStrNode));
  238. char str[] = "abcd";
  239. char str2[] = "mmm";
  240. StrAssign(t, str2);
  241. StrAssign(s, str);
  242. //LinkStrNode* y = SubStr(s, 2, 3);
  243. //LinkStrNode*y = Concat(s, t);
  244. //int h = StrLength(s);
  245. //int k = StrEqual(t, s);
  246. //printf("%d", h);
  247. LinkStrNode* y = RepStr(s, 2,2,t);
  248. Display(y);
  249. return 0;
  250. }

c++总代码

  1. //链串基本运算的算法
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. typedef struct snode
  5. {
  6. char data;
  7. struct snode *next;
  8. } LinkStrNode;
  9. void StrAssign(LinkStrNode *&s,char cstr[]) //字符串常量cstr赋给串s
  10. {
  11. int i;
  12. LinkStrNode *r,*p;
  13. s=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  14. r=s; //r始终指向尾结点
  15. for (i=0;cstr[i]!='\0';i++)
  16. { p=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  17. p->data=cstr[i];
  18. r->next=p;r=p;
  19. }
  20. r->next=NULL;
  21. }
  22. void DestroyStr(LinkStrNode *&s)
  23. { LinkStrNode *pre=s,*p=s->next; //pre指向结点p的前驱结点
  24. while (p!=NULL) //扫描链串s
  25. { free(pre); //释放pre结点
  26. pre=p; //pre、p同步后移一个结点
  27. p=pre->next;
  28. }
  29. free(pre); //循环结束时,p为NULL,pre指向尾结点,释放它
  30. }
  31. void StrCopy(LinkStrNode *&s,LinkStrNode *t) //串t复制给串s
  32. {
  33. LinkStrNode *p=t->next,*q,*r;
  34. s=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  35. r=s; //r始终指向尾结点
  36. while (p!=NULL) //将t的所有结点复制到s
  37. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  38. q->data=p->data;
  39. r->next=q;r=q;
  40. p=p->next;
  41. }
  42. r->next=NULL;
  43. }
  44. bool StrEqual(LinkStrNode *s,LinkStrNode *t) //判串相等
  45. {
  46. LinkStrNode *p=s->next,*q=t->next;
  47. while (p!=NULL && q!=NULL && p->data==q->data)
  48. { p=p->next;
  49. q=q->next;
  50. }
  51. if (p==NULL && q==NULL)
  52. return true;
  53. else
  54. return false;
  55. }
  56. int StrLength(LinkStrNode *s) //求串长
  57. {
  58. int i=0;
  59. LinkStrNode *p=s->next;
  60. while (p!=NULL)
  61. { i++;
  62. p=p->next;
  63. }
  64. return i;
  65. }
  66. LinkStrNode *Concat(LinkStrNode *s,LinkStrNode *t) //串连接
  67. {
  68. LinkStrNode *str,*p=s->next,*q,*r;
  69. str=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  70. r=str;
  71. while (p!=NULL) //将s的所有结点复制到str
  72. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  73. q->data=p->data;
  74. r->next=q;r=q;
  75. p=p->next;
  76. }
  77. p=t->next;
  78. while (p!=NULL) //将t的所有结点复制到str
  79. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  80. q->data=p->data;
  81. r->next=q;r=q;
  82. p=p->next;
  83. }
  84. r->next=NULL;
  85. return str;
  86. }
  87. LinkStrNode *SubStr(LinkStrNode *s,int i,int j) //求子串
  88. {
  89. int k;
  90. LinkStrNode *str,*p=s->next,*q,*r;
  91. str=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  92. str->next=NULL;
  93. r=str; //r指向新建链表的尾结点
  94. if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
  95. return str; //参数不正确时返回空串
  96. for (k=0;k<i-1;k++)
  97. p=p->next;
  98. for (k=1;k<=j;k++) //将s的第i个结点开始的j个结点复制到str
  99. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  100. q->data=p->data;
  101. r->next=q;r=q;
  102. p=p->next;
  103. }
  104. r->next=NULL;
  105. return str;
  106. }
  107. LinkStrNode *InsStr(LinkStrNode *s,int i,LinkStrNode *t) //串插入
  108. {
  109. int k;
  110. LinkStrNode *str,*p=s->next,*p1=t->next,*q,*r;
  111. str=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  112. str->next=NULL;
  113. r=str; //r指向新建链表的尾结点
  114. if (i<=0 || i>StrLength(s)+1) //参数不正确时返回空串
  115. return str;
  116. for (k=1;k<i;k++) //将s的前i个结点复制到str
  117. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  118. q->data=p->data;
  119. r->next=q;r=q;
  120. p=p->next;
  121. }
  122. while (p1!=NULL) //将t的所有结点复制到str
  123. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  124. q->data=p1->data;
  125. r->next=q;r=q;
  126. p1=p1->next;
  127. }
  128. while (p!=NULL) //将结点p及其后的结点复制到str
  129. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  130. q->data=p->data;
  131. r->next=q;r=q;
  132. p=p->next;
  133. }
  134. r->next=NULL;
  135. return str;
  136. }
  137. LinkStrNode *DelStr(LinkStrNode *s,int i,int j) //串删去
  138. {
  139. int k;
  140. LinkStrNode *str,*p=s->next,*q,*r;
  141. str=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  142. str->next=NULL;
  143. r=str; //r指向新建链表的尾结点
  144. if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
  145. return str; //参数不正确时返回空串
  146. for (k=0;k<i-1;k++) //将s的前i-1个结点复制到str
  147. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  148. q->data=p->data;
  149. r->next=q;r=q;
  150. p=p->next;
  151. }
  152. for (k=0;k<j;k++) //让p沿next跳j个结点
  153. p=p->next;
  154. while (p!=NULL) //将结点p及其后的结点复制到str
  155. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  156. q->data=p->data;
  157. r->next=q;r=q;
  158. p=p->next;
  159. }
  160. r->next=NULL;
  161. return str;
  162. }
  163. LinkStrNode *RepStr(LinkStrNode *s,int i,int j,LinkStrNode *t) //串替换
  164. {
  165. int k;
  166. LinkStrNode *str,*p=s->next,*p1=t->next,*q,*r;
  167. str=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  168. str->next=NULL;
  169. r=str; //r指向新建链表的尾结点
  170. if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
  171. return str; //参数不正确时返回空串
  172. for (k=0;k<i-1;k++) //将s的前i-1个结点复制到str
  173. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  174. q->data=p->data;q->next=NULL;
  175. r->next=q;r=q;
  176. p=p->next;
  177. }
  178. for (k=0;k<j;k++) //让p沿next跳j个结点
  179. p=p->next;
  180. while (p1!=NULL) //将t的所有结点复制到str
  181. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  182. q->data=p1->data;q->next=NULL;
  183. r->next=q;r=q;
  184. p1=p1->next;
  185. }
  186. while (p!=NULL) //将结点p及其后的结点复制到str
  187. { q=(LinkStrNode *)malloc(sizeof(LinkStrNode));
  188. q->data=p->data;q->next=NULL;
  189. r->next=q;r=q;
  190. p=p->next;
  191. }
  192. r->next=NULL;
  193. return str;
  194. }
  195. void DispStr(LinkStrNode *s) //输出串
  196. {
  197. LinkStrNode *p=s->next;
  198. while (p!=NULL)
  199. { printf("%c",p->data);
  200. p=p->next;
  201. }
  202. printf("\n");
  203. }

小结:

对文章有任何问题的还请指出,接受大家的批评,让我改进,如果大家有所收获的话还请不要吝啬你们的点赞和收藏,这可以激励我写出更加优秀的文章,本文代码均经过调试运行,如果对你有帮助的话还请给我点个赞。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/523813
推荐阅读
相关标签
  

闽ICP备14008679号