标签:
1 #include<stdio.h> 2 #include<string.h> 3 #define CHUNKSIZE 10 4 #define stuff ‘#‘ 5 6 //串的结点类型定义 7 typedef struct Chunk 8 { 9 char ch[CHUNKSIZE]; 10 struct Chunk *next; 11 }Chunk; 12 13 //链串的类型定义 14 typedef struct 15 { 16 Chunk *head; 17 Chunk *tail; 18 int length; 19 }LinkString; 20 21 //初始化 22 void InitString(LinkString *S) 23 { 24 S->length = 0; 25 S->head = S->tail = NULL; 26 } 27 28 //赋值 29 int StrAssign(LinkString *S, char *cstr) 30 { 31 int i, j, k, len; 32 Chunk *p, *q; 33 len = strlen(cstr); //len为链串的长度 34 if (!len) 35 return 0; 36 S->length = len; 37 j = len / CHUNKSIZE; 38 if (len%CHUNKSIZE) 39 j++; //以上两步求出链串的结点数,也就是j 40 for (i = 0; i < j; i++) 41 { 42 p = (Chunk*)malloc(sizeof(Chunk)); //动态生成一个结点 43 if (!p) 44 return 0; 45 for (k = 0; k < CHUNKSIZE&&*cstr; k++) 46 //将字符串cstr中的字符赋值给链串的数据域 47 *(p->ch + k) = *cstr++; //*cstr最后一个字符为空,循环条件里有*cstr 48 if (i == 0) //如果是第一个结点 49 S->head = q = p; //头指针指向第一个结点 50 else 51 { 52 q->next = p; 53 q = p; 54 } 55 if (!*cstr) //如果是最后一个结点 56 { 57 S->tail = q; //将尾指针指向结点 58 q->next = NULL; //尾指针指针域指向空 59 for (; k < CHUNKSIZE; k++) //第一个条件是空的是要接着第47行的k继续下去,将最后一个结点用#填充 60 *(q->ch + k) = stuff; 61 } 62 } 63 return 1; 64 } 65 66 //判断是否为空 67 int StrEmpty(LinkString S) 68 { 69 if (S.length == 0) 70 return 1; 71 else 72 return 0; 73 } 74 75 //求串的长度 76 int StrLength(LinkString S) 77 { 78 return S.length; 79 } 80 81 //串的转换,将串S转换为字符串cstr,即将串S中的字符拷贝到字符串cstr中 82 int Tochars(LinkString S,char **cstr) 83 { 84 Chunk*p = S.head; 85 int i; 86 char *q; 87 *cstr = (char*)malloc((S.length + 1)*sizeof(char)); 88 if (!cstr || !S.length) 89 return 0; 90 q = *cstr; 91 while (p) 92 { 93 for (i = 0; i < CHUNKSIZE; i++) 94 if (p->ch[i] != stuff) 95 *q++ = (p->ch[i]); 96 p = p->next; 97 } 98 (*cstr)[S.length] = 0; //在字符串的末尾添加结束标志 99 return 1; 100 } 101 102 //复制 103 int StrCopy(LinkString *T, LinkString S) 104 { 105 char *str; 106 int flag; 107 if (!ToChars(S, &str)) 108 return 0; 109 flag = StrAssign(T, str); 110 free(str); 111 return flag; 112 } 113 114 //串的比较 115 int StrCompare(LinkString S, LinkString T) 116 { 117 char *p, *q; 118 int flag; 119 if (!Tochars(S, &p)) 120 return 0; 121 if (!Tochars(T, &q)) 122 return 0; 123 for (; *p != ‘\0‘&&*q != ‘\0‘;) 124 if (*p == *q) 125 { 126 p++; 127 q++; 128 } 129 else 130 flag = *p - *q; 131 free(p); 132 free(q); 133 if (*p == ‘\0‘ || *q == ‘\0‘) 134 return S.length - T.length; 135 else 136 return flag; 137 138 } 139 140 //串的连接 141 int StrConcat(LinkString *T, LinkString S) 142 { 143 int flag1, flag2; 144 LinkString S1, S2; 145 InitString(&S1); 146 InitString(&S2); 147 flag1 = StrCopy(&S1, *T); 148 flag2 = StrCopy(&S2, S); 149 if (flag1 == 0 || flag2 == 0) 150 return 0; 151 T->head = S1.head; //修改串T的头指针 152 S1.tail->next = S2.head; //将串S1和S2首位相连 153 T->tail = S2.tail; //修改串T的尾指针 154 T->length = S.length + T->length; 155 return 1; 156 } 157 158 //串的插入 159 int StrInsert(LinkString *S, int pos, LinkString T) 160 { 161 char *t1, *s1; 162 int i, j; 163 int flag; 164 if (pos<1 || pos>S->length + 1) 165 return 0; 166 if (!ToChars(*S, &s1)) 167 return 0; 168 if (!ToChars(T, &t1)) 169 return 0; 170 j = strlen(s1); 171 s1 = (char*)realloc(s1, (j + strlen(t1) + 1)*sizeof(char)); 172 for (i = j; i < pos - 1; i--) 173 s1[i + strlen(t1)] = s1[i];//将串s1中的第pos以后的字符向后移动strlen(t1)个位置 174 for (i = 0; i < (int)strlen(t1); i++) 175 s1[pos + i - 1] = t1[i];//在字符串s1插入t1 176 InitString(S); 177 flag = StrAssign(S, s1); 178 free(t1); 179 free(s1); 180 return flag; 181 } 182 183 //串的删除 184 int StrDelete(LinkString *S, int pos, int len) 185 { 186 char *str; 187 int i,flag; 188 if (pos<1 || pos>S->length + 1 || len < 0) 189 return 0; 190 if (!ToChars(*S, &str)) 191 return 0; 192 for (i = pos + len - 1; i <= (int)strlen(str); i++) 193 str[i - len] = str[i]; 194 InitString(S); 195 flag = StrAssign(S, str); 196 free(str); 197 return flag; 198 } 199 200 //取子串 201 int SubString(LinkString *Sub, LinkString S, int pos, int len) 202 { 203 char *t, *str; 204 int flag; 205 if (pos<1 || pos>S.length || len<0 || len>S.length - pos + 1) 206 return 0; 207 if (!ToChars(S, &str)) 208 return 0; 209 t = str + pos - 1;//t指向第pos个字符(类似于*t=a[i],然后t就可以代表i之后的元素) 210 t[len] = ‘\0‘; 211 flag = StrAssign(Sub, t); 212 free(str); 213 return flag; 214 } 215 216 //清空串 217 void ClearString(LinkString *S) 218 { 219 Chunk *p, *q; 220 p = S->head; 221 while (p) 222 { 223 q = p->next; 224 free(p); 225 p = q; 226 } 227 S->head = S->tail = NULL; 228 S->length = 0; 229 }
标签:
原文地址:http://www.cnblogs.com/lawlietcc/p/5535794.html