1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
| #include <stdio.h> #include <stdlib.h>
#define true 1 #define false 0
typedef int Bool; typedef struct node { char data; struct node *next; } strLink;
strLink * InitStr(); void StrAssign(strLink *s,char cstr[]); void DispStr(strLink *s); int StrLength(strLink *s); strLink * InsStr(strLink *s,int i,strLink *t); strLink * DelStr(strLink *s,int i,int j); strLink * RepStr(strLink *s,int i,int j,strLink *t); strLink * SubStr(strLink *s,int i,int j); strLink * Concat(strLink *s,strLink *t); strLink * StrCopy(strLink *s); Bool StrEqual(strLink *s,strLink *t);
int main() { strLink *s,*s1,*s2,*s3,*s4,*s5; printf("链串的基本运算如下:\n"); printf(" (1)建立串s和串s1\n"); s = InitStr(); StrAssign(s,"abcdefghijklmn"); printf(" (2)输出串s:"); DispStr(s); s1 = InitStr(); StrAssign(s1,"123"); printf(" (2)输出串s1:"); DispStr(s1); printf(" (3)串s的长度:%d\n",StrLength(s)); printf(" (4)在串s的第9个字符位置插入串s1而产生串s2\n"); s2 = InsStr(s,9,s1); printf(" (5)输出串s2:"); DispStr(s2); printf(" (6)删除串s第2个字符开始的5个字符而产生串s2\n"); s2=DelStr(s,2,5); printf(" (7)输出串s2:"); DispStr(s2); printf(" (8)将串s第2个字符开始的5个字符替换成串s1而产生串s2\n"); s2 = RepStr(s,2,5,s1); printf(" (9)输出串s2:"); DispStr(s2); printf(" (10)提取串s的第2个字符开始的10个字符而产生串s3\n"); s3 = SubStr(s,2,10); printf(" (11)输出串s3:"); DispStr(s3); printf(" (12)将串s和串s1连接起来而产生串s4\n"); s4 = Concat(s,s1); printf(" (13)输出串s4:"); DispStr(s4);
printf(" (14)复制串s返回s5\n"); s5 = StrCopy(s); printf(" (15)输出串s5:"); DispStr(s5);
printf(" (16)比较串s和s5\n"); if(StrEqual(s,s5)) printf(" (17)串s和s5相等"); else printf(" (17)串s和s5不相等"); return 0; }
strLink * InitStr() { strLink *tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL; return tmp; }
void StrAssign(strLink *s,char cstr[]) { int i = 0; strLink *tmp,*p; p = s; while(cstr[i] != '\0') { tmp = (strLink *)malloc(sizeof(strLink)); tmp->data = cstr[i]; p->next = tmp; p = tmp; i++; } p->next = NULL; }
void DispStr(strLink *s) { strLink *tmp = s->next; while (tmp != NULL) { printf("%c ",tmp->data); tmp = tmp->next; } printf("\n"); }
int StrLength(strLink *s) { strLink *tmp = s->next; int i = 0; while (tmp != NULL) { i++; tmp = tmp->next; } return i; }
strLink * InsStr(strLink *s,int i,strLink *t) { strLink *tmp,*p1=s->next,*p2=t->next,*q,*r; int j; tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL; i--; if(i < 0 || i > StrLength(s)) return tmp;
r = tmp; for ( j = 0; j < i; j++) { q = (strLink *)malloc(sizeof(strLink)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; }
while(p2 != NULL) { q = (strLink *)malloc(sizeof(strLink)); q->data = p2->data; r->next = q; r = q; p2 = p2->next; } for ( j = i; j < StrLength(s); j++) { q = (strLink *)malloc(sizeof(strLink)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; } r->next = NULL; return tmp; }
strLink *DelStr(strLink *s,int i,int j) { strLink *tmp,*r,*q,*p = s->next; int k=0; tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL;
if(i+j-1 > StrLength(s)) return tmp; r = tmp; i--; while (k < i) { q = (strLink *)malloc(sizeof(strLink)); q->data = p->data; r->next = q; r = q; p = p->next; k++; }
while (k < i+j){ p = p->next; k++; } while ( k < StrLength(s)) { q = (strLink *)malloc(sizeof(strLink)); q->data = p->data; r->next = q; r = q; p = p->next; k++; } r->next = NULL; return tmp; }
strLink *RepStr(strLink *s,int i,int j,strLink *t) { strLink *tmp,*r,*q,*p1=s->next,*p2=t->next; int k = 0; tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL;
if(i+j-1 > StrLength(s)) return tmp; r = tmp; i--; while (k < i) { q = (strLink *)malloc(sizeof(strLink)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; k++; }
while (p2 != NULL) { q = (strLink *)malloc(sizeof(strLink)); q->data = p2->data; r->next = q; r = q; p2 = p2->next; }
while (k < i+j) { p1 = p1->next; k++; }
while ( k < StrLength(s)) { q = (strLink *)malloc(sizeof(strLink)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; k++; } r->next = NULL;
return tmp; }
strLink * SubStr(strLink *s,int i,int j) { int k = 0; strLink *tmp,*p = s->next,*r,*q; tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL;
if(i-1+j > StrLength(s)) return tmp; r = tmp; i--; while (k < i) { p = p->next; k++; }
while (k < i+j) { q = (strLink *)malloc(sizeof(strLink)); q->data = p->data; r->next = q; r = q; p = p->next; k++; } r->next = NULL; return tmp; }
strLink * Concat(strLink *s,strLink *t) { strLink *tmp,*r,*q,*p1 =s->next,*p2=t->next; tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL;
r = tmp; while (p1 != NULL) { q = (strLink *)malloc(sizeof(strLink)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; }
while (p2 != NULL) { q = (strLink *)malloc(sizeof(strLink)); q->data = p2->data; r->next = q; r = q; p2 = p2->next; } r->next = NULL; return tmp; }
strLink * StrCopy(strLink *s) { strLink *tmp,*r,*q,*p1=s->next; tmp = (strLink *)malloc(sizeof(strLink)); tmp->next = NULL;
r = tmp; while (p1 != NULL) { q = (strLink *)malloc(sizeof(strLink)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; } return tmp; }
Bool StrEqual(strLink *s,strLink *t) { strLink *p1=s->next,*p2=t->next; if(StrLength(s) != StrLength(t)) return false;
while (p1 != NULL) { if(p1->data != p2->data) return false;
p1=p1->next; p2=p2->next; } return true; }
|